Functional Weave
Code in TypeScript

math.basis-points@2.0.0

impl/rust/to_basis_points.rs

996 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_basis_points_convert_rate::convert_rate;  ← convertRate, another function of this group · built into the same file, even by a slim install

/// A rate as whole basis points, the unit the registry keeps rates in.
/// Refuses a rate that is not a whole number of basis points (12.345% is
/// 1234.5) rather than round it: which way to round a rate is the caller's
/// policy, not a conversion's.
///
/// # Panics
/// Panics if the rate is not a whole number of basis points or is beyond ±(2^53 - 1).
pub fn to_basis_points(value: &str, unit: &str) -> i64 {
    let text = convert_rate(value, unit, "basis-points");
    if text.contains('.') {
        panic!("{} {} is not a whole number of basis points", value, unit);
    }
    match text.parse::<i64>() {
        Ok(bp) if (-9_007_199_254_740_991..=9_007_199_254_740_991).contains(&bp) => bp,
        _ => panic!("{} {} is too large for integer basis points", value, unit),
    }
}

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