Functional Weave
Code in Rust

finance.invoice.line-total

Net total for one invoice line: unit price by quantity, less a discount.

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

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

What it does

The discount is rounded once, at the line, before tax is applied. Rounding the discount and the tax in the other order produces answers that differ by a penny on roughly one line in five, which is exactly the kind of difference that turns into a reconciliation project.

For example

  • line_total(£1.99, 3, 0%) → £5.97 three units at 1.99
  • line_total(£1.99, 3, 10%) → £5.37 ten percent off three units
  • line_total(£500.00, 1, 0%) → £500.00 single unit, no discount

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 line_total(unit_price: &Money, quantity: i64, discount_basis_points: i64) -> Money
unit_priceMoney
quantityintnegative for credit lines
discount_basis_pointsint1000 = 10% off
returnsMoney

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

fune!(finance.invoice.line-total@^1);  // then call line_total(…)
impl/rust.rs · 44 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
use super::money_amount::{money, money_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune
use super::money_apply_rate::apply_rate;  ← from money.apply-rate ^1.0.0 · built alongside by fune

/// Net total for one invoice line.
///
/// The discount is rounded once, here, before any tax is applied. Rounding the
/// discount and the tax in the other order produces answers that differ by a
/// penny on roughly one line in five.
///
/// # Panics
/// Panics if the discount is outside 0..=10000 basis points.
pub fn line_total(unit_price: &Money, quantity: i64, discount_basis_points: i64) -> Money {
    if !(0..=10000).contains(&discount_basis_points) {
        panic!(
            "discount_basis_points must be between 0 and 10000, received {}",
            discount_basis_points
        );
    }

    let gross = money(unit_price.minor * quantity, &unit_price.currency);
    let discount = apply_rate(&gross, discount_basis_points, "half-up");
    money(gross.minor - discount.minor, &gross.currency)
}

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[1] {
        if f.fract() != 0.0 {
            panic!("quantity must be an integer, received {}", f);
        }
    }
    if let Value::Float(f) = args[2] {
        if f.fract() != 0.0 {
            panic!("discountBasisPoints must be between 0 and 10000, received {}", f);
        }
    }
    money_to_value(&line_total(
        &money_from_value(&args[0]),
        args[1].as_i64(),
        args[2].as_i64(),
    ))
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and its 2 dependencies, 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 finance.invoice.line-total
Download for Rust finance.invoice.line-total-1.0.0-rust.fune · 4,900 bytes sha256 3a92b97db5d48f71ab20ba891108f0d4be565ed68ed330535a655237fe08f7ad

The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./finance.invoice.line-total-1.0.0-rust.fune, or fetch it from a terminal with fune pull finance.invoice.line-total@1.0.0:rust.

The whole function, every language, is one file too: finance.invoice.line-total-1.0.0.fune, 7,070 bytes, sha256 c2462dccc8a0b7672e97f8e360047906aee3004d804143ee03dd6a0afcc9520a. 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 finance.invoice.line-total

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

// fune: after finance.invoice.line-total

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

// fune: replace money.amount in finance.invoice.line-total
// fune: replace money.apply-rate in finance.invoice.line-total

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 finance.invoice.line-total --steps.

// fune: step finance.invoice.line-total 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
three units at 1.99 £1.99, 3, 0% → £5.97
ten percent off three units £1.99, 3, 10% → £5.37
single unit, no discount £500.00, 1, 0% → £500.00
zero quantity is zero £500.00, 0, 0% → £0.00
a credit line is negative £500.00, -1, 0% → -£500.00
a discount that lands on half a penny rounds up £99.99, 1, 10% → £89.99
a full discount leaves nothing £19.99, 2, 100% → £0.00
a fractional quantity is an error £1.99, 2.5, 0% → error: quantity must be an integer
a discount above 100 percent is an error £1.99, 1, 100.01% → error: between 0 and 10000

Files

PathBytes
README.md300
impl/python.py1,076
impl/rust.rs1,620
impl/typescript.ts995
vectors.json1,348