nautilus_bitmex/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::{BitmexEnvironment, BitmexSymbolStatus};
25
26#[pymethods]
27impl BitmexSymbolStatus {
28 #[new]
30 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
31 let t = Self::type_object(py);
32 Self::py_from_str(&t, value)
33 }
34
35 const fn __hash__(&self) -> isize {
36 *self as isize
37 }
38
39 fn __repr__(&self) -> String {
40 format!(
41 "<{}.{}: '{}'>",
42 stringify!(BitmexSymbolStatus),
43 self.name(),
44 self.value(),
45 )
46 }
47
48 fn __str__(&self) -> String {
49 self.to_string()
50 }
51
52 #[getter]
53 #[must_use]
54 pub fn name(&self) -> &str {
55 self.as_ref()
56 }
57
58 #[getter]
59 #[must_use]
60 pub const fn value(&self) -> u8 {
61 *self as u8
62 }
63
64 #[staticmethod]
65 #[must_use]
66 fn variants() -> Vec<String> {
67 Self::iter().map(|x| x.to_string()).collect()
68 }
69
70 #[classmethod]
71 #[pyo3(name = "from_str")]
72 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
73 let data_str: String = data.str()?.extract()?;
74 Self::from_str(&data_str).map_err(to_pyvalue_err)
75 }
76}
77
78#[pymethods]
79#[pyo3_stub_gen::derive::gen_stub_pymethods]
80impl BitmexEnvironment {
81 #[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 const fn __hash__(&self) -> isize {
89 *self as isize
90 }
91
92 fn __str__(&self) -> String {
93 self.to_string()
94 }
95
96 #[getter]
97 #[must_use]
98 pub fn name(&self) -> String {
99 self.to_string()
100 }
101
102 #[getter]
103 #[must_use]
104 pub fn value(&self) -> u8 {
105 *self as u8
106 }
107
108 #[classmethod]
109 #[must_use]
110 fn variants(_: &Bound<'_, PyType>) -> Vec<String> {
111 Self::iter().map(|x| x.to_string()).collect()
112 }
113
114 #[classmethod]
115 #[pyo3(name = "from_str")]
116 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
117 let data_str: String = data.str()?.extract()?;
118 Self::from_str(&data_str).map_err(to_pyvalue_err)
119 }
120}