Getting started
Install billing, rate a tiered water tariff, attach VAT, and read every EN 16931 total from a document that derived them once.
🔗Install
cargo add billing
# `dec!` lives in rust_decimal itself, behind the `macros` feature.
cargo add --dev rust_decimal --features macros#![no_std] with alloc, no unsafe, no I/O; one runtime dependency, rust_decimal (re-exported as billing::Decimal). MSRV 1.85. The optional serde feature serialises decimals as strings, and deserialising a Document re-runs build().
🔗A first invoice
A three-tier water tariff, billed on 28.5 m³, with 7 % VAT.
use billing::{Amount, AmountScale, Currency, Document, Percent, Price, Tariff, Tier, Tiers, Vat};
use rust_decimal::dec;
let tariff = Tariff::Graduated(Tiers::new([
Tier::up_to(dec!(5), Price::new(dec!(0.80))),
Tier::up_to(dec!(20), Price::new(dec!(1.40))),
Tier::above(Price::new(dec!(2.60))),
])?);
// 5 m³ × 0.80 + 15 m³ × 1.40 + 8.5 m³ × 2.60, one line per tier.
let lines = tariff
.rate(dec!(28.5))?
.into_iter()
.flat_map(|piece| piece.into_lines("water", "MTQ"));
let doc = Document::builder(Currency::EUR, AmountScale::EN16931)
.default_vat(Vat::standard(Percent::new(dec!(7)))?)
.lines(lines)
.build()?;
assert_eq!(doc.lines().len(), 3);
assert_eq!(doc.line_net(2), Amount::parse("22.10")?);
assert_eq!(doc.line_total(), Amount::parse("47.10")?); // BT-106
assert_eq!(doc.vat_total(), Amount::parse("3.30")?); // BT-110: 47.10 × 7 % = 3.297
assert_eq!(doc.payable(), Amount::parse("50.40")?); // BT-115- Currency and scale are required.
AmountScale::EN16931is two decimals, half away from zero. - The unit is a code you choose (
MTQ, UN/ECE Rec. 20); the engine only checks a stated price unit matches it (PEPPOL-EN16931-R130). - VAT is an attribute.
default_vatapplies wherever none is set; the breakdown rounds once per group. - Every check runs in
build(), which returns anErrornaming the rule and the line.
🔗Reading the result
Every total is an accessor returning an Amount<5> that fits the document’s scale; the totals chain maps each to its business term.
use billing::{AmountScale, Currency, Document, DocumentAdjustment, Line, Percent, Price, Quantity, UnitPrice, Vat};
use rust_decimal::dec;
let doc = Document::builder(Currency::EUR, AmountScale::EN16931)
.default_vat(Vat::standard(Percent::new(dec!(19)))?)
.line(Line::new("energy", Quantity::new(dec!(1000), "KWH"), UnitPrice::new(Price::new(dec!(0.30)))))
.adjustment(DocumentAdjustment::allowance("loyalty", dec!(10)))
.build()?;
let s = doc.scale();
let at_scale = |a| s.decimal(a).map(|d| d.to_string());
assert_eq!(at_scale(doc.line_total())?, "300.00"); // BT-106
assert_eq!(at_scale(doc.allowance_total())?, "10.00"); // BT-107
assert_eq!(at_scale(doc.tax_exclusive_total())?, "290.00"); // BT-109
assert_eq!(at_scale(doc.vat_total())?, "55.10"); // BT-110
assert_eq!(at_scale(doc.tax_inclusive_total())?, "345.10"); // BT-112
assert_eq!(at_scale(doc.payable())?, "345.10"); // BT-115
assert_eq!(s.minor_units(doc.payable())?, 34510); // centsscale.decimal hands them out exactly as emitted (Taking amounts out).
🔗Where to go next
| If you want to | Read |
|---|---|
understand Amount<P>, Price, rounding and overflow | Money and rounding |
| rate metered usage, spot intervals, HT/NT or a 95th percentile | Pricing |
| choose VAT categories and exemption reasons | VAT |
| invoice from gross (consumer) prices | Gross prices |
| add discounts, surcharges or a per-unit levy | Allowances, charges and levies |
| map to BT-106 … BT-115 | The totals chain |
| split a bill across tenants or prorate a fee | Splitting |
| issue a credit, merge documents, cap or floor a total | Corrections and bounds |
| deduct advances, cash-round, report VAT in another currency | Settlement |
| compute Skonto or late-payment interest | Payment terms |
| handle errors | Errors |
| upgrade from 0.16 or 0.15 | Migrating |