Design

What the crate guarantees and why: pure functions, exact integers, one sign, no defaults, method ids that never change meaning, and refusal over invention.

On this page

A flexibility settlement rarely fails with a crash. It fails with a plausible number the other side cannot reproduce. Each rule below exists to rule out one way that happens.

Pure#

No function reads the clock, the environment, the filesystem, the network or a random source. Instants, calendars, parameters and history are arguments; even the time-zone database is bundled rather than read from the system. The crate's lints and CI hold this.

The consequence is replay: a settlement from two years ago recomputes to the same bytes, on any machine.

Exact#

Power is whole watts in an i64, energy is whole watt-seconds, and an ISP is whole minutes, so power × duration is exact by construction. There is no floating-point arithmetic anywhere in the crate (clippy::float_arithmetic is denied). S2 and IEC 104 send binary floats; their features read each one once, through the decimal the peer wrote, and compute in integers. Literals and text are read from their decimal digits; a value that is not whole watts is refused, never rounded.

Where a division cannot be exact — a mean of days, a ratio adjustment, an average power — the rounding is named (Rounding::Exact, HalfAwayFromZero or TowardZero) and happens once, after summing. All arithmetic is checked: overflow is an error, never a wrap.

One sign#

Consumption is positive, feed-in negative, as in UFTP 3.1 § 4.2.1. Every converter changes sign at its own boundary and nowhere else. A limit is an absolute Power; an order is a Deviation from a baseline. They are different types, so the compiler refuses to mix them.

No defaults#

A baseline method has no parameter that "usually works". n and gap for mbma@1, x, y and the adjustment for xofy@1, the reading of UFTP's DeliveredFlexPower: each is a required argument, because each is a choice the contract makes and the other party has to make the same way.

Method ids never change their meaning#

mbma@1 computes tomorrow exactly what it computed when a settlement named it. Anything that can change an output — the formula, the rounding, the order of operations, how an edge case is refused — is part of the id's meaning. A change is a new id (mbma@2), and the old one stays. The test suite pins the canonical output of every id; a changed snapshot is fixed by a new id, never by a new snapshot. The same holds for documents: each names its profile (flexibility.settlement@1), and a change to its shape is @2.

Refusal over invention#

A missing ISP, too little history, a local time the clocks skip or repeat, a day of the wrong length, a cap below a guarantee: each is an Error naming the values involved, never a plausible substitute. Every error is one line, and Error::hint() adds a remedy where the crate knows one.

Results that show their work#

Every bound in an envelope remembers its Provenance, and explain lists the limits it shadows. Every baseline records its derivation and the digests of its inputs. Every settlement has a canonical text and a digest both sides compute, and diff names the first thing two settlements disagree on.

Check, never solve#

The crate checks: does a plan fit, did delivery meet the order, is a received message consistent. It does not optimise. Finding a schedule, choosing offers or splitting a pool's order across devices has many right answers and belongs to the caller. A check has one.

What it does not do#

  • No market. No order book, clearing, merit order or price; money is the caller's.
  • No device control. The crate says what is allowed; the energy manager and the device decide what happens.
  • No forecast that settles money. A counterfactual the other party cannot rerun exactly may explain a settlement but never computes one.
  • No identity. Provenance is opaque; authentication is the transport's.
  • No I/O. Reading meter data and sending messages is the caller's job.

The terms used here are in the glossary.