Functional Weave
Code in TypeScript

money.apply-rate@1.0.0

impl/rust.rs

1,001 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::math_round_div::round_div;  ← from math.round-div ^1.0.0 · built alongside by fune
use super::money_amount::{money, money_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

/// Apply a rate given in basis points (10000 = 100%).
///
/// Rates are integers, never floats, so VAT, discounts, interest and
/// commission all stay in exact arithmetic right up to the single rounding
/// step the caller asked for.
pub fn apply_rate(amount: &Money, basis_points: i64, mode: &str) -> Money {
    money(
        round_div(amount.minor * basis_points, 10000, mode),
        &amount.currency,
    )
}

/// The complement: what is left after the rate is taken off.
pub fn apply_rate_complement(amount: &Money, basis_points: i64, mode: &str) -> Money {
    money(
        amount.minor - apply_rate(amount, basis_points, mode).minor,
        &amount.currency,
    )
}

pub fn fune_vector(args: &[Value]) -> Value {
    money_to_value(&apply_rate(
        &money_from_value(&args[0]),
        args[1].as_i64(),
        args[2].as_str(),
    ))
}