Functional Weave
Code in Rust

manufacturing.tolerance-check

Check a measurement against a nominal size and its upper and lower deviations, in whole micrometres.

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

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

What it does

Checks one measurement against a nominal size and its tolerance, the way a go/no-go gauge or an inspection sheet does:

lowerLimit = nominal + lowerDeviation upperLimit = nominal + upperDeviation passed = lowerLimit <= measured <= upperLimit

For example

  • check_tolerance(25,030, 25,000, 50, -50) → passed true, status within, deviation 30, lower limit 24,950, upper limit 25,050, out by 0 symmetric ±0.05 mm at 25 mm: 25.030 passes
  • check_tolerance(25,050, 25,000, 50, -50) → passed true, status within, deviation 50, lower limit 24,950, upper limit 25,050, out by 0 exactly on the upper limit passes: limits are inclusive
  • check_tolerance(25,051, 25,000, 50, -50) → passed false, status above, deviation 51, lower limit 24,950, upper limit 25,050, out by 1 one micrometre over the upper limit is a reject

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 check_tolerance(measured: i64, nominal: i64, upper_deviation: i64, lower_deviation: i64) -> ToleranceCheck
measuredintthe measured size in micrometres (1 mm = 1000)
nominalintthe nominal (basic) size in micrometres
upper_deviationintupper limit minus nominal: +50 for ±0.05 mm, -7 for a g6 shaft at 25 mm
lower_deviationintlower limit minus nominal: -50 for ±0.05 mm, -20 for a g6 shaft at 25 mm
returnsToleranceCheck

The types it declares, generated into your project

/// The verdict, where the limits are, and how far out a reject is.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToleranceCheck {
    /// true when lowerLimit <= measured <= upperLimit; the limits are inside
    pub passed: bool,
    pub status: String,
    /// measured minus nominal, signed
    pub deviation: i64,
    pub lower_limit: i64,
    pub upper_limit: i64,
    /// distance past the limit that was broken; 0 when passed
    pub out_by: i64,
}

// ToleranceStatus is a string in Rust, one of: "within", "above", "below".
// 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!(manufacturing.tolerance-check@^1);  // then call check_tolerance(…)
impl/rust.rs · 77 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

const LIMIT: i64 = 1_000_000_000_000;

fn micrometres(name: &str, value: i64) {
    if !(-LIMIT..=LIMIT).contains(&value) {
        panic!("{} must be a whole number of micrometres within ±10^12, received {}", name, value);
    }
}

/// Check a measurement against nominal plus its upper and lower deviations.
///
/// The deviations are signed and independent, so a tolerance band that lies
/// wholly above or below nominal (an H7 hole, a g6 shaft) is checked
/// correctly; a symmetric band is just `+t, -t`.
///
/// # Panics
/// Panics on a value beyond ±10^12 µm, or a lower deviation above the upper.
pub fn check_tolerance(measured: i64, nominal: i64, upper_deviation: i64, lower_deviation: i64) -> ToleranceCheck {
    micrometres("measured", measured);
    micrometres("nominal", nominal);
    micrometres("upperDeviation", upper_deviation);
    micrometres("lowerDeviation", lower_deviation);
    if lower_deviation > upper_deviation {
        panic!(
            "lowerDeviation must not be greater than upperDeviation, received {} and {}",
            lower_deviation, upper_deviation
        );
    }
    let lower_limit = nominal + lower_deviation;
    let upper_limit = nominal + upper_deviation;
    let deviation = measured - nominal;
    let (passed, status, out_by) = if measured > upper_limit {
        (false, "above", measured - upper_limit)
    } else if measured < lower_limit {
        (false, "below", lower_limit - measured)
    } else {
        (true, "within", 0)
    };
    ToleranceCheck {
        passed,
        status: status.to_string(),
        deviation,
        lower_limit,
        upper_limit,
        out_by,
    }
}

pub fn tolerance_check_to_value(result: &ToleranceCheck) -> Value {
    Value::obj(vec![
        ("passed", Value::Bool(result.passed)),
        ("status", Value::str(&result.status)),
        ("deviation", Value::Int(result.deviation)),
        ("lowerLimit", Value::Int(result.lower_limit)),
        ("upperLimit", Value::Int(result.upper_limit)),
        ("outBy", Value::Int(result.out_by)),
    ])
}

pub fn fune_vector(args: &[Value]) -> Value {
    // Refuse what an i64 cannot hold, with the wording the other languages use.
    let names = ["measured", "nominal", "upperDeviation", "lowerDeviation"];
    for (i, name) in names.iter().enumerate() {
        match &args[i] {
            Value::Int(_) => {}
            Value::Float(f) => panic!("{} must be a whole number of micrometres within ±10^12, received {}", name, f),
            other => panic!("{} must be a whole number of micrometres within ±10^12, received {}", name, other),
        }
    }
    tolerance_check_to_value(&check_tolerance(
        args[0].as_i64(),
        args[1].as_i64(),
        args[2].as_i64(),
        args[3].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 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 manufacturing.tolerance-check
Download for Rust manufacturing.tolerance-check-1.0.0-rust.fune · 10,929 bytes sha256 c6c702d70e3e68d607f65cd81a9dfdc129a2856d0252c2c91a901f88adab00c4

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

The whole function, every language, is one file too: manufacturing.tolerance-check-1.0.0.fune, 14,651 bytes, sha256 02a3faf1c5fd1fc72dfdfa4fe659af7e86632dfe9b9993836204e86a78d84a43. 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 manufacturing.tolerance-check

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

// fune: after manufacturing.tolerance-check

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 manufacturing.tolerance-check --steps.

// fune: step manufacturing.tolerance-check 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
symmetric ±0.05 mm at 25 mm: 25.030 passes 25,030, 25,000, 50, -50 → passed true, status within, deviation 30, lower limit 24,950, upper limit 25,050, out by 0
exactly on the upper limit passes: limits are inclusive 25,050, 25,000, 50, -50 → passed true, status within, deviation 50, lower limit 24,950, upper limit 25,050, out by 0
one micrometre over the upper limit is a reject 25,051, 25,000, 50, -50 → passed false, status above, deviation 51, lower limit 24,950, upper limit 25,050, out by 1
exactly on the lower limit passes 24,950, 25,000, 50, -50 → passed true, status within, deviation -50, lower limit 24,950, upper limit 25,050, out by 0
under the lower limit is a reject by the distance below it 24,930, 25,000, 50, -50 → passed false, status below, deviation -70, lower limit 24,950, upper limit 25,050, out by 20
a g6 shaft at 25 mm measured at exactly nominal is too big: both limits are below nominal 25,000, 25,000, -7, -20 → passed false, status above, deviation 0, lower limit 24,980, upper limit 24,993, out by 7
a g6 shaft at 24.990 mm is inside 24.980 to 24.993 24,990, 25,000, -7, -20 → passed true, status within, deviation -10, lower limit 24,980, upper limit 24,993, out by 0
an H7 hole at 25 mm: nominal itself is the lower limit and passes 25,000, 25,000, 21, 0 → passed true, status within, deviation 0, lower limit 25,000, upper limit 25,021, out by 0
an H7 hole one micrometre under nominal is a reject 24,999, 25,000, 21, 0 → passed false, status below, deviation -1, lower limit 25,000, upper limit 25,021, out by 1
a zero tolerance accepts only the exact size 10,000, 10,000, 0, 0 → passed true, status within, deviation 0, lower limit 10,000, upper limit 10,000, out by 0
Show the other 4 tests
CaseArgumentsExpected
a negative coordinate from a datum, -12.000 mm +0.1/-0.1, measured -12.150 -12,150, -12,000, 100, -100 → passed false, status below, deviation -150, lower limit -12,100, upper limit -11,900, out by 50
a lower deviation above the upper one is an error 25,000, 25,000, -20, -7 → error: lowerDeviation must not be greater than upperDeviation
a fractional micrometre is an error 25,000.5, 25,000, 50, -50 → error: measured must be a whole number of micrometres
a size beyond 10^12 micrometres is an error 25,000, 1,000,000,000,001, 50, -50 → error: nominal must be a whole number of micrometres within ±10^12

More from the author

**Deviations, not a ± number.** Tolerances are given as the upper and lower deviation from nominal, as ISO 286 writes them (`es`/`ei` for shafts, `ES`/`EI` for holes), so both kinds of limit are one call:

- symmetric, 25 mm ± 0.05: `checkTolerance(m, 25000, 50, -50)` - asymmetric, a 25 mm H7 hole (+0.021 / 0): `checkTolerance(m, 25000, 21, 0)` - one-sided, a 25 mm g6 shaft (-0.007 / -0.020): `checkTolerance(m, 25000, -7, -20)`

The last is the case a "within ±tolerance of nominal" check gets wrong: a g6 shaft measured at exactly 25.000 mm is **too big**, 7 µm above its upper limit, because both limits sit below nominal.

**Whole micrometres.** Every size is an integer number of micrometres (µm), so limits and deviations are exact and all three languages agree; a reading of 25.0125 mm is 25013 µm after the gauge's own rounding. Every value must lie within ±10^12 µm (1,000 km), which keeps every sum exact.

**Limits are inclusive.** A part measured exactly on a limit passes. `outBy` is how far past the broken limit a reject is (always positive for a reject, 0 for a pass), and `status` says which side: `above`, `below` or `within`.

The lower deviation may not be greater than the upper one. Negative sizes are allowed, for coordinates and offsets measured from a datum.

Source: ISO 286-1:2010, Geometrical product specifications (GPS) - ISO code system for tolerances on linear sizes, clause 3 (deviations and limits). The H7 and g6 values for 25 mm are from its tables (IT6 = 13 µm, IT7 = 21 µm, fundamental deviation g = -7 µm for sizes over 18 up to 30 mm).

Files

PathBytes
README.md1,882
impl/python.py1,894
impl/rust.rs2,815
impl/typescript.ts1,671
vectors.json3,014