Skip to main content

nautilus_common/python/
enums.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
16use std::str::FromStr;
17
18use nautilus_core::python::to_pyvalue_err;
19use nautilus_model::python::common::EnumIterator;
20use pyo3::{PyTypeInfo, prelude::*, types::PyType};
21
22use crate::enums::{
23    ComponentState, ComponentTrigger, Environment, LogColor, LogFormat, LogLevel,
24    SerializationEncoding,
25};
26
27#[pymethods]
28impl Environment {
29    #[new]
30    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
31        let t = Self::type_object(py);
32        Self::py_from_str(&t, value)
33    }
34
35    const fn __hash__(&self) -> isize {
36        *self as isize
37    }
38
39    fn __str__(&self) -> String {
40        self.to_string()
41    }
42
43    #[getter]
44    #[must_use]
45    pub fn name(&self) -> String {
46        self.to_string()
47    }
48
49    #[getter]
50    #[must_use]
51    pub const fn value(&self) -> u8 {
52        *self as u8
53    }
54
55    #[classmethod]
56    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
57        EnumIterator::new::<Self>(py)
58    }
59
60    #[classmethod]
61    #[pyo3(name = "from_str")]
62    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
63        let data_str: &str = data.extract()?;
64        let tokenized = data_str.to_uppercase();
65        Self::from_str(&tokenized).map_err(to_pyvalue_err)
66    }
67}
68
69#[pymethods]
70impl LogLevel {
71    #[new]
72    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
73        let t = Self::type_object(py);
74        Self::py_from_str(&t, value)
75    }
76
77    const fn __hash__(&self) -> isize {
78        *self as isize
79    }
80
81    fn __str__(&self) -> String {
82        self.to_string()
83    }
84
85    #[getter]
86    #[must_use]
87    pub fn name(&self) -> String {
88        self.to_string()
89    }
90
91    #[getter]
92    #[must_use]
93    pub const fn value(&self) -> u8 {
94        *self as u8
95    }
96
97    #[classmethod]
98    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
99        EnumIterator::new::<Self>(py)
100    }
101
102    #[classmethod]
103    #[pyo3(name = "from_str")]
104    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
105        let data_str: &str = data.extract()?;
106        let tokenized = data_str.to_uppercase();
107        Self::from_str(&tokenized).map_err(to_pyvalue_err)
108    }
109}
110
111#[pymethods]
112impl LogColor {
113    #[new]
114    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
115        let t = Self::type_object(py);
116        Self::py_from_str(&t, value)
117    }
118
119    const fn __hash__(&self) -> isize {
120        *self as isize
121    }
122
123    fn __str__(&self) -> String {
124        self.to_string()
125    }
126
127    #[getter]
128    #[must_use]
129    pub fn name(&self) -> String {
130        self.to_string()
131    }
132
133    #[getter]
134    #[must_use]
135    pub const fn value(&self) -> u8 {
136        *self as u8
137    }
138
139    #[classmethod]
140    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
141        EnumIterator::new::<Self>(py)
142    }
143
144    #[classmethod]
145    #[pyo3(name = "from_str")]
146    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
147        let data_str: &str = data.extract()?;
148        let tokenized = data_str.to_uppercase();
149        Self::from_str(&tokenized).map_err(to_pyvalue_err)
150    }
151}
152
153#[pymethods]
154impl ComponentState {
155    const fn __hash__(&self) -> isize {
156        *self as isize
157    }
158}
159
160#[pymethods]
161impl ComponentTrigger {
162    const fn __hash__(&self) -> isize {
163        *self as isize
164    }
165}
166
167#[pymethods]
168impl LogFormat {
169    const fn __hash__(&self) -> isize {
170        *self as isize
171    }
172}
173
174#[pymethods]
175impl SerializationEncoding {
176    const fn __hash__(&self) -> isize {
177        *self as isize
178    }
179}