Skip to content

Commit c216bd4

Browse files
committed
Standardize core new and new_checked fn ordering
1 parent 2a0245f commit c216bd4

File tree

6 files changed

+120
-88
lines changed

6 files changed

+120
-88
lines changed

nautilus_core/model/src/data/bar.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,6 @@ pub struct BarSpecification {
131131
}
132132

133133
impl BarSpecification {
134-
/// Creates a new [`BarSpecification`] instance.
135-
#[must_use]
136-
pub fn new(step: usize, aggregation: BarAggregation, price_type: PriceType) -> Self {
137-
Self::new_checked(step, aggregation, price_type).expect(FAILED)
138-
}
139-
140134
/// Creates a new [`BarSpecification`] instance with correctness checking.
141135
///
142136
/// # Errors
@@ -161,6 +155,17 @@ impl BarSpecification {
161155
})
162156
}
163157

158+
/// Creates a new [`BarSpecification`] instance.
159+
///
160+
/// # Panics
161+
///
162+
/// This function panics:
163+
/// - If `step` is not positive (> 0).
164+
#[must_use]
165+
pub fn new(step: usize, aggregation: BarAggregation, price_type: PriceType) -> Self {
166+
Self::new_checked(step, aggregation, price_type).expect(FAILED)
167+
}
168+
164169
pub fn timedelta(&self) -> TimeDelta {
165170
match self.aggregation {
166171
BarAggregation::Millisecond => Duration::milliseconds(self.step.get() as i64),
@@ -574,22 +579,6 @@ pub struct Bar {
574579
}
575580

576581
impl Bar {
577-
/// Creates a new [`Bar`] instance.
578-
#[allow(clippy::too_many_arguments)]
579-
pub fn new(
580-
bar_type: BarType,
581-
open: Price,
582-
high: Price,
583-
low: Price,
584-
close: Price,
585-
volume: Quantity,
586-
ts_event: UnixNanos,
587-
ts_init: UnixNanos,
588-
) -> Self {
589-
Self::new_checked(bar_type, open, high, low, close, volume, ts_event, ts_init)
590-
.expect(FAILED)
591-
}
592-
593582
/// Creates a new [`Bar`] instance with correctness checking.
594583
///
595584
/// # Errors
@@ -631,6 +620,29 @@ impl Bar {
631620
})
632621
}
633622

623+
/// Creates a new [`Bar`] instance.
624+
///
625+
/// # Panics
626+
///
627+
/// This function panics:
628+
/// - If `high` is not >= `low`.
629+
/// - If `high` is not >= `close`.
630+
/// - If `low` is not <= `close.
631+
#[allow(clippy::too_many_arguments)]
632+
pub fn new(
633+
bar_type: BarType,
634+
open: Price,
635+
high: Price,
636+
low: Price,
637+
close: Price,
638+
volume: Quantity,
639+
ts_event: UnixNanos,
640+
ts_init: UnixNanos,
641+
) -> Self {
642+
Self::new_checked(bar_type, open, high, low, close, volume, ts_event, ts_init)
643+
.expect(FAILED)
644+
}
645+
634646
pub fn instrument_id(&self) -> InstrumentId {
635647
self.bar_type.instrument_id()
636648
}

nautilus_core/model/src/data/delta.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,61 +65,66 @@ pub struct OrderBookDelta {
6565
}
6666

6767
impl OrderBookDelta {
68-
/// Creates a new [`OrderBookDelta`] instance.
69-
#[must_use]
70-
pub fn new(
68+
/// Creates a new [`OrderBookDelta`] instance with correctness checking.
69+
///
70+
/// # Errors
71+
///
72+
/// This function returns an error:
73+
/// - If `action` is [`BookAction::Add`] or [`BookAction::Update`] and `size` is not positive (> 0).
74+
///
75+
/// # Notes
76+
///
77+
/// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
78+
pub fn new_checked(
7179
instrument_id: InstrumentId,
7280
action: BookAction,
7381
order: BookOrder,
7482
flags: u8,
7583
sequence: u64,
7684
ts_event: UnixNanos,
7785
ts_init: UnixNanos,
78-
) -> Self {
79-
Self::new_checked(
86+
) -> anyhow::Result<Self> {
87+
if matches!(action, BookAction::Add | BookAction::Update) {
88+
check_positive_u64(order.size.raw, "order.size.raw")?;
89+
}
90+
91+
Ok(Self {
8092
instrument_id,
8193
action,
8294
order,
8395
flags,
8496
sequence,
8597
ts_event,
8698
ts_init,
87-
)
88-
.expect(FAILED)
99+
})
89100
}
90101

91-
/// Creates a new [`OrderBookDelta`] instance with correctness checking.
102+
/// Creates a new [`OrderBookDelta`] instance.
92103
///
93-
/// # Errors
104+
/// # Panics
94105
///
95-
/// This function returns an error:
106+
/// This function panics:
96107
/// - If `action` is [`BookAction::Add`] or [`BookAction::Update`] and `size` is not positive (> 0).
97-
///
98-
/// # Notes
99-
///
100-
/// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
101-
pub fn new_checked(
108+
#[must_use]
109+
pub fn new(
102110
instrument_id: InstrumentId,
103111
action: BookAction,
104112
order: BookOrder,
105113
flags: u8,
106114
sequence: u64,
107115
ts_event: UnixNanos,
108116
ts_init: UnixNanos,
109-
) -> anyhow::Result<Self> {
110-
if matches!(action, BookAction::Add | BookAction::Update) {
111-
check_positive_u64(order.size.raw, "order.size.raw")?;
112-
}
113-
114-
Ok(Self {
117+
) -> Self {
118+
Self::new_checked(
115119
instrument_id,
116120
action,
117121
order,
118122
flags,
119123
sequence,
120124
ts_event,
121125
ts_init,
122-
})
126+
)
127+
.expect(FAILED)
123128
}
124129

125130
/// Creates a new [`OrderBookDelta`] instance with a `Clear` action and and NULL order.

nautilus_core/model/src/data/quote.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,6 @@ pub struct QuoteTick {
6565
}
6666

6767
impl QuoteTick {
68-
/// Creates a new [`QuoteTick`] instance.
69-
pub fn new(
70-
instrument_id: InstrumentId,
71-
bid_price: Price,
72-
ask_price: Price,
73-
bid_size: Quantity,
74-
ask_size: Quantity,
75-
ts_event: UnixNanos,
76-
ts_init: UnixNanos,
77-
) -> Self {
78-
Self::new_checked(
79-
instrument_id,
80-
bid_price,
81-
ask_price,
82-
bid_size,
83-
ask_size,
84-
ts_event,
85-
ts_init,
86-
)
87-
.expect(FAILED)
88-
}
89-
9068
/// Creates a new [`QuoteTick`] instance with correctness checking.
9169
///
9270
/// # Errors
@@ -130,6 +108,34 @@ impl QuoteTick {
130108
})
131109
}
132110

111+
/// Creates a new [`QuoteTick`] instance.
112+
///
113+
/// # Panics
114+
///
115+
/// This function panics:
116+
/// - If `bid_price.precision` does not equal `ask_price.precision`.
117+
/// - If `bid_size.precision` does not equal `ask_size.precision`.
118+
pub fn new(
119+
instrument_id: InstrumentId,
120+
bid_price: Price,
121+
ask_price: Price,
122+
bid_size: Quantity,
123+
ask_size: Quantity,
124+
ts_event: UnixNanos,
125+
ts_init: UnixNanos,
126+
) -> Self {
127+
Self::new_checked(
128+
instrument_id,
129+
bid_price,
130+
ask_price,
131+
bid_size,
132+
ask_size,
133+
ts_event,
134+
ts_init,
135+
)
136+
.expect(FAILED)
137+
}
138+
133139
/// Returns the metadata for the type, for use with serialization formats.
134140
#[must_use]
135141
pub fn get_metadata(

nautilus_core/model/src/data/trade.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,59 +64,64 @@ pub struct TradeTick {
6464
}
6565

6666
impl TradeTick {
67-
/// Creates a new [`TradeTick`] instance.
68-
#[must_use]
69-
pub fn new(
67+
/// Creates a new [`TradeTick`] instance with correctness checking.
68+
///
69+
/// # Errors
70+
///
71+
/// This function returns an error:
72+
/// - If `size` is not positive (> 0).
73+
///
74+
/// # Notes
75+
///
76+
/// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
77+
pub fn new_checked(
7078
instrument_id: InstrumentId,
7179
price: Price,
7280
size: Quantity,
7381
aggressor_side: AggressorSide,
7482
trade_id: TradeId,
7583
ts_event: UnixNanos,
7684
ts_init: UnixNanos,
77-
) -> Self {
78-
Self::new_checked(
85+
) -> anyhow::Result<Self> {
86+
check_positive_u64(size.raw, "size.raw")?;
87+
88+
Ok(Self {
7989
instrument_id,
8090
price,
8191
size,
8292
aggressor_side,
8393
trade_id,
8494
ts_event,
8595
ts_init,
86-
)
87-
.expect(FAILED)
96+
})
8897
}
8998

90-
/// Creates a new [`TradeTick`] instance with correctness checking.
99+
/// Creates a new [`TradeTick`] instance.
91100
///
92-
/// # Errors
101+
/// # Panics
93102
///
94-
/// This function returns an error:
103+
/// This function panics:
95104
/// - If `size` is not positive (> 0).
96-
///
97-
/// # Notes
98-
///
99-
/// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
100-
pub fn new_checked(
105+
#[must_use]
106+
pub fn new(
101107
instrument_id: InstrumentId,
102108
price: Price,
103109
size: Quantity,
104110
aggressor_side: AggressorSide,
105111
trade_id: TradeId,
106112
ts_event: UnixNanos,
107113
ts_init: UnixNanos,
108-
) -> anyhow::Result<Self> {
109-
check_positive_u64(size.raw, "size.raw")?;
110-
111-
Ok(Self {
114+
) -> Self {
115+
Self::new_checked(
112116
instrument_id,
113117
price,
114118
size,
115119
aggressor_side,
116120
trade_id,
117121
ts_event,
118122
ts_init,
119-
})
123+
)
124+
.expect(FAILED)
120125
}
121126

122127
/// Returns the metadata for the type, for use with serialization formats.

nautilus_core/model/src/identifiers/trade_id.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ impl TradeId {
7070
///
7171
/// Maximum length is 36 characters.
7272
///
73-
/// # Errors
73+
/// # Panics
7474
///
75-
/// This function returns an error:
75+
/// This function panics:
7676
/// - If `value` is an invalid string (e.g., is empty or contains non-ASCII characters).
7777
/// - If `value` length exceeds 36 characters.
7878
pub fn new<T: AsRef<str>>(value: T) -> Self {

nautilus_core/model/src/identifiers/trader_id.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ impl TraderId {
4545
///
4646
/// This function returns an error:
4747
/// - If `value` is not a valid string, or does not contain a hyphen '-' separator.
48+
///
49+
/// # Notes
50+
///
51+
/// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
4852
pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
4953
let value = value.as_ref();
5054
check_valid_string(value, stringify!(value))?;

0 commit comments

Comments
 (0)