Functional Weave
Code in TypeScript

property.holding-deposit-cap@1.0.0

impl/rust.rs

1,278 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_amount::{money, money_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

// Keeps annual rent exact as a JavaScript number; the same limit here.
const MAX_MINOR: i64 = 1_000_000_000_000;

/// One week's rent, the statutory maximum holding deposit: the annual rent
/// divided by 52, rounded down once so the deposit can never exceed the cap.
///
/// # Panics
/// Panics on rent not in GBP or negative, or an unknown rent period.
pub fn holding_deposit_cap(rent: &Money, rent_period: &str) -> Money {
    if rent.currency != "GBP" {
        panic!("rent must be in GBP, received {}", rent.currency);
    }
    if rent.minor < 0 {
        panic!("rent must not be negative, received {}", rent.minor);
    }
    let annual = match rent_period {
        "weekly" => rent.minor.checked_mul(52),
        "monthly" => rent.minor.checked_mul(12),
        "annual" => Some(rent.minor),
        other => panic!("unknown rent period \"{}\"", other),
    };
    match annual {
        Some(a) if a <= MAX_MINOR => money(a / 52, "GBP"),
        _ => panic!("annual rent must be at most {} minor units", MAX_MINOR),
    }
}

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