ocpi-tariffs 0.53.0

OCPI tariff calculations
Documentation
use crate::{
    json, schema, test,
    warning::{self, Warning as _},
};

const TARIFF: &str = r#"{
    "party_id": "ENE",
    "currency": "EUR"
}"#;

/// A failing operation reports the warning that stopped it.
#[test]
fn the_error_is_returned_when_nothing_else_was_collected() {
    test::setup();

    let doc = json::parse_object(TARIFF).unwrap();
    let root = doc.root();

    let warnings = warning::Set::new();
    let verdict: crate::Verdict<(), schema::Warning> =
        warnings.bail(root, schema::Warning::NullField);

    let error = verdict.unwrap_err().unwrap();

    assert_eq!(error.element().path.as_str(), "$");
    assert_eq!(error.into_warning().id().as_str(), "null_field");
}

/// The warnings collected before the bail say something the bail does not, so dropping them
/// silently is what this asserts against.
#[test]
#[should_panic(expected = "unexpected_field")]
fn collected_warnings_fail_the_unwrap() {
    test::setup();

    let doc = json::parse_object(TARIFF).unwrap();
    let root = doc.root();
    let party_id = root.find_field("party_id").unwrap().element();

    let mut warnings = warning::Set::new();
    warnings.insert(party_id, schema::Warning::UnexpectedField);

    let verdict: crate::Verdict<(), schema::Warning> =
        warnings.bail(root, schema::Warning::NullField);

    let _error = verdict.unwrap_err().unwrap();
}