Functional Weave
Code in Rust

form.money-input@1.0.0

impl/rust/money_input_format.rs

3,229 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_format_money_input::{number_style_for, separators_of, write_amount};  ← formatMoneyInput, another function of this group · built into the same file, even by a slim install
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_currency_digits::currency_digits;  ← from money.currency-digits ^1.0.0 · built alongside by fune
use super::money_format_data::CURRENCIES;  ← money.format’s rule data (^1.0.0) · built alongside by fune
use super::money_parse_data::CURRENCY_SYMBOLS;  ← money.parse’s rule data (^1.0.0) · built alongside by fune

/// A currency's symbol: `raw` keeps money.format's spacing ("CHF ", " kr").
pub struct CurrencySymbol {
    pub symbol: String,
    pub raw: String,
    pub before: bool,
}

/// money.format's symbol where it has the currency, else the first symbol
/// money.parse knows (before the amount), else the ISO code itself.
pub fn currency_symbol(currency: &str) -> CurrencySymbol {
    if let Some(row) = CURRENCIES.iter().find(|r| r.code == currency) {
        return CurrencySymbol { symbol: row.symbol.trim_matches(' ').to_string(), raw: row.symbol.to_string(), before: row.symbol_before };
    }
    if let Some(row) = CURRENCY_SYMBOLS.iter().find(|r| r.code == currency) {
        return CurrencySymbol { symbol: row.symbol.to_string(), raw: row.symbol.to_string(), before: true };
    }
    CurrencySymbol { symbol: currency.to_string(), raw: format!("{} ", currency), before: true }
}

/// An amount in a sentence: "£10.00", "100,00 kr", "€1.000,00".
pub fn money_for_message(minor: i64, currency: &str, locale: &str) -> String {
    let digits = currency_digits(currency);
    let sym = currency_symbol(currency);
    let number = write_amount(minor.abs(), digits, number_style_for(locale));
    let body = if sym.before { format!("{}{}", sym.raw, number) } else { format!("{}{}", number, sym.raw) };
    if minor < 0 {
        format!("-{}", body)
    } else {
        body
    }
}

/// What a money input for this currency and locale shows: the symbol, the
/// separators, and 1234.50 written the locale's way.
///
/// # Panics
/// Panics on an unknown currency or locale.
pub fn money_input_format(currency: &str, locale: &str) -> MoneyInputFormat {
    let digits = currency_digits(currency);
    let style = number_style_for(locale);
    let seps = separators_of(style);
    let sym = currency_symbol(currency);
    let mut fraction = String::from("50");
    while fraction.len() < digits as usize {
        fraction.push('0');
    }
    fraction.truncate(digits as usize);
    let example: i64 = format!("1234{}", fraction).parse().unwrap_or(1234);
    MoneyInputFormat {
        currency: currency.to_string(),
        symbol: sym.symbol,
        symbol_before: sym.before,
        digits,
        group_separator: seps.group.to_string(),
        decimal_separator: seps.decimal.to_string(),
        example: write_amount(example, digits, style),
    }
}

pub fn money_input_format_to_value(f: &MoneyInputFormat) -> Value {
    Value::obj(vec![
        ("currency", Value::str(&f.currency)),
        ("symbol", Value::str(&f.symbol)),
        ("symbolBefore", Value::Bool(f.symbol_before)),
        ("digits", Value::Int(f.digits)),
        ("groupSeparator", Value::str(&f.group_separator)),
        ("decimalSeparator", Value::str(&f.decimal_separator)),
        ("example", Value::str(&f.example)),
    ])
}

pub fn fune_vector(args: &[Value]) -> Value {
    money_input_format_to_value(&money_input_format(args[0].as_str(), args[1].as_str()))
}