nautilus_model/python/types/
currency.rs1use std::str::FromStr;
17
18use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
19use pyo3::{IntoPyObjectExt, prelude::*};
20
21use crate::{enums::CurrencyType, types::Currency};
22
23#[pymethods]
24#[pyo3_stub_gen::derive::gen_stub_pymethods]
25impl Currency {
26 #[new]
30 fn py_new(
31 code: &str,
32 precision: u8,
33 iso4217: u16,
34 name: &str,
35 currency_type: CurrencyType,
36 ) -> PyResult<Self> {
37 Self::new_checked(code, precision, iso4217, name, currency_type).map_err(to_pyvalue_err)
38 }
39
40 fn __reduce__(&self, py: Python) -> PyResult<Py<PyAny>> {
41 let unpickle = py.get_type::<Self>().getattr("_unpickle")?;
42 let args = (
43 self.code.to_string(),
44 self.precision,
45 self.iso4217,
46 self.name.to_string(),
47 self.currency_type.to_string(),
48 )
49 .into_py_any(py)?;
50 (unpickle, args).into_py_any(py)
51 }
52
53 #[staticmethod]
54 fn _unpickle(
55 code: &str,
56 precision: u8,
57 iso4217: u16,
58 name: &str,
59 currency_type_str: &str,
60 ) -> PyResult<Self> {
61 let currency_type = CurrencyType::from_str(currency_type_str).map_err(to_pyvalue_err)?;
62 Self::new_checked(code, precision, iso4217, name, currency_type).map_err(to_pyvalue_err)
63 }
64
65 fn __repr__(&self) -> String {
66 format!("{self:?}")
67 }
68
69 fn __str__(&self) -> &'static str {
70 self.code.as_str()
71 }
72
73 #[getter]
74 #[pyo3(name = "code")]
75 fn py_code(&self) -> &'static str {
76 self.code.as_str()
77 }
78
79 #[getter]
80 #[pyo3(name = "precision")]
81 fn py_precision(&self) -> u8 {
82 self.precision
83 }
84
85 #[getter]
86 #[pyo3(name = "iso4217")]
87 fn py_iso4217(&self) -> u16 {
88 self.iso4217
89 }
90
91 #[getter]
92 #[pyo3(name = "name")]
93 fn py_name(&self) -> &'static str {
94 self.name.as_str()
95 }
96
97 #[getter]
98 #[pyo3(name = "currency_type")]
99 fn py_currency_type(&self) -> CurrencyType {
100 self.currency_type
101 }
102
103 #[staticmethod]
111 #[pyo3(name = "is_fiat")]
112 fn py_is_fiat(code: &str) -> PyResult<bool> {
113 Self::is_fiat(code).map_err(to_pyvalue_err)
114 }
115
116 #[staticmethod]
124 #[pyo3(name = "is_crypto")]
125 fn py_is_crypto(code: &str) -> PyResult<bool> {
126 Self::is_crypto(code).map_err(to_pyvalue_err)
127 }
128
129 #[staticmethod]
130 #[pyo3(name = "is_commodity_backed")]
131 fn py_is_commodidity_backed(code: &str) -> PyResult<bool> {
132 Self::is_commodity_backed(code).map_err(to_pyvalue_err)
133 }
134
135 #[staticmethod]
136 #[pyo3(name = "from_str")]
137 #[pyo3(signature = (value, strict = false))]
138 fn py_from_str(value: &str, strict: bool) -> PyResult<Self> {
139 match Self::from_str(value) {
140 Ok(currency) => Ok(currency),
141 Err(e) => {
142 if strict {
143 Err(to_pyvalue_err(e))
144 } else {
145 Self::new_checked(value, 8, 0, value, CurrencyType::Crypto)
146 .map_err(to_pyvalue_err)
147 }
148 }
149 }
150 }
151
152 #[staticmethod]
161 #[pyo3(name = "register")]
162 #[pyo3(signature = (currency, overwrite = false))]
163 fn py_register(currency: Self, overwrite: bool) -> PyResult<()> {
164 Self::register(currency, overwrite).map_err(to_pyruntime_err)
165 }
166}