impl/rust/format_money_input.rs
3,168 bytes · the Rust implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
use super::form_money_input_data::LOCALES; ← this capability’s own data, compiled from data/locales.json into the same file by fune build
use super::funejson::Value; ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::money_amount::{money_from_value, Money}; ← from money.amount ^1.0.0 · built alongside by fune
use super::money_currency_digits::currency_digits; ← from money.currency-digits ^1.0.0 · built alongside by fune
/// The separators of a money.parse number style, as money.parse reads them.
pub struct NumberSeparators {
/// Every group separator money.parse accepts in this style.
pub groups: &'static [char],
/// The one this capability writes.
pub group: &'static str,
pub decimal: char,
}
/// The money.parse number style for a locale: the exact locale first
/// (case-insensitive, "_" read as "-"), then its language alone.
///
/// # Panics
/// Panics on a locale it does not list.
pub fn number_style_for(locale: &str) -> &'static str {
let wanted = locale.replace('_', "-").to_ascii_lowercase();
if let Some(row) = LOCALES.iter().find(|r| r.locale.to_ascii_lowercase() == wanted) {
return row.style;
}
let language = wanted.split('-').next().unwrap_or("");
if !language.is_empty() {
if let Some(row) = LOCALES.iter().find(|r| r.locale.to_ascii_lowercase() == language) {
return row.style;
}
}
panic!("unknown locale \"{}\": give a BCP 47 locale this capability lists, such as en-GB, de-DE or fr-FR", locale);
}
/// # Panics
/// Panics on an unknown style.
pub fn separators_of(style: &str) -> NumberSeparators {
match style {
"comma-dot" => NumberSeparators { groups: &[','], group: ",", decimal: '.' },
"dot-comma" => NumberSeparators { groups: &['.'], group: ".", decimal: ',' },
"space-comma" => NumberSeparators { groups: &[' ', '\u{a0}', '\u{202f}'], group: " ", decimal: ',' },
"apostrophe-dot" => NumberSeparators { groups: &['\'', '\u{2019}'], group: "'", decimal: '.' },
other => panic!("unknown number style \"{}\"", other),
}
}
/// Minor units written with thousands grouping and every decimal place, no symbol.
pub fn write_amount(minor: i64, digits: i64, style: &str) -> String {
let seps = separators_of(style);
let digits = digits as usize;
let mut raw = minor.unsigned_abs().to_string();
while raw.len() < digits + 1 {
raw.insert(0, '0');
}
let (whole, fraction) = raw.split_at(raw.len() - digits);
let mut grouped = String::new();
for (i, ch) in whole.chars().enumerate() {
if i > 0 && (whole.len() - i) % 3 == 0 {
grouped.push_str(seps.group);
}
grouped.push(ch);
}
let number = if digits > 0 { format!("{}{}{}", grouped, seps.decimal, fraction) } else { grouped };
if minor < 0 {
format!("-{}", number)
} else {
number
}
}
/// An amount as a money input shows it for editing: the locale's grouping and
/// decimal mark, every decimal place, no symbol.
///
/// # Panics
/// Panics on an unknown currency or locale.
pub fn format_money_input(amount: &Money, locale: &str) -> String {
let digits = currency_digits(&amount.currency);
write_amount(amount.minor, digits, number_style_for(locale))
}
pub fn fune_vector(args: &[Value]) -> Value {
Value::str(&format_money_input(&money_from_value(&args[0]), args[1].as_str()))
}