Skip to main content

nautilus_databento/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
16use std::str::FromStr;
17
18use nautilus_core::python::to_pyvalue_err;
19use pyo3::{PyTypeInfo, prelude::*, types::PyType};
20
21use crate::enums::{DatabentoStatisticType, DatabentoStatisticUpdateAction};
22
23#[pymethods]
24impl DatabentoStatisticType {
25    #[new]
26    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
27        let t = Self::type_object(py);
28        Self::py_from_str(&t, value).map_err(to_pyvalue_err)
29    }
30
31    const fn __hash__(&self) -> isize {
32        *self as isize
33    }
34
35    fn __repr__(&self) -> String {
36        format!(
37            "<{}.{}: '{}'>",
38            stringify!(DatabentoStatisticType),
39            self.name(),
40            self.value(),
41        )
42    }
43
44    fn __str__(&self) -> String {
45        self.to_string()
46    }
47
48    #[getter]
49    #[must_use]
50    pub fn name(&self) -> String {
51        self.to_string()
52    }
53
54    #[getter]
55    #[must_use]
56    pub const fn value(&self) -> u8 {
57        *self as u8
58    }
59
60    // #[classmethod]
61    // fn variants(_: &PyType, py: Python<'_>) -> EnumIterator {
62    //     EnumIterator::new::<Self>(py)
63    // }
64
65    #[classmethod]
66    #[pyo3(name = "from_str")]
67    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
68        let data_str: &str = data.extract()?;
69        let tokenized = data_str.to_uppercase();
70        Self::from_str(&tokenized).map_err(to_pyvalue_err)
71    }
72}
73
74#[pymethods]
75impl DatabentoStatisticUpdateAction {
76    #[new]
77    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
78        let t = Self::type_object(py);
79        Self::py_from_str(&t, value).map_err(to_pyvalue_err)
80    }
81
82    const fn __hash__(&self) -> isize {
83        *self as isize
84    }
85
86    fn __repr__(&self) -> String {
87        format!(
88            "<{}.{}: '{}'>",
89            stringify!(DatabentoStatisticUpdateAction),
90            self.name(),
91            self.value(),
92        )
93    }
94
95    fn __str__(&self) -> String {
96        self.to_string()
97    }
98
99    #[getter]
100    #[must_use]
101    pub fn name(&self) -> String {
102        self.to_string()
103    }
104
105    #[getter]
106    #[must_use]
107    pub const fn value(&self) -> u8 {
108        *self as u8
109    }
110
111    // #[classmethod]
112    // fn variants(_: &PyType, py: Python<'_>) -> EnumIterator {
113    //     EnumIterator::new::<Self>(py)
114    // }
115
116    #[classmethod]
117    #[pyo3(name = "from_str")]
118    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
119        let data_str: &str = data.extract()?;
120        let tokenized = data_str.to_uppercase();
121        Self::from_str(&tokenized).map_err(to_pyvalue_err)
122    }
123}