This commit is contained in:
Jorijn van der Graaf 2026-11-01 18:35:23 +01:00
commit 1ea50c50bc
62 changed files with 9982 additions and 608 deletions

View file

@ -1,5 +1,5 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro:Currency;
@ -9,7 +9,27 @@ export namespace Nostro {
enum class Currency : std::uint16_t {
Euro
};
}
namespace Nostro {
struct CurrencyInfo {
std::string_view code;
std::string_view symbol;
std::uint8_t decimals;
};
// What nostro knows of each currency, in the order of Currency's values: a new currency is a value there and a
// row here.
constexpr std::array<CurrencyInfo, 1> currencies = {{
{"EUR", "€", 2},
}};
constexpr const CurrencyInfo& Info(Currency currency) {
return currencies[std::to_underlying(currency)];
}
}
export namespace Nostro {
constexpr Currency GetCurrency(std::string_view name) {
if(name == "EUR" || name == "eur") {
return Currency::Euro;
@ -19,24 +39,15 @@ export namespace Nostro {
}
constexpr std::string_view GetCurrencyCode(Currency code) {
switch(code) {
case Currency::Euro: return "EUR";
}
std::unreachable();
return Info(code).code;
}
constexpr std::string_view GetCurrencySymbol(Currency code) {
switch(code) {
case Currency::Euro: return "€";
}
std::unreachable();
return Info(code).symbol;
}
constexpr std::uint8_t GetCurrencyDecimals(Currency code) {
switch(code) {
case Currency::Euro: return 2;
}
std::unreachable();
return Info(code).decimals;
}
constexpr std::int64_t ParseAmount(std::string_view value, Currency currency) {
@ -91,6 +102,12 @@ export namespace Nostro {
}
return digits;
}
// The amount with its currency's symbol, the sign in front: "-€1.02".
constexpr std::string FormatMoney(std::int64_t minor, Currency currency) {
const std::string amount = FormatAmount(minor, currency);
return amount.starts_with('-') ? std::format("-{}{}", GetCurrencySymbol(currency), amount.substr(1)) : std::format("{}{}", GetCurrencySymbol(currency), amount);
}
}
template<>