nautilus_common/python/
enums.rs1use std::str::FromStr;
17
18use nautilus_core::python::to_pyvalue_err;
19use nautilus_model::python::common::EnumIterator;
20use pyo3::{prelude::*, types::PyType, PyTypeInfo};
21
22use crate::enums::{LogColor, LogLevel};
23
24#[pymethods]
25impl LogLevel {
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!(LogLevel),
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 = "OFF")]
76 const fn py_off() -> Self {
77 Self::Off
78 }
79
80 #[classattr]
81 #[pyo3(name = "DEBUG")]
82 const fn py_debug() -> Self {
83 Self::Debug
84 }
85
86 #[classattr]
87 #[pyo3(name = "INFO")]
88 const fn py_info() -> Self {
89 Self::Info
90 }
91
92 #[classattr]
93 #[pyo3(name = "WARNING")]
94 const fn py_warning() -> Self {
95 Self::Warning
96 }
97
98 #[classattr]
99 #[pyo3(name = "ERROR")]
100 const fn py_error() -> Self {
101 Self::Error
102 }
103}
104
105#[pymethods]
106impl LogColor {
107 #[new]
108 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
109 let t = Self::type_object(py);
110 Self::py_from_str(&t, value)
111 }
112
113 const fn __hash__(&self) -> isize {
114 *self as isize
115 }
116
117 fn __repr__(&self) -> String {
118 format!(
119 "<{}.{}: '{}'>",
120 stringify!(LogColor),
121 self.name(),
122 self.value(),
123 )
124 }
125
126 fn __str__(&self) -> String {
127 self.to_string()
128 }
129
130 #[getter]
131 #[must_use]
132 pub fn name(&self) -> String {
133 self.to_string()
134 }
135
136 #[getter]
137 #[must_use]
138 pub const fn value(&self) -> u8 {
139 *self as u8
140 }
141
142 #[classmethod]
143 fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
144 EnumIterator::new::<Self>(py)
145 }
146
147 #[classmethod]
148 #[pyo3(name = "from_str")]
149 fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
150 let data_str: &str = data.extract()?;
151 let tokenized = data_str.to_uppercase();
152 Self::from_str(&tokenized).map_err(to_pyvalue_err)
153 }
154
155 #[classattr]
156 #[pyo3(name = "NORMAL")]
157 const fn py_normal() -> Self {
158 Self::Normal
159 }
160
161 #[classattr]
162 #[pyo3(name = "GREEN")]
163 const fn py_green() -> Self {
164 Self::Green
165 }
166
167 #[classattr]
168 #[pyo3(name = "BLUE")]
169 const fn py_blue() -> Self {
170 Self::Blue
171 }
172
173 #[classattr]
174 #[pyo3(name = "MAGENTA")]
175 const fn py_magenta() -> Self {
176 Self::Magenta
177 }
178
179 #[classattr]
180 #[pyo3(name = "CYAN")]
181 const fn py_cyan() -> Self {
182 Self::Cyan
183 }
184
185 #[classattr]
186 #[pyo3(name = "YELLOW")]
187 const fn py_error() -> Self {
188 Self::Yellow
189 }
190
191 #[classattr]
192 #[pyo3(name = "RED")]
193 const fn py_red() -> Self {
194 Self::Red
195 }
196}