Skip to main content

nautilus_deribit/python/
enums.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Deribit enumerations Python bindings.
17
18use 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::{DeribitCurrency, DeribitEnvironment, DeribitProductType};
25
26#[pymethods]
27#[pyo3_stub_gen::derive::gen_stub_pymethods]
28impl DeribitCurrency {
29    /// Deribit currency.
30    #[new]
31    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
32        let t = Self::type_object(py);
33        Self::py_from_str(&t, value)
34    }
35
36    fn __hash__(&self) -> isize {
37        *self as isize
38    }
39
40    fn __repr__(&self) -> String {
41        format!(
42            "<{}.{}: '{}'>",
43            stringify!(DeribitCurrency),
44            self.name(),
45            self.value(),
46        )
47    }
48
49    fn __str__(&self) -> String {
50        self.to_string()
51    }
52
53    #[getter]
54    #[must_use]
55    pub fn name(&self) -> &str {
56        self.as_ref()
57    }
58
59    #[getter]
60    #[must_use]
61    pub fn value(&self) -> u8 {
62        *self as u8
63    }
64
65    #[staticmethod]
66    #[must_use]
67    fn variants() -> Vec<String> {
68        Self::iter().map(|x| x.to_string()).collect()
69    }
70
71    #[classmethod]
72    #[pyo3(name = "from_str")]
73    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
74        let data_str: String = data.str()?.extract()?;
75        Self::from_str(&data_str).map_err(to_pyvalue_err)
76    }
77}
78
79#[pymethods]
80#[pyo3_stub_gen::derive::gen_stub_pymethods]
81impl DeribitProductType {
82    /// Deribit product type.
83    #[new]
84    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
85        let t = Self::type_object(py);
86        Self::py_from_str(&t, value)
87    }
88
89    fn __hash__(&self) -> isize {
90        *self as isize
91    }
92
93    fn __repr__(&self) -> String {
94        format!(
95            "<{}.{}: '{}'>",
96            stringify!(DeribitProductType),
97            self.name(),
98            self.value(),
99        )
100    }
101
102    fn __str__(&self) -> String {
103        self.to_string()
104    }
105
106    #[getter]
107    #[must_use]
108    pub fn name(&self) -> &str {
109        self.as_ref()
110    }
111
112    #[getter]
113    #[must_use]
114    pub fn value(&self) -> u8 {
115        *self as u8
116    }
117
118    #[staticmethod]
119    #[must_use]
120    fn variants() -> Vec<String> {
121        Self::iter().map(|x| x.to_string()).collect()
122    }
123
124    #[classmethod]
125    #[pyo3(name = "from_str")]
126    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
127        let data_str: String = data.str()?.extract()?;
128        Self::from_str(&data_str).map_err(to_pyvalue_err)
129    }
130}
131
132#[pymethods]
133#[pyo3_stub_gen::derive::gen_stub_pymethods]
134impl DeribitEnvironment {
135    /// Deribit API environment.
136    #[new]
137    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
138        let t = Self::type_object(py);
139        Self::py_from_str(&t, value)
140    }
141
142    const fn __hash__(&self) -> isize {
143        *self as isize
144    }
145
146    fn __str__(&self) -> String {
147        self.to_string()
148    }
149
150    #[getter]
151    #[must_use]
152    pub fn name(&self) -> String {
153        self.to_string()
154    }
155
156    #[getter]
157    #[must_use]
158    pub fn value(&self) -> u8 {
159        *self as u8
160    }
161
162    #[classmethod]
163    #[must_use]
164    fn variants(_: &Bound<'_, PyType>) -> Vec<String> {
165        Self::iter().map(|x| x.to_string()).collect()
166    }
167
168    #[classmethod]
169    #[pyo3(name = "from_str")]
170    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
171        let data_str: String = data.str()?.extract()?;
172        Self::from_str(&data_str).map_err(to_pyvalue_err)
173    }
174}