num2words/
currency.rs

1use std::str::FromStr;
2
3/// Defines currencies
4///
5/// Every three-letter variant is a valid ISO 4217 currency code. The only
6/// exceptions are `DINAR`, `DOLLAR`, `PESO` and `RIYAL`, which are generic
7/// terminology for the respective currencies.
8#[derive(Clone, Copy)]
9#[non_exhaustive]
10pub enum Currency {
11    /// Dirham
12    AED,
13    /// Argentine peso
14    ARS,
15    /// Australian dollar
16    AUD,
17    /// Brazilian real
18    BRL,
19    /// Canadian dollar
20    CAD,
21    /// Swiss franc
22    CHF,
23    /// Chilean peso
24    CLP,
25    /// Chinese yuan
26    CNY,
27    /// Colombian peso
28    COP,
29    /// Costa Rican colón
30    CRC,
31    /// Dinar
32    DINAR,
33    /// Dollar
34    DOLLAR,
35    /// Algerian dinar
36    DZD,
37    /// Euro
38    EUR,
39    /// British pound
40    GBP,
41    /// Hong Kong dollar
42    HKD,
43    /// Indonesian rupiah
44    IDR,
45    /// Israeli new shekel
46    ILS,
47    /// Indian rupee
48    INR,
49    /// Japanese yen
50    JPY,
51    /// South Korean won
52    KRW,
53    /// Kuwaiti dinar
54    KWD,
55    /// Kazakhstani tenge
56    KZT,
57    /// Mexican peso
58    MXN,
59    /// Malaysian ringgit
60    MYR,
61    /// Norwegian krone
62    NOK,
63    /// New Zealand dollar
64    NZD,
65    /// Peruvian sol
66    PEN,
67    /// Peso
68    PESO,
69    /// Philippine peso
70    PHP,
71    /// Polish zloty
72    PLN,
73    /// Qatari riyal
74    QAR,
75    /// Riyal
76    RIYAL,
77    /// Russian ruble
78    RUB,
79    /// Saudi riyal
80    SAR,
81    /// Singapore dollar
82    SGD,
83    /// Thai baht
84    THB,
85    /// Turkish lira
86    TRY,
87    /// Taiwan dollar
88    TWD,
89    /// Ukrainian hryvnia
90    UAH,
91    /// US dollar
92    USD,
93    /// Uruguayan peso
94    UYU,
95    /// Vietnamese dong
96    VND,
97    /// South African rand
98    ZAR,
99}
100
101impl Currency {
102    /// Returns a default string representation for the currency
103    ///
104    /// Since many languages share the same work for a specific currency (like
105    /// euro), it is easier and wiser for modularity to have a default value.
106    pub fn default_string(&self, plural_form: bool) -> String {
107        match self {
108            Currency::AED => "dirham{}",
109            Currency::ARS => "argentine peso{}",
110            Currency::AUD => "australian dollar{}",
111            Currency::BRL => {
112                if plural_form {
113                    "reais"
114                } else {
115                    "real"
116                }
117            }
118            Currency::CAD => "canadian dollar{}",
119            Currency::CHF => "franc{}",
120            Currency::CLP => "chilean peso{}",
121            Currency::CNY => "yuan{}",
122            Currency::COP => "colombian peso{}",
123            Currency::CRC => {
124                if plural_form {
125                    "colones"
126                } else {
127                    "colón"
128                }
129            }
130            Currency::DINAR => "dinar{}",
131            Currency::DOLLAR => "dollar{}",
132            Currency::DZD => "algerian dinar{}",
133            Currency::EUR => "euro{}",
134            Currency::GBP => "pound{}",
135            Currency::HKD => "hong kong dollar{}",
136            Currency::IDR => "indonesian rupiah{}",
137            Currency::ILS => "new shekel{}",
138            Currency::INR => "rupee{}",
139            Currency::JPY => "yen{}",
140            Currency::KRW => "won{}",
141            Currency::KWD => "kuwaiti dinar{}",
142            Currency::KZT => "tenge{}",
143            Currency::MXN => "mexican peso{}",
144            Currency::MYR => "ringgit{}",
145            Currency::NOK => "norwegian krone{}",
146            Currency::NZD => "new zealand dollar{}",
147            Currency::PEN => {
148                if plural_form {
149                    "soles"
150                } else {
151                    "sol"
152                }
153            }
154            Currency::PESO => "peso{}",
155            Currency::PHP => "philippine peso{}",
156            Currency::PLN => "zloty{}",
157            Currency::QAR => "qatari riyal{}",
158            Currency::RIYAL => "riyal{}",
159            Currency::RUB => "ruble{}",
160            Currency::SAR => "saudi riyal{}",
161            Currency::SGD => "singapore dollar{}",
162            Currency::THB => "baht{}",
163            Currency::TRY => "lira{}",
164            Currency::TWD => "taiwan dollar{}",
165            Currency::UAH => "hryvnia{}",
166            Currency::USD => "US dollar{}",
167            Currency::UYU => "uruguayan peso{}",
168            Currency::VND => "dong{}",
169            Currency::ZAR => "rand{}",
170        }
171        .replace("{}", if plural_form { "s" } else { "" })
172    }
173
174    /// Returns a default string representation for the cents of the currency
175    pub fn default_subunit_string(&self, cent: &str, plural_form: bool) -> String {
176        String::from(
177            match self {
178                Currency::AED | Currency::KWD => "fils",
179                Currency::ARS | Currency::BRL | Currency::CLP | Currency::COP | Currency::MXN => {
180                    "centavo{}"
181                }
182                Currency::CRC => "céntimo{}",
183                Currency::IDR | Currency::MYR => "sen{}",
184                Currency::KRW => "jeon{}",
185                Currency::SAR => "halalat{}",
186                Currency::THB => "satang{}",
187                Currency::UAH => "kopiyok{}",
188                Currency::UYU => "centesimo{}",
189                Currency::VND => "xu{}",
190                _ => cent,
191            }
192            .replace("{}", if plural_form { "s" } else { "" }),
193        )
194    }
195}
196
197impl FromStr for Currency {
198    type Err = ();
199
200    fn from_str(currency: &str) -> Result<Self, Self::Err> {
201        match currency {
202            "AED" => Ok(Currency::AED),
203            "ARS" => Ok(Currency::ARS),
204            "AUD" => Ok(Currency::AUD),
205            "BRL" => Ok(Currency::BRL),
206            "CAD" => Ok(Currency::CAD),
207            "CHF" => Ok(Currency::CHF),
208            "CLP" => Ok(Currency::CLP),
209            "CNY" => Ok(Currency::CNY),
210            "COP" => Ok(Currency::COP),
211            "CRC" => Ok(Currency::CRC),
212            "DINAR" => Ok(Currency::DINAR),
213            "DOLLAR" => Ok(Currency::DOLLAR),
214            "DZD" => Ok(Currency::DZD),
215            "EUR" => Ok(Currency::EUR),
216            "GBP" => Ok(Currency::GBP),
217            "HKD" => Ok(Currency::HKD),
218            "IDR" => Ok(Currency::IDR),
219            "ILS" => Ok(Currency::ILS),
220            "INR" => Ok(Currency::INR),
221            "JPY" => Ok(Currency::JPY),
222            "KRW" => Ok(Currency::KRW),
223            "KWD" => Ok(Currency::KWD),
224            "KZT" => Ok(Currency::KZT),
225            "MXN" => Ok(Currency::MXN),
226            "MYR" => Ok(Currency::MYR),
227            "NOK" => Ok(Currency::NOK),
228            "NZD" => Ok(Currency::NZD),
229            "PEN" => Ok(Currency::PEN),
230            "PESO" => Ok(Currency::PESO),
231            "PHP" => Ok(Currency::PHP),
232            "PLN" => Ok(Currency::PLN),
233            "QAR" => Ok(Currency::QAR),
234            "RIYAL" => Ok(Currency::RIYAL),
235            "RUB" => Ok(Currency::RUB),
236            "SAR" => Ok(Currency::SAR),
237            "SGD" => Ok(Currency::SGD),
238            "THB" => Ok(Currency::THB),
239            "TRY" => Ok(Currency::TRY),
240            "TWD" => Ok(Currency::TWD),
241            "UAH" => Ok(Currency::UAH),
242            "USD" => Ok(Currency::USD),
243            "UYU" => Ok(Currency::UYU),
244            "VND" => Ok(Currency::VND),
245            "ZAR" => Ok(Currency::ZAR),
246            _ => Err(()),
247        }
248    }
249}