nautilus_model/python/identifiers/
symbol.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
16use std::{
17    collections::hash_map::DefaultHasher,
18    hash::{Hash, Hasher},
19};
20
21use nautilus_core::python::{IntoPyObjectNautilusExt, to_pyvalue_err};
22use pyo3::{
23    IntoPyObjectExt,
24    prelude::*,
25    pyclass::CompareOp,
26    types::{PyString, PyTuple},
27};
28
29use crate::identifiers::symbol::Symbol;
30
31#[pymethods]
32impl Symbol {
33    #[new]
34    fn py_new(value: &str) -> PyResult<Self> {
35        Self::new_checked(value).map_err(to_pyvalue_err)
36    }
37
38    #[staticmethod]
39    fn _safe_constructor() -> Self {
40        Self::from("NULL")
41    }
42
43    fn __setstate__(&mut self, state: &Bound<'_, PyAny>) -> PyResult<()> {
44        let py_tuple: &Bound<'_, PyTuple> = state.downcast::<PyTuple>()?;
45        let binding = py_tuple.get_item(0)?;
46        let value = binding.downcast::<PyString>()?.extract::<&str>()?;
47        self.set_inner(value);
48        Ok(())
49    }
50
51    fn __getstate__(&self, py: Python) -> PyResult<PyObject> {
52        (self.to_string(),).into_py_any(py)
53    }
54
55    fn __reduce__(&self, py: Python) -> PyResult<PyObject> {
56        let safe_constructor = py.get_type::<Self>().getattr("_safe_constructor")?;
57        let state = self.__getstate__(py)?;
58        (safe_constructor, PyTuple::empty(py), state).into_py_any(py)
59    }
60
61    fn __richcmp__(&self, other: PyObject, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
62        if let Ok(other) = other.extract::<Self>(py) {
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        } else {
72            py.NotImplemented()
73        }
74    }
75
76    fn __hash__(&self) -> isize {
77        let mut h = DefaultHasher::new();
78        self.hash(&mut h);
79        h.finish() as isize
80    }
81
82    fn __repr__(&self) -> String {
83        format!("{}('{}')", stringify!(Symbol), self)
84    }
85
86    fn __str__(&self) -> String {
87        self.to_string()
88    }
89
90    #[staticmethod]
91    #[pyo3(name = "from_str")]
92    fn py_from_str(value: &str) -> PyResult<Self> {
93        Self::new_checked(value).map_err(to_pyvalue_err)
94    }
95
96    #[getter]
97    #[pyo3(name = "value")]
98    fn py_value(&self) -> String {
99        self.to_string()
100    }
101
102    #[getter]
103    #[pyo3(name = "is_composite")]
104    fn py_is_composite(&self) -> bool {
105        self.is_composite()
106    }
107
108    #[getter]
109    #[pyo3(name = "root")]
110    fn py_root(&self) -> &str {
111        self.root()
112    }
113
114    #[getter]
115    #[pyo3(name = "topic")]
116    fn py_topic(&self) -> String {
117        self.topic()
118    }
119}