Skip to main content

nautilus_model/python/
macros.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Provides macros.
17
18#[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                let validated = <$ty>::new_checked(value).map_err(to_pyvalue_err)?;
34                self.set_inner(validated.as_str());
35                Ok(())
36            }
37
38            fn __getstate__(&self, py: Python) -> PyResult<Py<PyAny>> {
39                use pyo3::IntoPyObjectExt;
40                (self.to_string(),).into_py_any(py)
41            }
42
43            fn __reduce__(&self, py: Python) -> PyResult<Py<PyAny>> {
44                use pyo3::IntoPyObjectExt;
45                let safe_constructor = py.get_type::<Self>().getattr("_safe_constructor")?;
46                let state = self.__getstate__(py)?;
47                (safe_constructor, PyTuple::empty(py), state).into_py_any(py)
48            }
49
50            #[gen_stub(skip)]
51            #[staticmethod]
52            #[pyo3(name = "_safe_constructor")]
53            fn py_safe_constructor() -> PyResult<Self> {
54                <$ty>::new_checked("NULL-NULL").map_err(to_pyvalue_err)
55            }
56
57            // Note: Cannot use into_py_any_unwrap from IntoPyObjectNautilusExt
58            // because type resolution for the trait happens after macros have
59            // been run.
60            fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
61                use nautilus_core::python::IntoPyObjectNautilusExt;
62
63                match op {
64                    CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
65                    CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
66                    CompareOp::Ge => self.ge(other).into_py_any_unwrap(py),
67                    CompareOp::Gt => self.gt(other).into_py_any_unwrap(py),
68                    CompareOp::Le => self.le(other).into_py_any_unwrap(py),
69                    CompareOp::Lt => self.lt(other).into_py_any_unwrap(py),
70                }
71            }
72
73            fn __hash__(&self) -> isize {
74                self.inner().precomputed_hash() as isize
75            }
76
77            fn __repr__(&self) -> String {
78                format!(
79                    "{}('{}')",
80                    stringify!($ty).split("::").last().unwrap_or(""),
81                    self.as_str()
82                )
83            }
84
85            fn __str__(&self) -> &'static str {
86                self.inner().as_str()
87            }
88
89            #[getter]
90            #[pyo3(name = "value")]
91            fn py_value(&self) -> String {
92                self.to_string()
93            }
94
95            #[staticmethod]
96            #[pyo3(name = "from_str")]
97            fn py_from_str(value: &str) -> PyResult<Self> {
98                <$ty>::new_checked(value).map_err(to_pyvalue_err)
99            }
100        }
101    };
102}