nautilus_bitmex/python/
enums.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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::BitmexSymbolStatus;
25
26#[pymethods]
27impl BitmexSymbolStatus {
28    #[new]
29    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
30        let t = Self::type_object(py);
31        Self::py_from_str(&t, value)
32    }
33
34    const fn __hash__(&self) -> isize {
35        *self as isize
36    }
37
38    fn __repr__(&self) -> String {
39        format!(
40            "<{}.{}: '{}'>",
41            stringify!(BitmexSymbolStatus),
42            self.name(),
43            self.value(),
44        )
45    }
46
47    fn __str__(&self) -> String {
48        self.to_string()
49    }
50
51    #[getter]
52    #[must_use]
53    pub fn name(&self) -> &str {
54        self.as_ref()
55    }
56
57    #[getter]
58    #[must_use]
59    pub const fn value(&self) -> u8 {
60        *self as u8
61    }
62
63    #[staticmethod]
64    #[must_use]
65    fn variants() -> Vec<String> {
66        Self::iter().map(|x| x.to_string()).collect()
67    }
68
69    #[classmethod]
70    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
71        let data_str: String = data.str()?.extract()?;
72        Self::from_str(&data_str).map_err(to_pyvalue_err)
73    }
74
75    #[classattr]
76    #[pyo3(name = "OPEN")]
77    const fn py_open() -> Self {
78        Self::Open
79    }
80
81    #[classattr]
82    #[pyo3(name = "CLOSED")]
83    const fn py_closed() -> Self {
84        Self::Closed
85    }
86
87    #[classattr]
88    #[pyo3(name = "UNLISTED")]
89    const fn py_unlisted() -> Self {
90        Self::Unlisted
91    }
92}