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