nautilus_serialization/python/
enums.rs
1use std::str::FromStr;
17
18use nautilus_core::python::to_pyvalue_err;
19use nautilus_model::python::common::EnumIterator;
20use pyo3::{PyTypeInfo, prelude::*, types::PyType};
21
22use crate::enums::ParquetWriteMode;
23
24#[pymethods]
25impl ParquetWriteMode {
26 #[new]
27 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
28 let t = Self::type_object(py);
29 Self::py_from_str(&t, value)
30 }
31
32 fn __hash__(&self) -> isize {
33 *self as isize
34 }
35
36 fn __repr__(&self) -> String {
37 format!(
38 "<{}.{}: '{}'>",
39 stringify!(ParquetWriteMode),
40 self.name(),
41 self.value(),
42 )
43 }
44
45 fn __str__(&self) -> String {
46 self.to_string()
47 }
48
49 #[getter]
50 #[must_use]
51 pub fn name(&self) -> String {
52 self.to_string()
53 }
54
55 #[getter]
56 #[must_use]
57 pub fn value(&self) -> u8 {
58 *self as u8
59 }
60
61 #[classmethod]
62 fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
63 EnumIterator::new::<Self>(py)
64 }
65
66 #[classmethod]
67 #[pyo3(name = "from_str")]
68 fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
69 let data_str: &str = data.extract()?;
70 let tokenized = data_str.to_uppercase();
71 Self::from_str(&tokenized).map_err(to_pyvalue_err)
72 }
73
74 #[classattr]
75 #[pyo3(name = "APPEND")]
76 fn py_append() -> Self {
77 Self::Append
78 }
79
80 #[classattr]
81 #[pyo3(name = "PREPEND")]
82 fn py_prepend() -> Self {
83 Self::Prepend
84 }
85
86 #[classattr]
87 #[pyo3(name = "OVERWRITE")]
88 fn py_overwrite() -> Self {
89 Self::Overwrite
90 }
91
92 #[classattr]
93 #[pyo3(name = "NEWFILE")]
94 fn py_newfile() -> Self {
95 Self::NewFile
96 }
97}