Functional Weave
Code in Rust

money.amount

A currency-tagged monetary amount held in integer minor units.

1.0.2 · published 2026-10-03 by charlie · Anterra

Pinned by 17 tests, run in TypeScript, Python and Rust.

What it does

Money is never a float. 0.1 + 0.2 is not 0.3 in binary floating point, and an invoice that is a penny out is a support ticket.

Amounts are integers in the currency's minor unit, tagged with the currency so that adding GBP to EUR fails loudly rather than silently producing a number.

For example

  • money(12,000, GBP) → £120.00 pounds and pence
  • money(0, GBP) → £0.00 zero
  • money(-2,599, GBP) → -£25.99 negative amounts are allowed (credit notes)

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

pub fn money(minor: i64, currency: &str) -> Money
minorintamount in minor units: pence, cents, yen
currencystringISO 4217 alphabetic code, uppercase
returnsMoney

The type it declares, generated into your project

/// A monetary amount in integer minor units, tagged with its currency.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Money {
    /// pence, cents, yen; never a float
    pub minor: i64,
    /// ISO 4217 alphabetic code, uppercase
    pub currency: String,
}

Your code names it in one line, in the file that uses it

fune!(money.amount@^1);  // then call money(…)
impl/rust.rs · 56 lines · open · 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

fn is_currency_code(code: &str) -> bool {
    code.len() == 3 && code.chars().all(|c| c.is_ascii_uppercase())
}

/// # Panics
/// Panics if `currency` is not an uppercase ISO 4217 alphabetic code.
pub fn money(minor: i64, currency: &str) -> Money {
    if !is_currency_code(currency) {
        panic!("\"{}\" is not an uppercase ISO 4217 currency code", currency);
    }
    Money {
        minor,
        currency: currency.to_string(),
    }
}

/// Guard against the silent nonsense of adding GBP to EUR.
///
/// # Panics
/// Panics if the two amounts are in different currencies.
pub fn assert_same_currency(a: &Money, b: &Money) {
    if a.currency != b.currency {
        panic!("currency mismatch: {} and {}", a.currency, b.currency);
    }
}

pub fn zero(currency: &str) -> Money {
    money(0, currency)
}

pub fn money_to_value(m: &Money) -> Value {
    Value::obj(vec![
        ("minor", Value::Int(m.minor)),
        ("currency", Value::str(&m.currency)),
    ])
}

pub fn money_from_value(v: &Value) -> Money {
    Money {
        minor: v.get("minor").as_i64(),
        currency: v.get("currency").as_str().to_string(),
    }
}

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 below quietly change it.
    if let Value::Float(f) = args[0] {
        if f.fract() != 0.0 {
            panic!("money() takes integer minor units, received {}", f);
        }
    }
    money_to_value(&money(args[0].as_i64(), args[1].as_str()))
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Rust package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:

fune add money.amount
Download for Rust money.amount-1.0.2-rust.fune · 5,953 bytes sha256 71196da22b8881bfe88906753a1f14ea46693a7d8e11081a483cc53fe8d2300f

The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./money.amount-1.0.2-rust.fune, or fetch it from a terminal with fune pull money.amount@1.0.2:rust.

The whole function, every language, is one file too: money.amount-1.0.2.fune, 8,117 bytes, sha256 b8ba6ab0ede8ca6006c44677a6da9c5d26f31ea497f0dbc75bfc7587ddc0ab66. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

// fune: before money.amount

after — your function gets the result and the arguments, and returns the final result.

// fune: after money.amount

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show money.amount --steps.

// fune: step money.amount after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
pounds and pence 12,000, GBP → £120.00
zero 0, GBP → £0.00
negative amounts are allowed (credit notes) -2,599, GBP → -£25.99
zero-decimal currency 1,500, JPY → ¥1,500
lowercase currency is rejected 100, gbp → error: ISO 4217
non-integer minor units are rejected 10.5, GBP → error: integer minor units
the largest safe integer is held exactly 9,007,199,254,740,991, USD → $90,071,992,547,409.91
a negative zero-decimal amount -1, JPY → -¥1
a three-decimal currency is still whole minor units (fils) 1,234, KWD → 1.234 KWD
a fractional credit is rejected, not rounded -0.5, GBP → error: integer minor units
Show the other 7 tests
CaseArgumentsExpected
two letters are not a currency code 100, GB → error: ISO 4217
four letters are not a currency code 100, GBPX → error: ISO 4217
an empty currency is rejected 100, → error: ISO 4217
mixed case is rejected, not upper-cased 100, Gbp → error: ISO 4217
digits are not a currency code 100, 123 → error: ISO 4217
a trailing newline is not part of a currency code 100, GBP → error: ISO 4217
a leading newline is not part of a currency code 100, GBP → error: ISO 4217

More from the author

1.0.1 adds tests; behaviour unchanged.

1.0.2 fixes Python accepting a trailing newline in currency; adds tests.

Files

PathBytes
README.md414
impl/python.py949
impl/rust.rs1,599
impl/typescript.ts1,093
vectors.json1,927