nautilus_okx/python/
enums.rs1use std::str::FromStr;
19
20use nautilus_core::python::to_pyvalue_err;
21use pyo3::{PyTypeInfo, prelude::*, types::PyType};
22use strum::IntoEnumIterator;
23
24use crate::common::enums::{
25 OKXContractType, OKXInstrumentType, OKXMarginMode, OKXOrderStatus, OKXPositionMode,
26 OKXTradeMode, OKXVipLevel,
27};
28
29#[pymethods]
30impl OKXInstrumentType {
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 fn __hash__(&self) -> isize {
38 *self as isize
39 }
40
41 fn __repr__(&self) -> String {
42 format!(
43 "<{}.{}: '{}'>",
44 stringify!(OKXInstrumentType),
45 self.name(),
46 self.value(),
47 )
48 }
49
50 fn __str__(&self) -> String {
51 self.to_string()
52 }
53
54 #[getter]
55 #[must_use]
56 pub fn name(&self) -> &str {
57 self.as_ref()
58 }
59
60 #[getter]
61 #[must_use]
62 pub fn value(&self) -> u8 {
63 *self as u8
64 }
65
66 #[staticmethod]
67 #[must_use]
68 fn variants() -> Vec<String> {
69 Self::iter().map(|x| x.to_string()).collect()
70 }
71
72 #[classmethod]
73 #[pyo3(name = "from_str")]
74 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
75 let data_str: String = data.str()?.extract()?;
76 Self::from_str(&data_str).map_err(to_pyvalue_err)
77 }
78}
79
80#[pymethods]
81impl OKXContractType {
82 #[new]
83 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
84 let t = Self::type_object(py);
85 Self::py_from_str(&t, value)
86 }
87
88 fn __hash__(&self) -> isize {
89 *self as isize
90 }
91
92 fn __repr__(&self) -> String {
93 format!(
94 "<{}.{}: '{}'>",
95 stringify!(OKXContractType),
96 self.name(),
97 self.value(),
98 )
99 }
100
101 fn __str__(&self) -> String {
102 self.to_string()
103 }
104
105 #[getter]
106 #[must_use]
107 pub fn name(&self) -> &str {
108 self.as_ref()
109 }
110
111 #[getter]
112 #[must_use]
113 pub fn value(&self) -> u8 {
114 *self as u8
115 }
116
117 #[classmethod]
118 #[pyo3(name = "from_str")]
119 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
120 let data_str: String = data.str()?.extract()?;
121 Self::from_str(&data_str).map_err(to_pyvalue_err)
122 }
123
124 #[staticmethod]
125 #[must_use]
126 fn variants() -> Vec<String> {
127 Self::iter().map(|x| x.to_string()).collect()
128 }
129}
130
131#[pymethods]
132impl OKXMarginMode {
133 #[new]
134 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
135 let t = Self::type_object(py);
136 Self::py_from_str(&t, value)
137 }
138
139 fn __hash__(&self) -> isize {
140 *self as isize
141 }
142
143 fn __repr__(&self) -> String {
144 format!(
145 "<{}.{}: '{}'>",
146 stringify!(OKXMarginMode),
147 self.name(),
148 self.value(),
149 )
150 }
151
152 fn __str__(&self) -> String {
153 self.to_string()
154 }
155
156 #[getter]
157 #[must_use]
158 pub fn name(&self) -> &str {
159 self.as_ref()
160 }
161
162 #[getter]
163 #[must_use]
164 pub fn value(&self) -> u8 {
165 *self as u8
166 }
167
168 #[staticmethod]
169 #[must_use]
170 fn variants() -> Vec<String> {
171 Self::iter().map(|x| x.to_string()).collect()
172 }
173
174 #[classmethod]
175 #[pyo3(name = "from_str")]
176 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
177 let data_str: String = data.str()?.extract()?;
178 Self::from_str(&data_str).map_err(to_pyvalue_err)
179 }
180}
181
182#[pymethods]
183impl OKXTradeMode {
184 #[new]
185 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
186 let t = Self::type_object(py);
187 Self::py_from_str(&t, value)
188 }
189
190 fn __hash__(&self) -> isize {
191 *self as isize
192 }
193
194 fn __repr__(&self) -> String {
195 format!(
196 "<{}.{}: '{}'>",
197 stringify!(OKXTradeMode),
198 self.name(),
199 self.value(),
200 )
201 }
202
203 fn __str__(&self) -> String {
204 self.to_string()
205 }
206
207 #[getter]
208 #[must_use]
209 pub fn name(&self) -> &str {
210 self.as_ref()
211 }
212
213 #[getter]
214 #[must_use]
215 pub fn value(&self) -> u8 {
216 *self as u8
217 }
218
219 #[staticmethod]
220 #[must_use]
221 fn variants() -> Vec<String> {
222 Self::iter().map(|x| x.to_string()).collect()
223 }
224
225 #[classmethod]
226 #[pyo3(name = "from_str")]
227 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
228 let data_str: String = data.str()?.extract()?;
229 Self::from_str(&data_str).map_err(to_pyvalue_err)
230 }
231}
232
233#[pymethods]
234impl OKXPositionMode {
235 #[new]
236 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
237 let t = Self::type_object(py);
238 Self::py_from_str(&t, value)
239 }
240
241 fn __hash__(&self) -> isize {
242 *self as isize
243 }
244
245 fn __repr__(&self) -> String {
246 format!(
247 "<{}.{}: '{}'>",
248 stringify!(OKXPositionMode),
249 self.name(),
250 self.value(),
251 )
252 }
253
254 fn __str__(&self) -> String {
255 self.to_string()
256 }
257
258 #[getter]
259 #[must_use]
260 pub fn name(&self) -> &str {
261 self.as_ref()
262 }
263
264 #[getter]
265 #[must_use]
266 pub fn value(&self) -> u8 {
267 *self as u8
268 }
269
270 #[staticmethod]
271 #[must_use]
272 fn variants() -> Vec<String> {
273 Self::iter().map(|x| x.to_string()).collect()
274 }
275
276 #[classmethod]
277 #[pyo3(name = "from_str")]
278 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
279 let data_str: String = data.str()?.extract()?;
280 Self::from_str(&data_str).map_err(to_pyvalue_err)
281 }
282}
283
284#[pymethods]
285impl OKXOrderStatus {
286 #[new]
287 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
288 let t = Self::type_object(py);
289 Self::py_from_str(&t, value)
290 }
291
292 fn __hash__(&self) -> isize {
293 *self as isize
294 }
295
296 fn __repr__(&self) -> String {
297 format!(
298 "<{}.{}: '{}'>",
299 stringify!(OKXOrderStatus),
300 self.name(),
301 self.value(),
302 )
303 }
304
305 fn __str__(&self) -> String {
306 self.to_string()
307 }
308
309 #[getter]
310 #[must_use]
311 pub fn name(&self) -> &str {
312 self.as_ref()
313 }
314
315 #[getter]
316 #[must_use]
317 pub fn value(&self) -> u8 {
318 *self as u8
319 }
320
321 #[staticmethod]
322 #[must_use]
323 fn variants() -> Vec<String> {
324 Self::iter().map(|x| x.to_string()).collect()
325 }
326
327 #[classmethod]
328 #[pyo3(name = "from_str")]
329 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
330 let data_str: String = data.str()?.extract()?;
331 Self::from_str(&data_str).map_err(to_pyvalue_err)
332 }
333}
334
335#[pymethods]
336impl OKXVipLevel {
337 #[new]
338 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
339 let t = Self::type_object(py);
340 Self::py_from_str(&t, value)
341 }
342
343 fn __hash__(&self) -> isize {
344 *self as isize
345 }
346
347 fn __repr__(&self) -> String {
348 format!(
349 "<{}.{}: '{}'>",
350 stringify!(OKXVipLevel),
351 self.name(),
352 self.value(),
353 )
354 }
355
356 fn __str__(&self) -> String {
357 self.to_string()
358 }
359
360 #[getter]
361 #[must_use]
362 pub fn name(&self) -> &str {
363 self.as_ref()
364 }
365
366 #[getter]
367 #[must_use]
368 pub fn value(&self) -> u8 {
369 *self as u8
370 }
371
372 #[staticmethod]
373 #[must_use]
374 fn variants() -> Vec<String> {
375 Self::iter().map(|x| x.to_string()).collect()
376 }
377
378 #[classmethod]
379 #[pyo3(name = "from_str")]
380 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
381 let data_str: String = data.str()?.extract()?;
382 Self::from_str(&data_str).map_err(to_pyvalue_err)
383 }
384}