nautilus_dydx/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
16//! dYdX 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::DydxNetwork;
25
26#[pymethods]
27#[pyo3_stub_gen::derive::gen_stub_pymethods]
28impl DydxNetwork {
29 /// dYdX network environment (mainnet vs testnet).
30 ///
31 /// This selects the underlying Cosmos chain for transaction submission.
32 #[new]
33 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
34 let t = Self::type_object(py);
35 Self::py_from_str(&t, value)
36 }
37
38 const fn __hash__(&self) -> isize {
39 *self as isize
40 }
41
42 fn __str__(&self) -> String {
43 self.to_string()
44 }
45
46 #[getter]
47 #[must_use]
48 pub fn name(&self) -> String {
49 self.to_string()
50 }
51
52 #[getter]
53 #[must_use]
54 pub fn value(&self) -> u8 {
55 *self as u8
56 }
57
58 #[classmethod]
59 #[must_use]
60 fn variants(_: &Bound<'_, PyType>) -> Vec<String> {
61 Self::iter().map(|x| x.to_string()).collect()
62 }
63
64 #[classmethod]
65 #[pyo3(name = "from_str")]
66 fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
67 let data_str: String = data.str()?.extract()?;
68 Self::from_str(&data_str).map_err(to_pyvalue_err)
69 }
70}