nautilus_model/python/identifiers/
instrument_id.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19 str::FromStr,
20};
21
22use nautilus_core::python::{IntoPyObjectNautilusExt, correctness_error_to_pyvalue_err};
23use pyo3::{
24 IntoPyObjectExt,
25 prelude::*,
26 pyclass::CompareOp,
27 types::{PyString, PyTuple},
28};
29
30use crate::{
31 enums::InstrumentClass,
32 identifiers::{InstrumentId, Symbol, Venue},
33 python::instrument_id_error_to_pyvalue_err,
34};
35
36#[pymethods]
37#[pyo3_stub_gen::derive::gen_stub_pymethods]
38impl InstrumentId {
39 #[new]
43 fn py_new(symbol: Symbol, venue: Venue) -> Self {
44 Self::new(symbol, venue)
45 }
46
47 fn __setstate__(&mut self, state: &Bound<'_, PyAny>) -> PyResult<()> {
48 let py_tuple: &Bound<'_, PyTuple> = state.cast::<PyTuple>()?;
49 let symbol = Symbol::new_checked(
50 py_tuple
51 .get_item(0)?
52 .cast::<PyString>()?
53 .extract::<&str>()?,
54 )
55 .map_err(correctness_error_to_pyvalue_err)?;
56 let venue = Venue::new_checked(
57 py_tuple
58 .get_item(1)?
59 .cast::<PyString>()?
60 .extract::<&str>()?,
61 )
62 .map_err(correctness_error_to_pyvalue_err)?;
63 *self = Self::new(symbol, venue);
64 Ok(())
65 }
66
67 fn __getstate__(&self, py: Python) -> PyResult<Py<PyAny>> {
68 (self.symbol.to_string(), self.venue.to_string()).into_py_any(py)
69 }
70
71 fn __reduce__(&self, py: Python) -> PyResult<Py<PyAny>> {
72 let safe_constructor = py.get_type::<Self>().getattr("_safe_constructor")?;
73 let state = self.__getstate__(py)?;
74 (safe_constructor, PyTuple::empty(py), state).into_py_any(py)
75 }
76
77 #[staticmethod]
78 fn _safe_constructor() -> Self {
79 Self::from_str("NULL.NULL").unwrap() }
81
82 #[expect(clippy::needless_pass_by_value)]
83 fn __richcmp__(&self, other: Py<PyAny>, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
84 if let Ok(other) = other.extract::<Self>(py) {
85 match op {
86 CompareOp::Eq => self.eq(&other).into_py_any_unwrap(py),
87 CompareOp::Ne => self.ne(&other).into_py_any_unwrap(py),
88 CompareOp::Ge => self.ge(&other).into_py_any_unwrap(py),
89 CompareOp::Gt => self.gt(&other).into_py_any_unwrap(py),
90 CompareOp::Le => self.le(&other).into_py_any_unwrap(py),
91 CompareOp::Lt => self.lt(&other).into_py_any_unwrap(py),
92 }
93 } else {
94 py.NotImplemented()
95 }
96 }
97
98 fn __hash__(&self) -> isize {
99 let mut h = DefaultHasher::new();
100 self.hash(&mut h);
101 h.finish() as isize
102 }
103
104 fn __repr__(&self) -> String {
105 format!("{}('{}')", stringify!(InstrumentId), self)
106 }
107
108 fn __str__(&self) -> String {
109 self.to_string()
110 }
111
112 #[getter]
113 #[pyo3(name = "symbol")]
114 fn py_symbol(&self) -> Symbol {
115 self.symbol
116 }
117
118 #[getter]
119 #[pyo3(name = "venue")]
120 fn py_venue(&self) -> Venue {
121 self.venue
122 }
123
124 #[getter]
125 fn value(&self) -> String {
126 self.to_string()
127 }
128
129 #[staticmethod]
130 #[pyo3(name = "from_str")]
131 fn py_from_str(value: &str) -> PyResult<Self> {
132 Self::from_str(value).map_err(instrument_id_error_to_pyvalue_err)
133 }
134
135 #[pyo3(name = "is_synthetic")]
136 fn py_is_synthetic(&self) -> bool {
137 self.is_synthetic()
138 }
139
140 #[pyo3(name = "parse_parent_components")]
150 fn py_parse_parent_components(&self) -> Option<(String, InstrumentClass)> {
151 self.parse_parent_components()
152 .map(|(root, class)| (root.to_string(), class))
153 }
154}