1use 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, OKXEnvironment, OKXInstrumentType, OKXMarginMode, OKXOrderStatus,
26 OKXPositionMode, OKXTradeMode, OKXVipLevel,
27};
28
29#[pymethods]
30#[pyo3_stub_gen::derive::gen_stub_pymethods]
31impl OKXInstrumentType {
32 #[new]
34 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
35 let t = Self::type_object(py);
36 Self::py_from_str(&t, value)
37 }
38
39 fn __hash__(&self) -> isize {
40 *self as isize
41 }
42
43 fn __repr__(&self) -> String {
44 format!(
45 "<{}.{}: '{}'>",
46 stringify!(OKXInstrumentType),
47 self.name(),
48 self.value(),
49 )
50 }
51
52 fn __str__(&self) -> String {
53 self.to_string()
54 }
55
56 #[getter]
57 #[must_use]
58 pub fn name(&self) -> &str {
59 self.as_ref()
60 }
61
62 #[getter]
63 #[must_use]
64 pub fn value(&self) -> u8 {
65 *self as u8
66 }
67
68 #[staticmethod]
69 #[must_use]
70 fn variants() -> Vec<String> {
71 Self::iter().map(|x| x.to_string()).collect()
72 }
73
74 #[classmethod]
75 #[pyo3(name = "from_str")]
76 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
77 let data_str: String = data.str()?.extract()?;
78 Self::from_str(&data_str).map_err(to_pyvalue_err)
79 }
80}
81
82#[pymethods]
83#[pyo3_stub_gen::derive::gen_stub_pymethods]
84impl OKXContractType {
85 #[new]
87 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
88 let t = Self::type_object(py);
89 Self::py_from_str(&t, value)
90 }
91
92 fn __hash__(&self) -> isize {
93 *self as isize
94 }
95
96 fn __repr__(&self) -> String {
97 format!(
98 "<{}.{}: '{}'>",
99 stringify!(OKXContractType),
100 self.name(),
101 self.value(),
102 )
103 }
104
105 fn __str__(&self) -> String {
106 self.to_string()
107 }
108
109 #[getter]
110 #[must_use]
111 pub fn name(&self) -> &str {
112 self.as_ref()
113 }
114
115 #[getter]
116 #[must_use]
117 pub fn value(&self) -> u8 {
118 *self as u8
119 }
120
121 #[classmethod]
122 #[pyo3(name = "from_str")]
123 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
124 let data_str: String = data.str()?.extract()?;
125 Self::from_str(&data_str).map_err(to_pyvalue_err)
126 }
127
128 #[staticmethod]
129 #[must_use]
130 fn variants() -> Vec<String> {
131 Self::iter().map(|x| x.to_string()).collect()
132 }
133}
134
135#[pymethods]
136#[pyo3_stub_gen::derive::gen_stub_pymethods]
137impl OKXMarginMode {
138 #[new]
146 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
147 let t = Self::type_object(py);
148 Self::py_from_str(&t, value)
149 }
150
151 fn __hash__(&self) -> isize {
152 *self as isize
153 }
154
155 fn __repr__(&self) -> String {
156 format!(
157 "<{}.{}: '{}'>",
158 stringify!(OKXMarginMode),
159 self.name(),
160 self.value(),
161 )
162 }
163
164 fn __str__(&self) -> String {
165 self.to_string()
166 }
167
168 #[getter]
169 #[must_use]
170 pub fn name(&self) -> &str {
171 self.as_ref()
172 }
173
174 #[getter]
175 #[must_use]
176 pub fn value(&self) -> u8 {
177 *self as u8
178 }
179
180 #[staticmethod]
181 #[must_use]
182 fn variants() -> Vec<String> {
183 Self::iter().map(|x| x.to_string()).collect()
184 }
185
186 #[classmethod]
187 #[pyo3(name = "from_str")]
188 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
189 let data_str: String = data.str()?.extract()?;
190 Self::from_str(&data_str).map_err(to_pyvalue_err)
191 }
192}
193
194#[pymethods]
195#[pyo3_stub_gen::derive::gen_stub_pymethods]
196impl OKXTradeMode {
197 #[new]
199 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
200 let t = Self::type_object(py);
201 Self::py_from_str(&t, value)
202 }
203
204 fn __hash__(&self) -> isize {
205 *self as isize
206 }
207
208 fn __repr__(&self) -> String {
209 format!(
210 "<{}.{}: '{}'>",
211 stringify!(OKXTradeMode),
212 self.name(),
213 self.value(),
214 )
215 }
216
217 fn __str__(&self) -> String {
218 self.to_string()
219 }
220
221 #[getter]
222 #[must_use]
223 pub fn name(&self) -> &str {
224 self.as_ref()
225 }
226
227 #[getter]
228 #[must_use]
229 pub fn value(&self) -> u8 {
230 *self as u8
231 }
232
233 #[staticmethod]
234 #[must_use]
235 fn variants() -> Vec<String> {
236 Self::iter().map(|x| x.to_string()).collect()
237 }
238
239 #[classmethod]
240 #[pyo3(name = "from_str")]
241 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
242 let data_str: String = data.str()?.extract()?;
243 Self::from_str(&data_str).map_err(to_pyvalue_err)
244 }
245}
246
247#[pymethods]
248#[pyo3_stub_gen::derive::gen_stub_pymethods]
249impl OKXPositionMode {
250 #[new]
256 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
257 let t = Self::type_object(py);
258 Self::py_from_str(&t, value)
259 }
260
261 fn __hash__(&self) -> isize {
262 *self as isize
263 }
264
265 fn __repr__(&self) -> String {
266 format!(
267 "<{}.{}: '{}'>",
268 stringify!(OKXPositionMode),
269 self.name(),
270 self.value(),
271 )
272 }
273
274 fn __str__(&self) -> String {
275 self.to_string()
276 }
277
278 #[getter]
279 #[must_use]
280 pub fn name(&self) -> &str {
281 self.as_ref()
282 }
283
284 #[getter]
285 #[must_use]
286 pub fn value(&self) -> u8 {
287 *self as u8
288 }
289
290 #[staticmethod]
291 #[must_use]
292 fn variants() -> Vec<String> {
293 Self::iter().map(|x| x.to_string()).collect()
294 }
295
296 #[classmethod]
297 #[pyo3(name = "from_str")]
298 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
299 let data_str: String = data.str()?.extract()?;
300 Self::from_str(&data_str).map_err(to_pyvalue_err)
301 }
302}
303
304#[pymethods]
305#[pyo3_stub_gen::derive::gen_stub_pymethods]
306impl OKXOrderStatus {
307 #[new]
309 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
310 let t = Self::type_object(py);
311 Self::py_from_str(&t, value)
312 }
313
314 fn __hash__(&self) -> isize {
315 *self as isize
316 }
317
318 fn __repr__(&self) -> String {
319 format!(
320 "<{}.{}: '{}'>",
321 stringify!(OKXOrderStatus),
322 self.name(),
323 self.value(),
324 )
325 }
326
327 fn __str__(&self) -> String {
328 self.to_string()
329 }
330
331 #[getter]
332 #[must_use]
333 pub fn name(&self) -> &str {
334 self.as_ref()
335 }
336
337 #[getter]
338 #[must_use]
339 pub fn value(&self) -> u8 {
340 *self as u8
341 }
342
343 #[staticmethod]
344 #[must_use]
345 fn variants() -> Vec<String> {
346 Self::iter().map(|x| x.to_string()).collect()
347 }
348
349 #[classmethod]
350 #[pyo3(name = "from_str")]
351 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
352 let data_str: String = data.str()?.extract()?;
353 Self::from_str(&data_str).map_err(to_pyvalue_err)
354 }
355}
356
357#[pymethods]
358#[pyo3_stub_gen::derive::gen_stub_pymethods]
359impl OKXVipLevel {
360 #[new]
374 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
375 let t = Self::type_object(py);
376 Self::py_from_str(&t, value)
377 }
378
379 fn __hash__(&self) -> isize {
380 *self as isize
381 }
382
383 fn __repr__(&self) -> String {
384 format!(
385 "<{}.{}: '{}'>",
386 stringify!(OKXVipLevel),
387 self.name(),
388 self.value(),
389 )
390 }
391
392 fn __str__(&self) -> String {
393 self.to_string()
394 }
395
396 #[getter]
397 #[must_use]
398 pub fn name(&self) -> &str {
399 self.as_ref()
400 }
401
402 #[getter]
403 #[must_use]
404 pub fn value(&self) -> u8 {
405 *self as u8
406 }
407
408 #[staticmethod]
409 #[must_use]
410 fn variants() -> Vec<String> {
411 Self::iter().map(|x| x.to_string()).collect()
412 }
413
414 #[classmethod]
415 #[pyo3(name = "from_str")]
416 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
417 let data_str: String = data.str()?.extract()?;
418 Self::from_str(&data_str).map_err(to_pyvalue_err)
419 }
420}
421
422#[pymethods]
423#[pyo3_stub_gen::derive::gen_stub_pymethods]
424impl OKXEnvironment {
425 #[new]
427 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
428 let t = Self::type_object(py);
429 Self::py_from_str(&t, value)
430 }
431
432 const fn __hash__(&self) -> isize {
433 *self as isize
434 }
435
436 fn __str__(&self) -> String {
437 self.to_string()
438 }
439
440 #[getter]
441 #[must_use]
442 pub fn name(&self) -> String {
443 self.to_string()
444 }
445
446 #[getter]
447 #[must_use]
448 pub fn value(&self) -> u8 {
449 *self as u8
450 }
451
452 #[classmethod]
453 #[must_use]
454 fn variants(_: &Bound<'_, PyType>) -> Vec<String> {
455 Self::iter().map(|x| x.to_string()).collect()
456 }
457
458 #[classmethod]
459 #[pyo3(name = "from_str")]
460 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
461 let data_str: String = data.str()?.extract()?;
462 Self::from_str(&data_str).map_err(to_pyvalue_err)
463 }
464}