Skip to content

Commit

Permalink
Convert all identifiers to newtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 16, 2024
1 parent c0c98cc commit 16978c8
Show file tree
Hide file tree
Showing 43 changed files with 470 additions and 345 deletions.
10 changes: 5 additions & 5 deletions nautilus_core/adapters/src/databento/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn decode_options_contract_v1(
let currency_str = unsafe { raw_ptr_to_string(msg.currency.as_ptr())? };
let cfi_str = unsafe { raw_ptr_to_string(msg.cfi.as_ptr())? };
let exchange = unsafe { raw_ptr_to_ustr(msg.exchange.as_ptr())? };
let asset_class_opt = match instrument_id.venue.value.as_str() {
let asset_class_opt = match instrument_id.venue.as_str() {
"OPRA" => Some(AssetClass::Equity),
_ => {
let (asset_class, _) = parse_cfi_iso10926(&cfi_str)?;
Expand Down Expand Up @@ -338,7 +338,7 @@ pub fn decode_options_spread_v1(
let currency_str = unsafe { raw_ptr_to_string(msg.currency.as_ptr())? };
let cfi_str = unsafe { raw_ptr_to_string(msg.cfi.as_ptr())? };
let exchange = unsafe { raw_ptr_to_ustr(msg.exchange.as_ptr())? };
let asset_class_opt = match instrument_id.venue.value.as_str() {
let asset_class_opt = match instrument_id.venue.as_str() {
"OPRA" => Some(AssetClass::Equity),
_ => {
let (asset_class, _) = parse_cfi_iso10926(&cfi_str)?;
Expand Down Expand Up @@ -632,7 +632,7 @@ pub fn decode_ohlcv_msg(

// Adjust raw prices by a display factor
let mut display_factor = 1;
if instrument_id.venue.value == "GLBX" {
if instrument_id.venue.as_str() == "GLBX" {
display_factor = 100;
};

Expand Down Expand Up @@ -890,7 +890,7 @@ pub fn decode_options_contract(
let currency_str = unsafe { raw_ptr_to_string(msg.currency.as_ptr())? };
let cfi_str = unsafe { raw_ptr_to_string(msg.cfi.as_ptr())? };
let exchange = unsafe { raw_ptr_to_ustr(msg.exchange.as_ptr())? };
let asset_class_opt = match instrument_id.venue.value.as_str() {
let asset_class_opt = match instrument_id.venue.as_str() {
"OPRA" => Some(AssetClass::Equity),
_ => {
let (asset_class, _) = parse_cfi_iso10926(&cfi_str)?;
Expand Down Expand Up @@ -933,7 +933,7 @@ pub fn decode_options_spread(
) -> anyhow::Result<OptionsSpread> {
let currency_str = unsafe { raw_ptr_to_string(msg.currency.as_ptr())? };
let cfi_str = unsafe { raw_ptr_to_string(msg.cfi.as_ptr())? };
let asset_class_opt = match instrument_id.venue.value.as_str() {
let asset_class_opt = match instrument_id.venue.as_str() {
"OPRA" => Some(AssetClass::Equity),
_ => {
let (asset_class, _) = parse_cfi_iso10926(&cfi_str)?;
Expand Down
10 changes: 2 additions & 8 deletions nautilus_core/adapters/src/databento/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use tokio::{
time::{timeout, Duration},
};
use tracing::{debug, error, info, trace};
use ustr::Ustr;

use super::{
decode::{decode_imbalance_msg, decode_statistics_msg},
Expand Down Expand Up @@ -357,9 +356,7 @@ fn update_instrument_id_map(
.get_for_rec(record)
.expect("Cannot resolve `raw_symbol` from `symbol_map`");

let symbol = Symbol {
value: Ustr::from(raw_symbol),
};
let symbol = Symbol::from_str_unchecked(raw_symbol);

let publisher_id = header.publisher_id;
let venue = publisher_venue_map
Expand All @@ -378,10 +375,7 @@ fn handle_instrument_def_msg(
) -> anyhow::Result<InstrumentAny> {
let c_str: &CStr = unsafe { CStr::from_ptr(msg.raw_symbol.as_ptr()) };
let raw_symbol: &str = c_str.to_str().map_err(to_pyvalue_err)?;

let symbol = Symbol {
value: Ustr::from(raw_symbol),
};
let symbol = Symbol::from(raw_symbol);

let publisher_id = msg.header().publisher_id;
let venue = publisher_venue_map
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/adapters/src/databento/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl DatabentoDataLoader {
raw_ptr_to_ustr(rec.raw_symbol.as_ptr())
.expect("Error obtaining `raw_symbol` pointer")
};
let symbol = Symbol { value: raw_symbol };
let symbol = Symbol::from(raw_symbol);

let venue = self
.publisher_venue_map
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/adapters/src/databento/python/historical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl DatabentoHistoricalClient {

while let Ok(Some(msg)) = decoder.decode_record::<dbn::InstrumentDefMsg>().await {
let raw_symbol = unsafe { raw_ptr_to_ustr(msg.raw_symbol.as_ptr()).unwrap() };
let symbol = Symbol { value: raw_symbol };
let symbol = Symbol::from(raw_symbol);

let publisher = msg.hd.publisher().expect("Invalid `publisher` for record");
let venue = publisher_venue_map
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/common/src/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn get_stream_name(
}

if let Some(json!(true)) = config.get("use_trader_id") {
stream_name.push_str(trader_id.value.as_str());
stream_name.push_str(trader_id.as_str());
stream_name.push(DELIMITER);
}

Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/common/src/xrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn get_exchange_rate(

// Build quote table
for (symbol, quote) in calculation_quotes.iter() {
let pieces: Vec<&str> = symbol.value.as_str().split('/').collect();
let pieces: Vec<&str> = symbol.as_str().split('/').collect();
let code_lhs = Ustr::from(pieces[0]);
let code_rhs = Ustr::from(pieces[1]);

Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/infrastructure/src/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ fn get_trader_key(
key.push_str("trader-");
}

key.push_str(trader_id.value.as_str());
key.push_str(trader_id.as_str());

if let Some(json!(true)) = config.get("use_instance_id") {
key.push(DELIMITER);
Expand Down
7 changes: 2 additions & 5 deletions nautilus_core/model/src/ffi/data/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ pub extern "C" fn quote_tick_eq(lhs: &QuoteTick, rhs: &QuoteTick) -> u8 {
assert_eq!(lhs.bid_size, rhs.bid_size);
assert_eq!(lhs.ts_event, rhs.ts_event);
assert_eq!(lhs.ts_init, rhs.ts_init);
assert_eq!(
lhs.instrument_id.symbol.value,
rhs.instrument_id.symbol.value
);
assert_eq!(lhs.instrument_id.venue.value, rhs.instrument_id.venue.value);
assert_eq!(lhs.instrument_id.symbol, rhs.instrument_id.symbol);
assert_eq!(lhs.instrument_id.venue, rhs.instrument_id.venue);
u8::from(lhs == rhs)
}

Expand Down
6 changes: 3 additions & 3 deletions nautilus_core/model/src/ffi/identifiers/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub unsafe extern "C" fn account_id_new(ptr: *const c_char) -> AccountId {

#[no_mangle]
pub extern "C" fn account_id_hash(id: &AccountId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -51,7 +51,7 @@ mod tests {
let c_string = CString::new(s).unwrap();
let ptr = c_string.as_ptr();
let account_id = unsafe { account_id_new(ptr) };
let char_ptr = account_id.value.as_char_ptr();
let char_ptr = account_id.inner().as_char_ptr();
let account_id_2 = unsafe { account_id_new(char_ptr) };
assert_eq!(account_id, account_id_2);
}
Expand All @@ -62,7 +62,7 @@ mod tests {
let c_string = CString::new(s).unwrap();
let ptr = c_string.as_ptr();
let account_id = unsafe { account_id_new(ptr) };
let cstr_ptr = account_id.value.as_char_ptr();
let cstr_ptr = account_id.inner().as_char_ptr();
let c_str = unsafe { CStr::from_ptr(cstr_ptr) };
assert_eq!(c_str.to_str().unwrap(), s);
}
Expand Down
4 changes: 2 additions & 2 deletions nautilus_core/model/src/ffi/identifiers/client_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub unsafe extern "C" fn client_id_new(ptr: *const c_char) -> ClientId {

#[no_mangle]
pub extern "C" fn client_id_hash(id: &ClientId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -49,7 +49,7 @@ mod tests {
#[rstest]
fn test_client_id_to_cstr_c() {
let id = ClientId::from("BINANCE");
let c_string = id.value.as_char_ptr();
let c_string = id.inner().as_char_ptr();
let rust_string = unsafe { CStr::from_ptr(c_string) }.to_str().unwrap();
assert_eq!(rust_string, "BINANCE");
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/client_order_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn client_order_id_new(ptr: *const c_char) -> ClientOrderI

#[no_mangle]
pub extern "C" fn client_order_id_hash(id: &ClientOrderId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/component_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn component_id_new(ptr: *const c_char) -> ComponentId {

#[no_mangle]
pub extern "C" fn component_id_hash(id: &ComponentId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn exec_algorithm_id_new(ptr: *const c_char) -> ExecAlgori

#[no_mangle]
pub extern "C" fn exec_algorithm_id_hash(id: &ExecAlgorithmId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/order_list_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn order_list_id_new(ptr: *const c_char) -> OrderListId {

#[no_mangle]
pub extern "C" fn order_list_id_hash(id: &OrderListId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/position_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn position_id_new(ptr: *const c_char) -> PositionId {

#[no_mangle]
pub extern "C" fn position_id_hash(id: &PositionId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/strategy_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn strategy_id_new(ptr: *const c_char) -> StrategyId {

#[no_mangle]
pub extern "C" fn strategy_id_hash(id: &StrategyId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn symbol_new(ptr: *const c_char) -> Symbol {

#[no_mangle]
pub extern "C" fn symbol_hash(id: &Symbol) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/trader_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn trader_id_new(ptr: *const c_char) -> TraderId {

#[no_mangle]
pub extern "C" fn trader_id_hash(id: &TraderId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/venue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub unsafe extern "C" fn venue_new(ptr: *const c_char) -> Venue {

#[no_mangle]
pub extern "C" fn venue_hash(id: &Venue) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}

#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/ffi/identifiers/venue_order_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pub unsafe extern "C" fn venue_order_id_new(ptr: *const c_char) -> VenueOrderId

#[no_mangle]
pub extern "C" fn venue_order_id_hash(id: &VenueOrderId) -> u64 {
id.value.precomputed_hash()
id.inner().precomputed_hash()
}
42 changes: 29 additions & 13 deletions nautilus_core/model/src/identifiers/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,55 @@ use ustr::Ustr;
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
)]
pub struct AccountId {
/// The account ID value.
pub value: Ustr,
}
pub struct AccountId(Ustr);

impl AccountId {
/// Creates a new `AccountId` from the given identifier value.
///
/// # Panics
///
/// Panics if the value is not a valid string, or does not contain a hyphen '-' separator.
pub fn new(value: &str) -> anyhow::Result<Self> {
check_valid_string(value, stringify!(value))?;
check_string_contains(value, "-", stringify!(value))?;

Ok(Self {
value: Ustr::from(value),
})
Ok(Self(Ustr::from(value)))
}

/// Sets the inner identifier value.
pub(crate) fn set_inner(&mut self, value: &str) {
self.0 = Ustr::from(value);
}

/// Returns the inner identifier value.
#[must_use]
pub fn inner(&self) -> Ustr {
self.0
}

/// Returns the inner identifier value as a string slice.
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}

impl Default for AccountId {
fn default() -> Self {
Self {
value: Ustr::from("SIM-001"),
}
// SAFETY: Default value is safe
Self::new("SIM-001").unwrap()
}
}

impl Debug for AccountId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.value)
write!(f, "{:?}", self.0)
}
}

impl Display for AccountId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
write!(f, "{}", self.0)
}
}

Expand Down Expand Up @@ -110,6 +126,6 @@ mod tests {

#[rstest]
fn test_string_reprs(account_ib: AccountId) {
assert_eq!(account_ib.to_string(), "IB-1234567890");
assert_eq!(account_ib.as_str(), "IB-1234567890");
}
}
Loading

0 comments on commit 16978c8

Please sign in to comment.