flexibility 0.3.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
//! User story 1: one power value, one sign.

use flexibility::{Direction, Error, Power};

#[test]
fn consumption_is_positive() {
    let p = Power::consumption(500).unwrap();
    assert_eq!(p.watts(), 500);
    assert_eq!(p.direction(), Direction::Consumption);
    assert_eq!(p.magnitude(), 500);
}

#[test]
fn feed_in_is_negative() {
    let p = Power::feed_in(500).unwrap();
    assert_eq!(p.watts(), -500);
    assert_eq!(p.direction(), Direction::FeedIn);
    assert_eq!(p.magnitude(), 500);
}

#[test]
fn less_consumption_is_more_feed_in() {
    let less = Power::ZERO
        .checked_sub(Power::consumption(500).unwrap())
        .unwrap();
    let more = Power::ZERO
        .checked_add(Power::feed_in(500).unwrap())
        .unwrap();
    assert_eq!(less, more);
}

#[test]
fn overflow_is_an_error() {
    let max = Power::from_watts(i64::MAX);
    let err = max.checked_add(Power::from_watts(1)).unwrap_err();
    assert!(matches!(err, Error::Overflow { .. }), "{err}");
    assert!(
        Power::from_watts(i64::MIN)
            .checked_sub(Power::from_watts(1))
            .is_err()
    );
}

#[test]
fn magnitudes_beyond_range_are_refused() {
    assert_eq!(
        Power::consumption(u64::MAX).unwrap_err(),
        Error::PowerOutOfRange { watts: u64::MAX }
    );
    assert!(Power::feed_in(u64::MAX).is_err());
    assert_eq!(Power::from_watts(0).direction(), Direction::Zero);
}

#[test]
fn display_names_watts() {
    assert_eq!(Power::feed_in(3_000).unwrap().to_string(), "-3 kW");
    assert_eq!(format!("{:#}", Power::feed_in(3_000).unwrap()), "-3000 W");
}