Functional Weave
Code in TypeScript

money.compare@2.0.0

impl/rust/clamp_money.rs

887 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_clamp::clamp;  ← from math.clamp ^1.0.0 · built alongside by fune
use super::money_amount::{assert_same_currency, money, money_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

/// `amount` held within `min..=max`, all three in one currency. Bounds the
/// wrong way round are an error (from math.clamp), not an answer: capping a
/// refund at the original charge with the bounds swapped is a bug to surface.
///
/// # Panics
/// Panics on mixed currencies or if `min` is above `max`.
pub fn clamp_money(amount: &Money, min: &Money, max: &Money) -> Money {
    assert_same_currency(amount, min);
    assert_same_currency(amount, max);
    money(clamp(amount.minor, min.minor, max.minor), &amount.currency)
}

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