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