2024-08-01 17:01:50 +00:00
|
|
|
module money.currency;
|
2024-07-31 17:20:17 +00:00
|
|
|
|
|
|
|
struct Currency {
|
|
|
|
const char[3] code;
|
|
|
|
const ubyte fractionalDigits;
|
|
|
|
const ushort numericCode;
|
|
|
|
|
|
|
|
import std.traits : isSomeString, EnumMembers;
|
|
|
|
static Currency ofCode(S)(S code) if (isSomeString!S) {
|
|
|
|
if (code.length != 3) {
|
|
|
|
throw new Exception("Invalid currency code: " ~ code);
|
|
|
|
}
|
|
|
|
static foreach (c; EnumMembers!Currencies) {
|
|
|
|
if (c.code == code) return c;
|
|
|
|
}
|
|
|
|
throw new Exception("Unknown currency code: " ~ code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Currencies : Currency {
|
|
|
|
USD = Currency("USD", 2, 840),
|
|
|
|
CAD = Currency("CAD", 2, 124),
|
|
|
|
GBP = Currency("GBP", 2, 826),
|
|
|
|
EUR = Currency("EUR", 2, 978),
|
|
|
|
CHF = Currency("CHF", 2, 756),
|
|
|
|
ZAR = Currency("ZAR", 2, 710),
|
|
|
|
JPY = Currency("JPY", 0, 392),
|
|
|
|
INR = Currency("INR", 2, 356)
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest {
|
|
|
|
assert(Currency.ofCode("USD") == Currencies.USD);
|
|
|
|
}
|