flexibility 0.4.0

Grid flexibility as exact, replayable arithmetic: limits, baselines, UFTP-exact settlement, and UFTP, EEBUS, S2, OpenADR, CSIP-AUS, IEC 104, CLS.EEDI converters.
Documentation
//! The DSO sends its settlement as canonical text and a digest; the aggregator reads it back,
//! settles from its own meter data, compares one hash, and finds the first disagreement.
//! Run with `cargo run --example verify_a_receipt`.

use flexibility::MeasurementPoint;
use flexibility::baseline::{self, Mbma};
use flexibility::jiff::civil::{date, time};
use flexibility::jiff::tz::TimeZone;
use flexibility::{Deviation, Digest, Grid, IspDuration, Series, Settlement, kw, settle};

fn settle_evening(
    grid: &Grid,
    isp71: flexibility::Power,
) -> Result<Settlement, flexibility::Error> {
    let evening = grid.window(time(17, 0, 0, 0), time(19, 0, 0, 0))?;
    let measured = Series::new(
        grid.clone(),
        grid.numbers()
            .map(|isp| match isp {
                71 => isp71,
                69..=76 => kw!(4.5),
                _ => kw!(7),
            })
            .collect(),
    )?;
    let base = baseline::mbma(&measured, evening, Mbma { n: 4, gap: 1 })?;
    let actual = measured.over(evening)?;
    let ordered = Series::filled(actual.grid().clone(), Deviation::from_watts(-3_000));
    settle(&base, &actual, &ordered, MeasurementPoint::ConnectionPoint)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let berlin = TimeZone::get("Europe/Berlin")?;
    let day = Grid::day(date(2026, 12, 1), &berlin, IspDuration::FIFTEEN_MINUTES)?;

    // The DSO's meter reading for ISP 71 differs from the aggregator's by 100 W.
    let sent = settle_evening(&day, kw!(4.4))?.canonical();
    let published = Digest::of(sent.as_bytes());

    // The aggregator: read what was received, settle from its own data, compare.
    let theirs = Settlement::from_canonical(&sent, berlin.clone())?;
    assert_eq!(
        theirs.digest(),
        published,
        "the digest is of the bytes received"
    );
    let ours = settle_evening(&day, kw!(4.5))?;
    println!("ours   {}\ntheirs {}", ours.digest(), theirs.digest());
    if let Some(difference) = ours.diff(&theirs) {
        println!("first difference: {difference}");
    }

    // An edited copy is refused, naming where.
    let edited = sent.replacen("\"deficiency\":500", "\"deficiency\":0", 1);
    if let Err(e) = Settlement::from_canonical(&edited, berlin) {
        println!("refused: {e}");
    }
    Ok(())
}