nautilus_deribit/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::{
25 common::enums::{DeribitCurrency, DeribitEnvironment, DeribitProductType},
26 websocket::enums::DeribitUpdateInterval,
27};
28
29#[pymethods]
30#[pyo3_stub_gen::derive::gen_stub_pymethods]
31impl DeribitCurrency {
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!(DeribitCurrency),
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 DeribitProductType {
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!(DeribitProductType),
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 #[staticmethod]
122 #[must_use]
123 fn variants() -> Vec<String> {
124 Self::iter().map(|x| x.to_string()).collect()
125 }
126
127 #[classmethod]
128 #[pyo3(name = "from_str")]
129 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
130 let data_str: String = data.str()?.extract()?;
131 Self::from_str(&data_str).map_err(to_pyvalue_err)
132 }
133}
134
135#[pymethods]
136#[pyo3_stub_gen::derive::gen_stub_pymethods]
137impl DeribitUpdateInterval {
138 #[new]
143 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
144 let t = Self::type_object(py);
145 Self::py_from_str(&t, value)
146 }
147
148 fn __hash__(&self) -> isize {
149 *self as isize
150 }
151
152 fn __repr__(&self) -> String {
153 format!(
154 "<{}.{}: '{}'>",
155 stringify!(DeribitUpdateInterval),
156 self.name(),
157 self.value(),
158 )
159 }
160
161 fn __str__(&self) -> String {
162 self.to_string()
163 }
164
165 #[getter]
166 #[must_use]
167 pub fn name(&self) -> &str {
168 self.as_ref()
169 }
170
171 #[getter]
172 #[must_use]
173 pub fn value(&self) -> u8 {
174 *self as u8
175 }
176
177 #[staticmethod]
178 #[must_use]
179 fn variants() -> Vec<String> {
180 Self::iter().map(|x| x.to_string()).collect()
181 }
182
183 #[classmethod]
184 #[pyo3(name = "from_str")]
185 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
186 let data_str: String = data.str()?.extract()?;
187 Self::from_str(&data_str).map_err(to_pyvalue_err)
188 }
189}
190
191#[pymethods]
192#[pyo3_stub_gen::derive::gen_stub_pymethods]
193impl DeribitEnvironment {
194 #[new]
196 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
197 let t = Self::type_object(py);
198 Self::py_from_str(&t, value)
199 }
200
201 const fn __hash__(&self) -> isize {
202 *self as isize
203 }
204
205 fn __str__(&self) -> String {
206 self.to_string()
207 }
208
209 #[getter]
210 #[must_use]
211 pub fn name(&self) -> String {
212 self.to_string()
213 }
214
215 #[getter]
216 #[must_use]
217 pub fn value(&self) -> u8 {
218 *self as u8
219 }
220
221 #[classmethod]
222 #[must_use]
223 fn variants(_: &Bound<'_, PyType>) -> Vec<String> {
224 Self::iter().map(|x| x.to_string()).collect()
225 }
226
227 #[classmethod]
228 #[pyo3(name = "from_str")]
229 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
230 let data_str: String = data.str()?.extract()?;
231 Self::from_str(&data_str).map_err(to_pyvalue_err)
232 }
233}