finnow/finnow-api/source/money/currency.d

34 lines
906 B
D

module money.currency;
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);
}