ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
use crate::{json, schema, test, warning};

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

/// A caller compares the IDs with a list of the IDs it accepts,
/// and an ID left out would let a warning through unchecked.
/// So `ids` has to list the ID of every warning on an element, not only the first.
#[test]
fn should_list_every_id_once() {
    test::setup();

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

    let country_code = root.find_field("country_code").unwrap().element();
    let currency = root.find_field("currency").unwrap().element();

    let mut warnings = warning::Set::<schema::Warning>::new();
    warnings.insert(country_code, schema::Warning::NonSpecField);
    warnings.insert(country_code, schema::Warning::UnexpectedField);
    warnings.insert(currency, schema::Warning::NullField);
    warnings.insert(currency, schema::Warning::UnexpectedField);

    let ids = warnings.ids();
    let ids: Vec<&str> = ids.iter().map(warning::Id::as_str).collect();
    assert_eq!(ids, ["non_spec_field", "null_field", "unexpected_field"]);
}

/// `missing_field(..)` includes the field name,
/// and a caller reads each such ID as one missing field.
/// So `ids` has to keep one ID for each missing field rather than one for the variant.
#[test]
fn should_list_one_id_for_each_missing_field() {
    test::setup();

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

    let mut warnings = warning::Set::<schema::Warning>::new();
    warnings.insert(
        root,
        schema::Warning::MissingField {
            name: "start_date_time",
        },
    );
    warnings.insert(
        root,
        schema::Warning::MissingField {
            name: "last_updated",
        },
    );

    let ids = warnings.ids();
    let ids: Vec<&str> = ids.iter().map(warning::Id::as_str).collect();
    assert_eq!(
        ids,
        [
            "missing_field(last_updated)",
            "missing_field(start_date_time)"
        ]
    );
}