Functional Weave
Code in TypeScript

math.basis-points@2.0.0

impl/rust/from_basis_points.rs

1,119 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

/// Integer basis points as the shortest exact decimal in another unit.
///
/// # Panics
/// Panics if `basis_points` is beyond ±(2^53 - 1) or the unit is unknown.
pub fn from_basis_points(basis_points: i64, unit: &str) -> String {
    // i64 could go further, but TypeScript cannot, and the three languages
    // must give the same answer or the same error.
    if !(-9_007_199_254_740_991..=9_007_199_254_740_991).contains(&basis_points) {
        panic!("basisPoints is outside the safe integer range (±9007199254740991)");
    }
    convert_rate(&basis_points.to_string(), "basis-points", unit)
}

pub fn fune_vector(args: &[Value]) -> Value {
    // Refuse what the typed signature cannot hold, with the wording TypeScript
    // and Python use, rather than let the conversion quietly truncate it.
    if let Value::Float(f) = args[0] {
        if f.fract() != 0.0 {
            panic!("basisPoints must be an integer, received {}", f);
        }
    }
    Value::Str(from_basis_points(args[0].as_i64(), args[1].as_str()))
}