nautilus_common/python/
enums.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::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    #[classattr]
75    #[pyo3(name = "BACKTEST")]
76    const fn py_backtest() -> Self {
77        Self::Backtest
78    }
79
80    #[classattr]
81    #[pyo3(name = "SANDBOX")]
82    const fn py_sandbox() -> Self {
83        Self::Sandbox
84    }
85
86    #[classattr]
87    #[pyo3(name = "LIVE")]
88    const fn py_live() -> Self {
89        Self::Live
90    }
91}
92
93#[pymethods]
94impl LogLevel {
95    #[new]
96    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
97        let t = Self::type_object(py);
98        Self::py_from_str(&t, value)
99    }
100
101    const fn __hash__(&self) -> isize {
102        *self as isize
103    }
104
105    fn __repr__(&self) -> String {
106        format!(
107            "<{}.{}: '{}'>",
108            stringify!(LogLevel),
109            self.name(),
110            self.value(),
111        )
112    }
113
114    fn __str__(&self) -> String {
115        self.to_string()
116    }
117
118    #[getter]
119    #[must_use]
120    pub fn name(&self) -> String {
121        self.to_string()
122    }
123
124    #[getter]
125    #[must_use]
126    pub const fn value(&self) -> u8 {
127        *self as u8
128    }
129
130    #[classmethod]
131    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
132        EnumIterator::new::<Self>(py)
133    }
134
135    #[classmethod]
136    #[pyo3(name = "from_str")]
137    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
138        let data_str: &str = data.extract()?;
139        let tokenized = data_str.to_uppercase();
140        Self::from_str(&tokenized).map_err(to_pyvalue_err)
141    }
142
143    #[classattr]
144    #[pyo3(name = "OFF")]
145    const fn py_off() -> Self {
146        Self::Off
147    }
148
149    #[classattr]
150    #[pyo3(name = "DEBUG")]
151    const fn py_debug() -> Self {
152        Self::Debug
153    }
154
155    #[classattr]
156    #[pyo3(name = "INFO")]
157    const fn py_info() -> Self {
158        Self::Info
159    }
160
161    #[classattr]
162    #[pyo3(name = "WARNING")]
163    const fn py_warning() -> Self {
164        Self::Warning
165    }
166
167    #[classattr]
168    #[pyo3(name = "ERROR")]
169    const fn py_error() -> Self {
170        Self::Error
171    }
172}
173
174#[pymethods]
175impl LogColor {
176    #[new]
177    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
178        let t = Self::type_object(py);
179        Self::py_from_str(&t, value)
180    }
181
182    const fn __hash__(&self) -> isize {
183        *self as isize
184    }
185
186    fn __repr__(&self) -> String {
187        format!(
188            "<{}.{}: '{}'>",
189            stringify!(LogColor),
190            self.name(),
191            self.value(),
192        )
193    }
194
195    fn __str__(&self) -> String {
196        self.to_string()
197    }
198
199    #[getter]
200    #[must_use]
201    pub fn name(&self) -> String {
202        self.to_string()
203    }
204
205    #[getter]
206    #[must_use]
207    pub const fn value(&self) -> u8 {
208        *self as u8
209    }
210
211    #[classmethod]
212    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
213        EnumIterator::new::<Self>(py)
214    }
215
216    #[classmethod]
217    #[pyo3(name = "from_str")]
218    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
219        let data_str: &str = data.extract()?;
220        let tokenized = data_str.to_uppercase();
221        Self::from_str(&tokenized).map_err(to_pyvalue_err)
222    }
223
224    #[classattr]
225    #[pyo3(name = "NORMAL")]
226    const fn py_normal() -> Self {
227        Self::Normal
228    }
229
230    #[classattr]
231    #[pyo3(name = "GREEN")]
232    const fn py_green() -> Self {
233        Self::Green
234    }
235
236    #[classattr]
237    #[pyo3(name = "BLUE")]
238    const fn py_blue() -> Self {
239        Self::Blue
240    }
241
242    #[classattr]
243    #[pyo3(name = "MAGENTA")]
244    const fn py_magenta() -> Self {
245        Self::Magenta
246    }
247
248    #[classattr]
249    #[pyo3(name = "CYAN")]
250    const fn py_cyan() -> Self {
251        Self::Cyan
252    }
253
254    #[classattr]
255    #[pyo3(name = "YELLOW")]
256    const fn py_error() -> Self {
257        Self::Yellow
258    }
259
260    #[classattr]
261    #[pyo3(name = "RED")]
262    const fn py_red() -> Self {
263        Self::Red
264    }
265}