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