Functional Weave
Code in Python

money.currency-digits@1.0.0

impl/rust.rs

713 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::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_data::CURRENCY_DIGITS;  ← this capability’s own data, compiled from data/currency-digits.json into the same file by fune build

/// Decimal places in a currency's minor unit, per ISO 4217.
///
/// Dividing by 100 is wrong for yen, won, dinar and a few dozen others, so
/// anything moving between minor and major units asks here.
///
/// # Panics
/// Panics if the code is not an active ISO 4217 currency with a minor unit.
pub fn currency_digits(currency: &str) -> i64 {
    match CURRENCY_DIGITS.iter().find(|c| c.code == currency) {
        Some(row) => row.digits,
        None => panic!("no ISO 4217 minor unit for currency \"{}\"", currency),
    }
}

pub fn fune_vector(args: &[Value]) -> Value {
    Value::Int(currency_digits(args[0].as_str()))
}