Skip to main content

nautilus_bitmex/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//! BitMEX 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::{BitmexEnvironment, BitmexSymbolStatus};
25
26#[pymethods]
27#[pyo3_stub_gen::derive::gen_stub_pymethods]
28impl BitmexSymbolStatus {
29    /// Represents the status of a BitMEX symbol.
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    const fn __hash__(&self) -> isize {
37        *self as isize
38    }
39
40    fn __repr__(&self) -> String {
41        format!(
42            "<{}.{}: '{}'>",
43            stringify!(BitmexSymbolStatus),
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 const 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    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    /// BitMEX API environment.
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    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}