use std::{
fmt::{Debug, Display},
hash::Hash,
};
use nautilus_core::correctness::{FAILED, check_valid_string_utf8};
use ustr::Ustr;
#[repr(C)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
)]
pub struct Symbol(Ustr);
impl Symbol {
pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
let value = value.as_ref();
check_valid_string_utf8(value, stringify!(value))?;
Ok(Self(Ustr::from(value)))
}
pub fn new<T: AsRef<str>>(value: T) -> Self {
Self::new_checked(value).expect(FAILED)
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(crate) fn set_inner(&mut self, value: &str) {
self.0 = Ustr::from(value);
}
#[must_use]
pub fn from_str_unchecked<T: AsRef<str>>(s: T) -> Self {
Self(Ustr::from(s.as_ref()))
}
#[must_use]
pub const fn from_ustr_unchecked(s: Ustr) -> Self {
Self(s)
}
#[must_use]
pub fn inner(&self) -> Ustr {
self.0
}
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[must_use]
pub fn is_composite(&self) -> bool {
self.as_str().contains('.')
}
#[must_use]
pub fn root(&self) -> &str {
let symbol_str = self.as_str();
if let Some(index) = symbol_str.find('.') {
&symbol_str[..index]
} else {
symbol_str
}
}
#[must_use]
pub fn topic(&self) -> String {
let root_str = self.root();
if root_str == self.as_str() {
root_str.to_string()
} else {
format!("{root_str}*")
}
}
}
impl Debug for Symbol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\"{}\"", self.0)
}
}
impl Display for Symbol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<Ustr> for Symbol {
fn from(value: Ustr) -> Self {
Self(value)
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use crate::identifiers::{Symbol, stubs::*};
#[rstest]
fn test_string_reprs(symbol_eth_perp: Symbol) {
assert_eq!(symbol_eth_perp.as_str(), "ETH-PERP");
assert_eq!(format!("{symbol_eth_perp}"), "ETH-PERP");
}
#[rstest]
#[case("AUDUSD", false)]
#[case("AUD/USD", false)]
#[case("CL.FUT", true)]
#[case("LO.OPT", true)]
#[case("ES.c.0", true)]
fn test_symbol_is_composite(#[case] input: &str, #[case] expected: bool) {
let symbol = Symbol::new(input);
assert_eq!(symbol.is_composite(), expected);
}
#[rstest]
#[case("AUDUSD", "AUDUSD")]
#[case("AUD/USD", "AUD/USD")]
#[case("CL.FUT", "CL")]
#[case("LO.OPT", "LO")]
#[case("ES.c.0", "ES")]
fn test_symbol_root(#[case] input: &str, #[case] expected_root: &str) {
let symbol = Symbol::new(input);
assert_eq!(symbol.root(), expected_root);
}
#[rstest]
#[case("AUDUSD", "AUDUSD")]
#[case("AUD/USD", "AUD/USD")]
#[case("CL.FUT", "CL*")]
#[case("LO.OPT", "LO*")]
#[case("ES.c.0", "ES*")]
fn test_symbol_topic(#[case] input: &str, #[case] expected_topic: &str) {
let symbol = Symbol::new(input);
assert_eq!(symbol.topic(), expected_topic);
}
#[rstest]
#[case("")] #[case(" ")] fn test_symbol_with_invalid_values(#[case] input: &str) {
assert!(Symbol::new_checked(input).is_err());
}
}