ocpi-tariffs 0.54.0

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

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

/// A caller reads each pair as "this warning is about this element", so
/// `iter_warnings` has to yield every warning of an element, each paired with that element.
#[test]
fn should_yield_every_warning_paired_with_its_own_element() {
    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(currency, schema::Warning::NullField);
    warnings.insert(currency, schema::Warning::UnexpectedField);
    warnings.insert(country_code, schema::Warning::NonSpecField);

    let pairs: Vec<(&str, String)> = warnings
        .iter_warnings()
        .map(|(element, warning)| (element.path.as_str(), warning.id().as_str().to_owned()))
        .collect();

    assert_eq!(
        pairs,
        [
            ("$.country_code", "non_spec_field".to_owned()),
            ("$.currency", "null_field".to_owned()),
            ("$.currency", "unexpected_field".to_owned()),
        ]
    );
}