Functional Weave
Code in Rust

retail.volumetric-weight

Chargeable parcel weight: the greater of actual and volumetric (dimensional) weight, rounded up to a billing step.

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

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

What it does

Carriers charge for the space a parcel takes as well as its weight. The volumetric weight is `length x width x height / divisor`, where the divisor is in cubic centimetres per kilogram (5000 for most express couriers, 4000 or 6000 for some services), and the chargeable weight is whichever of actual and volumetric is greater.

Working in millimetres and grams keeps it all integers: mm³ / (cm³/kg) is exactly grams, because 1 cm³ = 1000 mm³ and 1 kg = 1000 g cancel. A 400 x 300 x 200 mm box at divisor 5000 is 24,000,000 / 5000 = 4800 g.

For example

  • chargeable_weight(400, 300, 200, 2,500, 5,000, 500) → actual grams 2,500, volumetric grams 4,800, chargeable grams 5,000, basis volumetric a light bulky box is charged on volume, rounded up to the half kilo
  • chargeable_weight(400, 300, 200, 6,200, 5,000, 500) → actual grams 6,200, volumetric grams 4,800, chargeable grams 6,500, basis actual a heavy small box is charged on actual weight
  • chargeable_weight(400, 300, 200, 4,800, 5,000, 500) → actual grams 4,800, volumetric grams 4,800, chargeable grams 5,000, basis actual equal weights: the basis is actual

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 chargeable_weight(length_mm: i64, width_mm: i64, height_mm: i64, actual_grams: i64, divisor: i64, round_up_to_grams: i64) -> ChargeableWeight
length_mmintoutside dimensions in millimetres, 1 or more
width_mmint
height_mmint
actual_gramsintweight on the scales, 0 or more
divisorintthe carrier's volumetric divisor in cm3 per kg: 5000 is common, 4000 and 6000 also
round_up_to_gramsintthe carrier's billing step: 1, 500 for half kilos, 1000 for whole kilos
returnsChargeableWeight

The types it declares, generated into your project

/// Both weights, which one won, and the weight to charge on.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChargeableWeight {
    pub actual_grams: i64,
    /// length x width x height / divisor, rounded up to the gram
    pub volumetric_grams: i64,
    /// the greater of the two, rounded up to the billing step
    pub chargeable_grams: i64,
    /// volumetric only when it is strictly greater
    pub basis: String,
}

// WeightBasis is a string in Rust, one of: "actual", "volumetric".
// Parameters take it as &str and results hold it as String.

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

fune!(retail.volumetric-weight@^1);  // then call chargeable_weight(…)
impl/rust.rs · 66 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::math_round_div::round_div;  ← from math.round-div ^1.0.0 · built alongside by fune

/// The weight a carrier charges on: the greater of actual and volumetric,
/// rounded up to the carrier's billing step.
///
/// mm³ / (cm³ per kg) is exactly grams, so everything stays in integers.
///
/// # Panics
/// Panics on a dimension below 1 mm, a negative weight, or a divisor or
/// billing step below 1.
pub fn chargeable_weight(
    length_mm: i64,
    width_mm: i64,
    height_mm: i64,
    actual_grams: i64,
    divisor: i64,
    round_up_to_grams: i64,
) -> ChargeableWeight {
    for d in [length_mm, width_mm, height_mm] {
        if d < 1 {
            panic!("dimensions must be 1 mm or more, received {}", d);
        }
    }
    if actual_grams < 0 {
        panic!("actualGrams must not be negative, received {}", actual_grams);
    }
    if divisor < 1 {
        panic!("divisor must be 1 or more, received {}", divisor);
    }
    if round_up_to_grams < 1 {
        panic!("roundUpToGrams must be 1 or more, received {}", round_up_to_grams);
    }
    let volumetric_grams = round_div(length_mm * width_mm * height_mm, divisor, "up");
    let heavier = actual_grams.max(volumetric_grams);
    ChargeableWeight {
        actual_grams,
        volumetric_grams,
        chargeable_grams: round_div(heavier, round_up_to_grams, "up") * round_up_to_grams,
        basis: if volumetric_grams > actual_grams {
            "volumetric".to_string()
        } else {
            "actual".to_string()
        },
    }
}

pub fn chargeable_weight_to_value(w: &ChargeableWeight) -> Value {
    Value::obj(vec![
        ("actualGrams", Value::Int(w.actual_grams)),
        ("volumetricGrams", Value::Int(w.volumetric_grams)),
        ("chargeableGrams", Value::Int(w.chargeable_grams)),
        ("basis", Value::str(&w.basis)),
    ])
}

pub fn fune_vector(args: &[Value]) -> Value {
    chargeable_weight_to_value(&chargeable_weight(
        args[0].as_i64(),
        args[1].as_i64(),
        args[2].as_i64(),
        args[3].as_i64(),
        args[4].as_i64(),
        args[5].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 1 dependency, 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 retail.volumetric-weight
Download for Rust retail.volumetric-weight-1.0.0-rust.fune · 9,605 bytes sha256 156165c0c381358be017c77e2875658ead7d59405bed0b33dad8cb9b21b9588a

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

The whole function, every language, is one file too: retail.volumetric-weight-1.0.0.fune, 12,793 bytes, sha256 aae507e675957ea20e4a1fa476747be9fc726667d41008986e4e2dedfe705a4d. 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 retail.volumetric-weight

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

// fune: after retail.volumetric-weight

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 math.round-div in retail.volumetric-weight

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 retail.volumetric-weight --steps.

// fune: step retail.volumetric-weight 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
a light bulky box is charged on volume, rounded up to the half kilo 400, 300, 200, 2,500, 5,000, 500 → actual grams 2,500, volumetric grams 4,800, chargeable grams 5,000, basis volumetric
a heavy small box is charged on actual weight 400, 300, 200, 6,200, 5,000, 500 → actual grams 6,200, volumetric grams 4,800, chargeable grams 6,500, basis actual
equal weights: the basis is actual 400, 300, 200, 4,800, 5,000, 500 → actual grams 4,800, volumetric grams 4,800, chargeable grams 5,000, basis actual
divisor 6000 makes the same box lighter 400, 300, 200, 2,500, 6,000, 500 → actual grams 2,500, volumetric grams 4,000, chargeable grams 4,000, basis volumetric
divisor 4000 makes it heavier 400, 300, 200, 2,500, 4,000, 500 → actual grams 2,500, volumetric grams 6,000, chargeable grams 6,000, basis volumetric
volumetric grams round up, where floor division gives 7385 333, 333, 333, 1,000, 5,000, 1 → actual grams 1,000, volumetric grams 7,386, chargeable grams 7,386, basis volumetric
billing in whole kilos rounds 7386 g up to 8 kg 333, 333, 333, 1,000, 5,000, 1,000 → actual grams 1,000, volumetric grams 7,386, chargeable grams 8,000, basis volumetric
a tiny parcel still has at least 1 g of volume 10, 10, 10, 100, 5,000, 100 → actual grams 100, volumetric grams 1, chargeable grams 100, basis actual
a weightless parcel is charged on volume, then the minimum step 100, 100, 100, 0, 5,000, 500 → actual grams 0, volumetric grams 200, chargeable grams 500, basis volumetric
dimension order does not matter 200, 400, 300, 2,500, 5,000, 500 → actual grams 2,500, volumetric grams 4,800, chargeable grams 5,000, basis volumetric
Show the other 6 tests
CaseArgumentsExpected
an exact billing step is not rounded further 500, 400, 250, 1,000, 5,000, 500 → actual grams 1,000, volumetric grams 10,000, chargeable grams 10,000, basis volumetric
a pallet-sized parcel stays exact in integers 1,200, 1,000, 1,500, 150,000, 5,000, 1,000 → actual grams 150,000, volumetric grams 360,000, chargeable grams 360,000, basis volumetric
a zero dimension is an error 0, 300, 200, 2,500, 5,000, 500 → error: dimensions must be 1 mm or more
a negative weight is an error 400, 300, 200, -1, 5,000, 500 → error: actualGrams must not be negative
a zero divisor is an error 400, 300, 200, 2,500, 0, 500 → error: divisor must be 1 or more
a zero billing step is an error 400, 300, 200, 2,500, 5,000, 0 → error: roundUpToGrams must be 1 or more

More from the author

## Rounding

Two roundings, both up, both documented because both move money:

1. The volumetric weight is rounded up to the whole gram (7385.2 g is 7386 g). Carriers never round a parcel's size down. 2. The chargeable weight is rounded up to `roundUpToGrams`, the carrier's billing step (most bill in 500 g or 1 kg steps). Pass 1 to leave it at the gram.

When the two weights are equal the basis is `actual`.

## Inputs

Measure the longest, widest and tallest points of the packed parcel, bulges included; that is what the carrier's scanner will do. The order of the three dimensions does not matter. Carriers that measure in centimetres and round each dimension first need that rounding done before calling this.

Files

PathBytes
README.md1,299
impl/python.py1,593
impl/rust.rs2,080
impl/typescript.ts1,453
vectors.json3,036