Skip to content

Commit 5bdcc54

Browse files
committed
Add errors and panics docs for common crate
1 parent a8de5fd commit 5bdcc54

File tree

16 files changed

+190
-1
lines changed

16 files changed

+190
-1
lines changed

crates/common/src/actor/data_actor.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#![allow(dead_code)]
1818
#![allow(unused_variables)]
1919
#![allow(unused_imports)]
20-
#![allow(clippy::missing_errors_doc)]
2120

2221
use std::{
2322
any::{Any, TypeId},
@@ -1796,6 +1795,13 @@ impl DataActorCore {
17961795

17971796
// -- REQUESTS --------------------------------------------------------------------------------
17981797

1798+
/// Request historical custom data of the given `data_type`.
1799+
///
1800+
/// Returns a unique request ID to correlate subsequent [`CustomDataResponse`].
1801+
///
1802+
/// # Errors
1803+
///
1804+
/// Returns an error if the provided time range is invalid.
17991805
pub fn request_data<A: DataActor>(
18001806
&self,
18011807
data_type: DataType,
@@ -1835,6 +1841,13 @@ impl DataActorCore {
18351841
Ok(request_id)
18361842
}
18371843

1844+
/// Request historical [`InstrumentResponse`] data for the given `instrument_id`.
1845+
///
1846+
/// Returns a unique request ID to correlate subsequent [`InstrumentResponse`].
1847+
///
1848+
/// # Errors
1849+
///
1850+
/// Returns an error if the provided time range is invalid.
18381851
pub fn request_instrument<A: DataActor>(
18391852
&self,
18401853
instrument_id: InstrumentId,
@@ -1875,6 +1888,13 @@ impl DataActorCore {
18751888
Ok(request_id)
18761889
}
18771890

1891+
/// Request historical [`InstrumentsResponse`] definitions for the optional `venue`.
1892+
///
1893+
/// Returns a unique request ID to correlate subsequent [`InstrumentsResponse`].
1894+
///
1895+
/// # Errors
1896+
///
1897+
/// Returns an error if the provided time range is invalid.
18781898
pub fn request_instruments<A: DataActor>(
18791899
&self,
18801900
venue: Option<Venue>,
@@ -1915,6 +1935,13 @@ impl DataActorCore {
19151935
Ok(request_id)
19161936
}
19171937

1938+
/// Request an [`OrderBook`] snapshot for the given `instrument_id`.
1939+
///
1940+
/// Returns a unique request ID to correlate subsequent [`BookResponse`].
1941+
///
1942+
/// # Errors
1943+
///
1944+
/// This function never returns an error.
19181945
pub fn request_book_snapshot<A: DataActor>(
19191946
&self,
19201947
instrument_id: InstrumentId,
@@ -1950,6 +1977,13 @@ impl DataActorCore {
19501977
Ok(request_id)
19511978
}
19521979

1980+
/// Request historical [`QuoteTick`] data for the given `instrument_id`.
1981+
///
1982+
/// Returns a unique request ID to correlate subsequent [`QuotesResponse`].
1983+
///
1984+
/// # Errors
1985+
///
1986+
/// Returns an error if the provided time range is invalid.
19531987
pub fn request_quotes<A: DataActor>(
19541988
&self,
19551989
instrument_id: InstrumentId,
@@ -1992,6 +2026,13 @@ impl DataActorCore {
19922026
Ok(request_id)
19932027
}
19942028

2029+
/// Request historical [`TradeTick`] data for the given `instrument_id`.
2030+
///
2031+
/// Returns a unique request ID to correlate subsequent [`TradesResponse`].
2032+
///
2033+
/// # Errors
2034+
///
2035+
/// Returns an error if the provided time range is invalid.
19952036
pub fn request_trades<A: DataActor>(
19962037
&self,
19972038
instrument_id: InstrumentId,
@@ -2034,6 +2075,13 @@ impl DataActorCore {
20342075
Ok(request_id)
20352076
}
20362077

2078+
/// Request historical [`Bar`] data for the given `bar_type`.
2079+
///
2080+
/// Returns a unique request ID to correlate subsequent [`BarsResponse`].
2081+
///
2082+
/// # Errors
2083+
///
2084+
/// Returns an error if the provided time range is invalid.
20372085
pub fn request_bars<A: DataActor>(
20382086
&self,
20392087
bar_type: BarType,

crates/common/src/actor/registry.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ pub fn get_actor(id: &Ustr) -> Option<Rc<UnsafeCell<dyn Actor>>> {
7575
get_actor_registry().get(id)
7676
}
7777

78+
/// Returns a mutable reference to the registered actor of type `T` for the given `id`.
79+
///
80+
/// # Panics
81+
///
82+
/// Panics if no actor with the specified `id` is found in the registry.
7883
#[allow(clippy::mut_from_ref)]
7984
pub fn get_actor_unchecked<T: Actor>(id: &Ustr) -> &mut T {
8085
let actor = get_actor(id).unwrap_or_else(|| panic!("Actor for {id} not found"));

crates/common/src/cache/database.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
// Under development
1919
#![allow(dead_code)]
2020
#![allow(unused_variables)]
21+
// TODO: Remove once error documentation is complete
22+
#![allow(clippy::missing_errors_doc)]
2123

2224
use std::collections::HashMap;
2325

@@ -155,10 +157,20 @@ pub trait CacheDatabaseAdapter {
155157
/// Returns an error if loading positions fails.
156158
async fn load_positions(&self) -> anyhow::Result<HashMap<PositionId, Position>>;
157159

160+
/// Loads all [`GreeksData`] from the cache.
161+
///
162+
/// # Errors
163+
///
164+
/// Returns an error if loading greeks data fails.
158165
async fn load_greeks(&self) -> anyhow::Result<HashMap<InstrumentId, GreeksData>> {
159166
Ok(HashMap::new())
160167
}
161168

169+
/// Loads all [`YieldCurveData`] from the cache.
170+
///
171+
/// # Errors
172+
///
173+
/// Returns an error if loading yield curve data fails.
162174
async fn load_yield_curves(&self) -> anyhow::Result<HashMap<String, YieldCurveData>> {
163175
Ok(HashMap::new())
164176
}

crates/common/src/cache/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// -------------------------------------------------------------------------------------------------
1515

1616
// Under development
17+
// TODO: Remove once error documentation is complete
1718
#![allow(clippy::missing_errors_doc)]
1819

1920
//! In-memory cache for market and execution data, with optional persistent backing.
@@ -1961,6 +1962,11 @@ impl Cache {
19611962
query
19621963
}
19631964

1965+
/// Retrieves orders corresponding to the given `client_order_ids`, optionally filtering by side.
1966+
///
1967+
/// # Panics
1968+
///
1969+
/// Panics if any `client_order_id` in the set is not found in the cache.
19641970
fn get_orders_for_ids(
19651971
&self,
19661972
client_order_ids: &HashSet<ClientOrderId>,
@@ -1982,6 +1988,11 @@ impl Cache {
19821988
orders
19831989
}
19841990

1991+
/// Retrieves positions corresponding to the given `position_ids`, optionally filtering by side.
1992+
///
1993+
/// # Panics
1994+
///
1995+
/// Panics if any `position_id` in the set is not found in the cache.
19851996
fn get_positions_for_ids(
19861997
&self,
19871998
position_ids: &HashSet<PositionId>,

crates/common/src/clock.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ impl Clock for TestClock {
295295
self.default_callback = Some(callback);
296296
}
297297

298+
/// Returns the handler for the given `TimeEvent`.
299+
///
300+
/// # Panics
301+
///
302+
/// Panics if no event-specific or default callback has been registered for the event.
298303
fn get_handler(&self, event: TimeEvent) -> TimeEventHandlerV2 {
299304
// Get the callback from either the event-specific callbacks or default callback
300305
let callback = self
@@ -539,6 +544,11 @@ impl Clock for LiveClock {
539544
self.default_callback = Some(handler);
540545
}
541546

547+
/// # Panics
548+
///
549+
/// This function panics if:
550+
/// - The `clock_v2` feature is not enabled.
551+
/// - The event does not have an associated handler (see trait documentation).
542552
#[allow(unused_variables)]
543553
fn get_handler(&self, event: TimeEvent) -> TimeEventHandlerV2 {
544554
#[cfg(not(feature = "clock_v2"))]

crates/common/src/ffi/enums.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ pub extern "C" fn component_state_to_cstr(value: ComponentState) -> *const c_cha
2929
/// # Safety
3030
///
3131
/// - Assumes `ptr` is a valid C string pointer.
32+
///
33+
/// # Panics
34+
///
35+
/// Panics if the input C string does not match a valid enum variant.
3236
#[unsafe(no_mangle)]
3337
pub unsafe extern "C" fn component_state_from_cstr(ptr: *const c_char) -> ComponentState {
3438
let value = unsafe { cstr_as_str(ptr) };
@@ -46,6 +50,10 @@ pub extern "C" fn component_trigger_to_cstr(value: ComponentTrigger) -> *const c
4650
/// # Safety
4751
///
4852
/// - Assumes `ptr` is a valid C string pointer.
53+
///
54+
/// # Panics
55+
///
56+
/// Panics if the input C string does not match a valid enum variant.
4957
#[unsafe(no_mangle)]
5058
pub unsafe extern "C" fn component_trigger_from_cstr(ptr: *const c_char) -> ComponentTrigger {
5159
let value = unsafe { cstr_as_str(ptr) };
@@ -63,6 +71,10 @@ pub extern "C" fn log_level_to_cstr(value: LogLevel) -> *const c_char {
6371
/// # Safety
6472
///
6573
/// - Assumes `ptr` is a valid C string pointer.
74+
///
75+
/// # Panics
76+
///
77+
/// Panics if the input C string does not match a valid enum variant.
6678
#[unsafe(no_mangle)]
6779
pub unsafe extern "C" fn log_level_from_cstr(ptr: *const c_char) -> LogLevel {
6880
let value = unsafe { cstr_as_str(ptr) };
@@ -80,6 +92,10 @@ pub extern "C" fn log_color_to_cstr(value: LogColor) -> *const c_char {
8092
/// # Safety
8193
///
8294
/// - Assumes `ptr` is a valid C string pointer.
95+
///
96+
/// # Panics
97+
///
98+
/// Panics if the input C string does not match a valid enum variant.
8399
#[unsafe(no_mangle)]
84100
pub unsafe extern "C" fn log_color_from_cstr(ptr: *const c_char) -> LogColor {
85101
let value = unsafe { cstr_as_str(ptr) };

crates/common/src/ffi/timer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ pub struct TimeEventHandler {
3838
}
3939

4040
impl From<TimeEventHandlerV2> for TimeEventHandler {
41+
/// # Panics
42+
///
43+
/// Panics if the provided `TimeEventHandlerV2` contains a Rust callback,
44+
/// since only Python callbacks are supported by the legacy `TimeEventHandler`.
4145
fn from(value: TimeEventHandlerV2) -> Self {
4246
Self {
4347
event: value.event,

crates/common/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#![deny(rustdoc::broken_intra_doc_links)]
3434
#![deny(missing_debug_implementations)]
3535
#![deny(clippy::missing_errors_doc)]
36+
// TODO: Remove once panic documentation is complete
37+
// #![deny(clippy::missing_panics_doc)]
3638

3739
// Uncomment once we've added trivial `Debug` impls everywhere
3840
// #![warn(missing_debug_implementations)]

crates/common/src/logging/headers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ fn python_package_version(package: &str) -> String {
140140
}
141141

142142
#[cfg(not(feature = "python"))]
143+
/// # Panics
144+
///
145+
/// Panics if the `python` feature is not enabled.
143146
fn python_package_version(_package: &str) -> &str {
144147
panic!("`python` feature is not enabled");
145148
}
@@ -152,6 +155,9 @@ fn python_version() -> String {
152155
}
153156

154157
#[cfg(not(feature = "python"))]
158+
/// # Panics
159+
///
160+
/// Panics if the `python` feature is not enabled.
155161
fn python_version() -> String {
156162
panic!("`python` feature is not enabled");
157163
}

crates/common/src/logging/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ pub const fn map_log_level_to_filter(log_level: LogLevel) -> LevelFilter {
169169
}
170170
}
171171

172+
/// Parses a string into a [`LevelFilter`].
173+
///
174+
/// # Panics
175+
///
176+
/// Panics if the provided string is not a valid `LevelFilter`.
172177
#[must_use]
173178
pub fn parse_level_filter_str(s: &str) -> LevelFilter {
174179
let mut log_level_str = s.to_string().to_uppercase();
@@ -180,6 +185,11 @@ pub fn parse_level_filter_str(s: &str) -> LevelFilter {
180185
}
181186

182187
#[must_use]
188+
/// Parses component-specific log levels from a JSON value map.
189+
///
190+
/// # Panics
191+
///
192+
/// Panics if a JSON value in the map is not a string representing a log level.
183193
pub fn parse_component_levels(
184194
original_map: Option<HashMap<String, serde_json::Value>>,
185195
) -> HashMap<Ustr, LevelFilter> {

crates/common/src/msgbus/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ unsafe impl Sync for ShareableMessageBus {}
6060
static MESSAGE_BUS: OnceLock<ShareableMessageBus> = OnceLock::new();
6161

6262
/// Sets the global message bus.
63+
///
64+
/// # Panics
65+
///
66+
/// Panics if a message bus has already been set.
6367
pub fn set_message_bus(msgbus: Rc<RefCell<MessageBus>>) {
6468
if MESSAGE_BUS.set(ShareableMessageBus(msgbus)).is_err() {
6569
panic!("Failed to set MessageBus");

crates/common/src/msgbus/stubs.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ pub fn get_call_check_shareable_handler(id: Option<Ustr>) -> ShareableMessageHan
106106
}
107107

108108
#[must_use]
109+
/// Returns whether the given `CallCheckMessageHandler` has been invoked at least once.
110+
///
111+
/// # Panics
112+
///
113+
/// Panics if the provided `handler` is not a `CallCheckMessageHandler`.
109114
pub fn check_handler_was_called(call_check_handler: ShareableMessageHandler) -> bool {
110115
call_check_handler
111116
.0
@@ -135,6 +140,11 @@ impl<T: Clone + 'static> MessageHandler for MessageSavingHandler<T> {
135140
self.id
136141
}
137142

143+
/// Handles an incoming message by saving it.
144+
///
145+
/// # Panics
146+
///
147+
/// Panics if the provided `message` is not of the expected type `T`.
138148
fn handle(&self, message: &dyn Any) {
139149
let mut messages = self.messages.borrow_mut();
140150
match message.downcast_ref::<T>() {
@@ -161,6 +171,11 @@ pub fn get_message_saving_handler<T: Clone + 'static>(id: Option<Ustr>) -> Share
161171
}
162172

163173
#[must_use]
174+
/// Retrieves the messages saved by a [`MessageSavingHandler`].
175+
///
176+
/// # Panics
177+
///
178+
/// Panics if the provided `handler` is not a `MessageSavingHandler<T>`.
164179
pub fn get_saved_messages<T: Clone + 'static>(handler: ShareableMessageHandler) -> Vec<T> {
165180
handler
166181
.0

crates/common/src/python/timer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ pub struct TimeEventHandler_Py {
5050
}
5151

5252
impl From<TimeEventHandlerV2> for TimeEventHandler_Py {
53+
/// # Panics
54+
///
55+
/// Panics if the provided `TimeEventHandlerV2` contains a Rust callback,
56+
/// since only Python callbacks are supported by this handler.
5357
fn from(value: TimeEventHandlerV2) -> Self {
5458
Self {
5559
event: value.event,

crates/common/src/timer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ impl Debug for TimeEventCallback {
149149
}
150150

151151
impl TimeEventCallback {
152+
/// Invokes the callback for the given `TimeEvent`.
153+
///
154+
/// # Panics
155+
///
156+
/// Panics if the underlying Python callback invocation fails (e.g., raises an exception).
152157
pub fn call(&self, event: TimeEvent) {
153158
match self {
154159
#[cfg(feature = "python")]
@@ -199,6 +204,11 @@ impl TimeEventHandlerV2 {
199204
Self { event, callback }
200205
}
201206

207+
/// Executes the handler by invoking its callback for the associated event.
208+
///
209+
/// # Panics
210+
///
211+
/// Panics if the underlying callback invocation fails (e.g., a Python callback raises an exception).
202212
pub fn run(self) {
203213
let Self { event, callback } = self;
204214
callback.call(event);

0 commit comments

Comments
 (0)