Functional Weave
Code in Rust

money.add@1.0.1

impl/rust.rs

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

/// # Panics
/// Panics if the two amounts are in different currencies.
pub fn add_money(a: &Money, b: &Money) -> Money {
    assert_same_currency(a, b);
    money(a.minor + b.minor, &a.currency)
}

/// # Panics
/// Panics if the two amounts are in different currencies.
pub fn subtract_money(a: &Money, b: &Money) -> Money {
    assert_same_currency(a, b);
    money(a.minor - b.minor, &a.currency)
}

pub fn negate_money(a: &Money) -> Money {
    money(-a.minor, &a.currency)
}

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