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