1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//! Module to house the currency structs.

macro_rules! international_currency_symbol {
	() => {
		"\u{00A4}"
	};
}

pub(super) use international_currency_symbol;

use super::Mudra;

#[allow(private_bounds)]
pub trait Currency: Sealed {
	/// Returns the ISO 4217 code of the currency.
	fn code(&self) -> &'static str;

	/// Returns the corresponding [Mudra] variant.
	fn mudra(&self) -> Mudra;

	/// Returns the name of the currency in English.
	fn name(&self) -> &'static str;

	/// Returns the ISO 4217 numeric code of the currency.
	fn numeric(&self) -> u16;

	#[doc = concat!(
		"Returns the commonly used symbol of the currency. ",
		"If there is no symbol associated, the international currency symbol `",
		international_currency_symbol!(),
		"` is returned"
	)]
	fn symbol(&self) -> &'static str;
}

trait Sealed {}

#[rustfmt::skip]
macro_rules! define_currency {
	(
		$currency:ident,
		$code:literal,
		$name:literal,
		$numeric:literal
		$(,)?
    ) => {
		#[doc = concat!($numeric , " - ", $name, " (", international_currency_symbol!(), ")")]
		#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
		pub struct $currency;

		impl $currency {
			const CODE: &'static str = $code;

			const MUDRA: Mudra = Mudra::$currency;

			const NAME: &'static str = $name;

			const NUMERIC: u16 = $numeric;

			const SYMBOL: &'static str = international_currency_symbol!();
		}

		impl TryFrom<Mudra> for $currency {
			type Error = ();

			#[doc = concat!(
				"Returns [`", $code, "`] ", "when `mudra` is [`Mudra::", $code, "`], else `Err(())`.",
			)]
			fn try_from(mudra: Mudra) -> Result<Self, Self::Error> {
				Self::try_from(&mudra)
			}
		}

		impl TryFrom<&Mudra> for $currency {
			type Error = ();

			#[doc = concat!(
				"Returns [`", $code, "`] ", "when `mudra` is [`Mudra::", $code, "`], else `Err(())`.",
			)]
			fn try_from(mudra: &Mudra) -> Result<Self, Self::Error> {
				match mudra {
					Mudra::$currency => Ok(Self),
					_ => Err(()),
				}
			}
		}

		impl From<$currency> for Mudra {
			#[doc = concat!("Returns [`Mudra::", $code, "`].")]
			fn from(currency: $currency) -> Self {
				Self::from(&currency)
			}
		}

		impl From<&$currency> for Mudra {
			#[doc = concat!("Returns [`Mudra::", $code, "`].")]
			fn from(_currency: &$currency) -> Self {
				Self::$currency
			}
		}

		impl Currency for $currency {
			#[doc = concat!("Returns", " `", $code, "`.")]
			fn code(&self) -> &'static str {
				Self::CODE
			}

			#[doc = concat!("Returns", " [`Mudra::", $code, "`].")]
			fn mudra(&self) -> Mudra {
				Self::MUDRA
			}

			#[doc = concat!("Returns", " `", $name, "`.")]
			fn name(&self) -> &'static str {
				Self::NAME
			}

			#[doc = concat!("Returns", " `", $numeric, "`.")]
			fn numeric(&self) -> u16 {
				Self::NUMERIC
			}

			#[doc = concat!("Returns", " `", international_currency_symbol!(), "`.")]
			fn symbol(&self) -> &'static str {
				Self::SYMBOL
			}
		}

		impl Sealed for $currency {}
	};

	(
		$currency:ident,
		$code:literal,
		$name:literal,
		$numeric:literal,
		$symbol:literal
		$(,)?
    ) => {
		#[doc = concat!($numeric, " - ", $name, " (", $symbol, ")")]
		#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
		pub struct $currency;

		impl $currency {
			const CODE: &'static str = $code;

			const MUDRA: Mudra = Mudra::$currency;

			const NAME: &'static str = $name;

			const NUMERIC: u16 = $numeric;

			const SYMBOL: &'static str = $symbol;
		}

		impl TryFrom<Mudra> for $currency {
			type Error = ();

			#[doc = concat!(
				"Returns [`", $code, "`] ", "when `mudra` is [`Mudra::", $code, "`], else `Err(())`.",
			)]
			fn try_from(mudra: Mudra) -> Result<Self, Self::Error> {
				Self::try_from(&mudra)
			}
		}

		impl TryFrom<&Mudra> for $currency {
			type Error = ();

			#[doc = concat!(
				"Returns [`", $code, "`] ", "when `mudra` is [`Mudra::", $code, "`], else `Err(())`.",
			)]
			fn try_from(mudra: &Mudra) -> Result<Self, Self::Error> {
				match mudra {
					Mudra::$currency => Ok(Self),
					_ => Err(()),
				}
			}
		}

		impl From<$currency> for Mudra {
			#[doc = concat!("Returns [`Mudra::", $code, "`].")]
			fn from(currency: $currency) -> Self {
				Self::from(&currency)
			}
		}

		impl From<&$currency> for Mudra {
			#[doc = concat!("Returns [`Mudra::", $code, "`].")]
			fn from(_currency: &$currency) -> Self {
				Self::$currency
			}
		}

		impl Currency for $currency {
			#[doc = concat!("Returns", " `", $code, "`.")]
			fn code(&self) -> &'static str {
				Self::CODE
			}

			#[doc = concat!("Returns", " [`Mudra::", $code, "`].")]
			fn mudra(&self) -> Mudra {
				Self::MUDRA
			}

			#[doc = concat!("Returns", " `", $name, "`.")]
			fn name(&self) -> &'static str {
				Self::NAME
			}

			#[doc = concat!("Returns", " `", $numeric, "`.")]
			fn numeric(&self) -> u16 {
				Self::NUMERIC
			}

			#[doc = concat!("Returns", " `", $symbol, "`.")]
			fn symbol(&self) -> &'static str {
				Self::SYMBOL
			}
		}

		impl Sealed for $currency {}
	};
}

define_currency!(
	AED,
	"AED",
	"United Arab Emirates dirham",
	784,
	"\u{062F}\u{002E}\u{0625}",
);
define_currency!(ARS, "ARS", "Argentine peso", 32, "\u{0024}");
define_currency!(AUD, "AUD", "Australian dollar", 36, "\u{0024}");
define_currency!(BGN, "BGN", "Bulgarian lev", 975, "\u{043B}\u{0432}\u{002E}");
define_currency!(
	BHD,
	"BHD",
	"Bahraini dinar",
	48,
	"\u{002E}\u{062F}\u{002E}\u{0628}",
);
define_currency!(BOV, "BOV", "Bolivian Mvdol", 984);
define_currency!(BRL, "BRL", "Brazilian real", 986, "\u{0024}");
define_currency!(CAD, "CAD", "Canadian dollar", 124, "\u{0024}");
define_currency!(CHF, "CHF", "Swiss franc", 756);
define_currency!(CLP, "CLP", "Chilean peso", 152, "\u{0024}");
define_currency!(CNY, "CNY", "Renminbi", 156, "\u{00A5}");
define_currency!(COP, "COP", "Colombian peso", 170, "\u{0024}");
define_currency!(CZK, "CZK", "Czech koruna", 203, "\u{004B}\u{010D}");
define_currency!(DKK, "DKK", "Danish krone", 208, "\u{006B}\u{0072}");
define_currency!(EUR, "EUR", "Euro", 978, "\u{20AC}");
define_currency!(GBP, "GBP", "Pound sterling", 826, "\u{00A3}");
define_currency!(HKD, "HKD", "Hong Kong dollar", 344, "\u{0024}");
define_currency!(HUF, "HUF", "Hungarian forint", 348, "\u{0046}\u{0074}");
define_currency!(IDR, "IDR", "Indonesian rupiah", 360, "\u{0052}\u{0070}");
define_currency!(ILS, "ILS", "Israeli new shekel", 376, "\u{20AA}");
define_currency!(INR, "INR", "Indian rupee", 356, "\u{20B9}");
define_currency!(JPY, "JPY", "Japanese yen", 392, "\u{00A5}");
define_currency!(KRW, "KRW", "South Korean won", 410, "\u{20A9}");
define_currency!(MXN, "MXN", "Mexican peso", 484, "\u{0024}");
define_currency!(MYR, "MYR", "Malaysian ringgit", 458, "\u{0052}\u{004D}");
define_currency!(NOK, "NOK", "Norwegian krone", 578, "\u{006B}\u{0072}");
define_currency!(NZD, "NZD", "New Zealand dollar", 554, "\u{0024}");
define_currency!(PEN, "PEN", "Peruvian sol", 604, "\u{0053}\u{002F}");
define_currency!(PHP, "PHP", "Philippine peso", 608, "\u{20B1}");
define_currency!(PLN, "PLN", "Polish zloty", 985, "\u{007A}\u{0142}");
define_currency!(RON, "RON", "Romanian leu", 946, "\u{004C}");
define_currency!(RUB, "RUB", "Russian ruble", 643, "\u{20BD}");
define_currency!(SAR, "SAR", "Saudi riyal", 682, "\u{0631}\u{002E}\u{0633}");
define_currency!(SEK, "SEK", "Swedish krona", 752, "\u{006B}\u{0072}");
define_currency!(SGD, "SGD", "Singapore dollar", 702, "\u{0024}");
define_currency!(THB, "THB", "Thai baht", 764, "\u{0E3F}");
define_currency!(TRY, "TRY", "Turkish lira", 949, "\u{20BA}");
define_currency!(TWD, "TWD", "New Taiwan dollar", 901, "\u{0024}");
define_currency!(USD, "USD", "United States dollar", 840, "\u{0024}");
define_currency!(ZAR, "ZAR", "South African rand", 710, "\u{0052}");

#[cfg(test)]
mod tests {
	use super::Currency;
	use super::{BOV, USD};

	#[test]
	fn has_code_name_numeric_and_international_currency_symbol() {
		let currency = BOV;

		assert_eq!(currency.code(), "BOV");
		assert_eq!(currency.name(), "Bolivian Mvdol");
		assert_eq!(currency.numeric(), 984);
		assert_eq!(currency.symbol(), "¤");
	}

	#[test]
	fn has_code_name_numeric_symbol() {
		let currency = USD;

		assert_eq!(currency.code(), "USD");
		assert_eq!(currency.name(), "United States dollar");
		assert_eq!(currency.numeric(), 840);
		assert_eq!(currency.symbol(), "$");
	}
}