Skip to main content

nautilus_hyperliquid/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//! Hyperliquid 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::{
25    HyperliquidConditionalOrderType, HyperliquidProductType, HyperliquidTpSl,
26    HyperliquidTrailingOffsetType,
27};
28
29#[pymethods]
30impl HyperliquidTpSl {
31    #[new]
32    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
33        let t = Self::type_object(py);
34        Self::py_from_str(&t, value)
35    }
36
37    fn __hash__(&self) -> isize {
38        *self as isize
39    }
40
41    fn __repr__(&self) -> String {
42        format!(
43            "<{}.{}: '{}'>",
44            stringify!(HyperliquidTpSl),
45            self.name(),
46            self.value(),
47        )
48    }
49
50    fn __str__(&self) -> String {
51        self.to_string()
52    }
53
54    #[getter]
55    #[must_use]
56    pub fn name(&self) -> &str {
57        self.as_ref()
58    }
59
60    #[getter]
61    #[must_use]
62    pub fn value(&self) -> String {
63        self.to_string().to_lowercase()
64    }
65
66    #[staticmethod]
67    #[must_use]
68    fn variants() -> Vec<String> {
69        Self::iter().map(|x| x.to_string()).collect()
70    }
71
72    #[classmethod]
73    #[pyo3(name = "from_str")]
74    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
75        let data_str: String = data.str()?.extract()?;
76        Self::from_str(&data_str).map_err(to_pyvalue_err)
77    }
78}
79
80#[pymethods]
81impl HyperliquidConditionalOrderType {
82    #[new]
83    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
84        let t = Self::type_object(py);
85        Self::py_from_str(&t, value)
86    }
87
88    fn __hash__(&self) -> isize {
89        *self as isize
90    }
91
92    fn __repr__(&self) -> String {
93        format!(
94            "<{}.{}: '{}'>",
95            stringify!(HyperliquidConditionalOrderType),
96            self.name(),
97            self.value(),
98        )
99    }
100
101    fn __str__(&self) -> String {
102        self.to_string()
103    }
104
105    #[getter]
106    #[must_use]
107    pub fn name(&self) -> &str {
108        self.as_ref()
109    }
110
111    #[getter]
112    #[must_use]
113    pub fn value(&self) -> String {
114        self.to_string().to_lowercase()
115    }
116
117    #[staticmethod]
118    #[must_use]
119    fn variants() -> Vec<String> {
120        Self::iter().map(|x| x.to_string()).collect()
121    }
122
123    #[classmethod]
124    #[pyo3(name = "from_str")]
125    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
126        let data_str: String = data.str()?.extract()?;
127        Self::from_str(&data_str).map_err(to_pyvalue_err)
128    }
129}
130
131#[pymethods]
132impl HyperliquidTrailingOffsetType {
133    #[new]
134    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
135        let t = Self::type_object(py);
136        Self::py_from_str(&t, value)
137    }
138
139    fn __hash__(&self) -> isize {
140        *self as isize
141    }
142
143    fn __repr__(&self) -> String {
144        format!(
145            "<{}.{}: '{}'>",
146            stringify!(HyperliquidTrailingOffsetType),
147            self.name(),
148            self.value(),
149        )
150    }
151
152    fn __str__(&self) -> String {
153        self.to_string()
154    }
155
156    #[getter]
157    #[must_use]
158    pub fn name(&self) -> &str {
159        self.as_ref()
160    }
161
162    #[getter]
163    #[must_use]
164    pub fn value(&self) -> String {
165        self.to_string().to_lowercase()
166    }
167
168    #[staticmethod]
169    #[must_use]
170    fn variants() -> Vec<String> {
171        Self::iter().map(|x| x.to_string()).collect()
172    }
173
174    #[classmethod]
175    #[pyo3(name = "from_str")]
176    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
177        let data_str: String = data.str()?.extract()?;
178        Self::from_str(&data_str).map_err(to_pyvalue_err)
179    }
180}
181
182#[pymethods]
183impl HyperliquidProductType {
184    #[new]
185    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
186        let t = Self::type_object(py);
187        Self::py_from_str(&t, value)
188    }
189
190    fn __hash__(&self) -> isize {
191        *self as isize
192    }
193
194    fn __eq__(&self, other: &Self) -> bool {
195        self == other
196    }
197
198    fn __repr__(&self) -> String {
199        format!(
200            "<{}.{}: '{}'>",
201            stringify!(HyperliquidProductType),
202            self.name(),
203            self.value(),
204        )
205    }
206
207    fn __str__(&self) -> String {
208        self.to_string()
209    }
210
211    #[getter]
212    #[must_use]
213    pub fn name(&self) -> &str {
214        self.as_ref()
215    }
216
217    #[getter]
218    #[must_use]
219    pub fn value(&self) -> String {
220        self.to_string()
221    }
222
223    #[staticmethod]
224    #[must_use]
225    fn variants() -> Vec<String> {
226        Self::iter().map(|x| x.to_string()).collect()
227    }
228
229    #[classmethod]
230    #[pyo3(name = "from_str")]
231    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
232        let data_str: String = data.str()?.extract()?;
233        Self::from_str(&data_str).map_err(to_pyvalue_err)
234    }
235}