nautilus_model/python/
macros.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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        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                Ok((self.to_string(),).to_object(py))
38            }
39
40            fn __reduce__(&self, py: Python) -> PyResult<PyObject> {
41                let safe_constructor = py.get_type::<Self>().getattr("_safe_constructor")?;
42                let state = self.__getstate__(py)?;
43                Ok((safe_constructor, PyTuple::empty(py), state).to_object(py))
44            }
45
46            #[staticmethod]
47            fn _safe_constructor() -> PyResult<Self> {
48                Ok(<$ty>::from("NULL")) // Safe default
49            }
50
51            fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
52                match op {
53                    CompareOp::Eq => self.eq(other).into_py(py),
54                    CompareOp::Ne => self.ne(other).into_py(py),
55                    CompareOp::Ge => self.ge(other).into_py(py),
56                    CompareOp::Gt => self.gt(other).into_py(py),
57                    CompareOp::Le => self.le(other).into_py(py),
58                    CompareOp::Lt => self.lt(other).into_py(py),
59                }
60            }
61
62            fn __hash__(&self) -> isize {
63                self.inner().precomputed_hash() as isize
64            }
65
66            fn __repr__(&self) -> String {
67                format!(
68                    "{}('{}')",
69                    stringify!($ty).split("::").last().unwrap_or(""),
70                    self.as_str()
71                )
72            }
73
74            fn __str__(&self) -> &'static str {
75                self.inner().as_str()
76            }
77
78            #[getter]
79            #[pyo3(name = "value")]
80            fn py_value(&self) -> String {
81                self.to_string()
82            }
83
84            #[staticmethod]
85            #[pyo3(name = "from_str")]
86            fn py_from_str(value: &str) -> Self {
87                Self::from(value)
88            }
89        }
90    };
91}