lwk/
currency_code.rs

1use std::sync::Arc;
2
3/// Currency code as defined by ISO 4217
4#[derive(uniffi::Object, PartialEq, Eq, Debug, Clone)]
5#[uniffi::export(Display, Eq)]
6pub struct CurrencyCode {
7    pub(crate) inner: lwk_wollet::CurrencyCode,
8}
9
10impl std::fmt::Display for CurrencyCode {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        write!(f, "{}", self.inner)
13    }
14}
15
16impl AsRef<lwk_wollet::CurrencyCode> for CurrencyCode {
17    fn as_ref(&self) -> &lwk_wollet::CurrencyCode {
18        &self.inner
19    }
20}
21
22impl From<lwk_wollet::CurrencyCode> for CurrencyCode {
23    fn from(inner: lwk_wollet::CurrencyCode) -> Self {
24        Self { inner }
25    }
26}
27
28impl From<CurrencyCode> for lwk_wollet::CurrencyCode {
29    fn from(value: CurrencyCode) -> Self {
30        value.inner
31    }
32}
33
34impl From<&CurrencyCode> for lwk_wollet::CurrencyCode {
35    fn from(value: &CurrencyCode) -> Self {
36        value.inner.clone()
37    }
38}
39
40#[uniffi::export]
41impl CurrencyCode {
42    /// Create a CurrencyCode from an alpha3 code (e.g., "USD", "EUR")
43    #[uniffi::constructor]
44    pub fn new(alpha3: &str) -> Result<Arc<CurrencyCode>, crate::LwkError> {
45        let inner = alpha3.parse().map_err(|_| crate::LwkError::Generic {
46            msg: format!("Invalid currency code: {}", alpha3),
47        })?;
48        Ok(Arc::new(CurrencyCode { inner }))
49    }
50
51    /// Get the alpha3 code (e.g., "USD")
52    pub fn alpha3(&self) -> String {
53        self.inner.alpha3.to_string()
54    }
55
56    /// Get the currency name (e.g., "US Dollar")
57    pub fn name(&self) -> String {
58        self.inner.name.to_string()
59    }
60
61    /// Get the number of decimals for this currency
62    pub fn exp(&self) -> i8 {
63        self.inner.exp
64    }
65}