Functional Weave
Code in Rust

time.round-to-increment@1.0.0

impl/rust.rs

825 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

/// Round a duration to a whole number of billing increments. The rounding is
/// math.round-div's, so "half-up" here is the same rule as for money.
///
/// # Panics
/// Panics on negative minutes, an increment below one, or an unknown mode.
pub fn round_to_increment(minutes: i64, increment: i64, mode: &str) -> i64 {
    if minutes < 0 {
        panic!("minutes must be a non-negative integer, received {}", minutes);
    }
    if increment < 1 {
        panic!("increment must be a positive integer, received {}", increment);
    }
    round_div(minutes, increment, mode) * increment
}

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