Rust Sewing Measurement Arithmetic | Sewlore

Sewlore Sewing Math for Rust (sewlore-sewing-math) is a small library and offline command line tool for comparing sewing measurement readings. It keeps each length associated with an explicit unit: millimetres, centimetres or inches. It calculates stretch, signed change after release, recovered extension and independent width and height print-scale errors.

What the library does

The Length type rejects negative, infinite and NaN values. Mixed units are converted to the original length unit for stretch calculations or to the target unit for print-scale comparisons. The code has no external dependencies and no network or storage calls.

This is arithmetic for readings supplied by the caller. It does not measure fabric, establish a material specification, certify a printer or prescribe a print correction. The examples below are hypothetical numbers.

Three readings, three different denominators

Result Calculation
Stretch (%) (stretched − original) / original × 100
Signed residual change (%) (released − original) / original × 100
Recovered extension (%) (stretched − released) / (stretched − original) × 100

For an original reading of 10 cm, a stretched reading of 140 mm and a released reading of 10.5 cm, the results are 40% stretch, +5% residual change and 87.5% recovered extension. When stretched and original are equal, recovered extension has no denominator: the Rust API returns None and the CLI returns JSON null.

A released reading outside the original-to-stretched interval is retained and flagged. The code does not clamp recovery into 0–100% or hide a negative residual change.

Rust API example

use sewlore_sewing_math::{stretch, Length, Unit};
fn main() -> Result<(), sewlore_sewing_math::Error> {
    let reading = stretch(
        Length::new(10.0, Unit::Centimetre)?,
        Length::new(140.0, Unit::Millimetre)?,
        Length::new(10.5, Unit::Centimetre)?,
    )?;
    assert_eq!(reading.stretch_percent, 40.0);
    assert_eq!(reading.residual_change_percent, 5.0);
    assert_eq!(reading.recovered_extension_percent, Some(87.5));
    Ok(())
}

Compare both axes of a print test

The print-scale function compares width and height against one target side. Each signed error is (measured − target) / target × 100. A hypothetical 100 mm target measured as 95 mm wide and 101 mm high has −5% X error and +1% Y error. Averaging the axes would conceal the different errors, so both are returned.

The caller supplies a tolerance. within_tolerance checks both absolute errors; non_uniform independently checks their difference in percentage points. Target, width and height must be positive. The library compares readings; it does not choose new print settings.

Local command line use

sewlore-measure stretch 10cm 140mm 10.5cm
sewlore-measure print-scale 100mm 95mm 101mm 0.5
sewlore-measure convert 1in mm

The CLI emits one JSON line on success. Invalid input writes a message to standard error, leaves standard output empty and exits with status 2. Lengths require a unit suffix and numbers use a dot decimal separator. One inch is exactly 25.4 mm.

Source, limits and related methods

The implementation uses ordinary floating-point arithmetic. Overflow and positive-value conversion underflow return a numeric-range error. It provides neither exact decimal arithmetic nor estimates of physical measurement uncertainty.

Read the Rust source, installation instructions and executable examples. Formula definitions derive from Sewlore's MIT-licensed sewing math package; upstream notices are preserved. The library is MIT-licensed, and using it does not require links in generated results.

For recording the physical procedure around these calculations, see Sewlore's fabric measurement methods guide.