nautilus_model/python/
macros.rs1#[macro_export]
19macro_rules! identifier_for_python {
20 ($ty:ty) => {
21 #[pymethods]
22 impl $ty {
23 #[new]
24 fn py_new(value: &str) -> PyResult<Self> {
25 <$ty>::new_checked(value).map_err(to_pyvalue_err)
26 }
27
28 fn __setstate__(&mut self, state: &Bound<'_, PyAny>) -> PyResult<()> {
29 let py_tuple: &Bound<'_, PyTuple> = state.downcast::<PyTuple>()?;
30 let bindings = py_tuple.get_item(0)?;
31 let value = bindings.downcast::<PyString>()?.extract::<&str>()?;
32 self.set_inner(value);
33 Ok(())
34 }
35
36 fn __getstate__(&self, py: Python) -> PyResult<PyObject> {
37 use pyo3::IntoPyObjectExt;
38 (self.to_string(),).into_py_any(py)
39 }
40
41 fn __reduce__(&self, py: Python) -> PyResult<PyObject> {
42 use pyo3::IntoPyObjectExt;
43 let safe_constructor = py.get_type::<Self>().getattr("_safe_constructor")?;
44 let state = self.__getstate__(py)?;
45 (safe_constructor, PyTuple::empty(py), state).into_py_any(py)
46 }
47
48 #[staticmethod]
49 fn _safe_constructor() -> PyResult<Self> {
50 Ok(<$ty>::from("NULL")) }
52
53 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
57 use nautilus_core::python::IntoPyObjectNautilusExt;
58
59 match op {
60 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
61 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
62 CompareOp::Ge => self.ge(other).into_py_any_unwrap(py),
63 CompareOp::Gt => self.gt(other).into_py_any_unwrap(py),
64 CompareOp::Le => self.le(other).into_py_any_unwrap(py),
65 CompareOp::Lt => self.lt(other).into_py_any_unwrap(py),
66 }
67 }
68
69 fn __hash__(&self) -> isize {
70 self.inner().precomputed_hash() as isize
71 }
72
73 fn __repr__(&self) -> String {
74 format!(
75 "{}('{}')",
76 stringify!($ty).split("::").last().unwrap_or(""),
77 self.as_str()
78 )
79 }
80
81 fn __str__(&self) -> &'static str {
82 self.inner().as_str()
83 }
84
85 #[getter]
86 #[pyo3(name = "value")]
87 fn py_value(&self) -> String {
88 self.to_string()
89 }
90
91 #[staticmethod]
92 #[pyo3(name = "from_str")]
93 fn py_from_str(value: &str) -> Self {
94 Self::from(value)
95 }
96 }
97 };
98}