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    #[pyo3(name = "from_str")]
64    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
65        let data_str: &str = data.extract()?;
66        let tokenized = data_str.to_uppercase();
67        Self::from_str(&tokenized).map_err(to_pyvalue_err)
68    }
69}
70
71#[pymethods]
72#[pyo3_stub_gen::derive::gen_stub_pymethods]
73impl DatabentoStatisticUpdateAction {
74    /// Represents a Databento statistic update action.
75    #[new]
76    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
77        let t = Self::type_object(py);
78        Self::py_from_str(&t, value).map_err(to_pyvalue_err)
79    }
80
81    const fn __hash__(&self) -> isize {
82        *self as isize
83    }
84
85    fn __repr__(&self) -> String {
86        format!(
87            "<{}.{}: '{}'>",
88            stringify!(DatabentoStatisticUpdateAction),
89            self.name(),
90            self.value(),
91        )
92    }
93
94    fn __str__(&self) -> String {
95        self.to_string()
96    }
97
98    #[getter]
99    #[must_use]
100    pub fn name(&self) -> String {
101        self.to_string()
102    }
103
104    #[getter]
105    #[must_use]
106    pub const fn value(&self) -> u8 {
107        *self as u8
108    }
109
110    #[classmethod]
111    #[pyo3(name = "from_str")]
112    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
113        let data_str: &str = data.extract()?;
114        let tokenized = data_str.to_uppercase();
115        Self::from_str(&tokenized).map_err(to_pyvalue_err)
116    }
117}