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",
    "currency": "EUR",
    "currency": "USD"
}"#;

/// Duplicate keys give several elements one path,
/// and a caller asking about that path can't tell the elements apart,
/// so `warnings_at` has to yield the warnings of every one of them.
#[test]
fn should_yield_the_warnings_of_every_element_at_the_path() {
    test::setup();

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

    let [country_code, first_currency, second_currency] = root.as_object_fields().unwrap() else {
        panic!("The tariff has three fields");
    };

    let mut warnings = warning::Set::<schema::Warning>::new();
    warnings.insert(country_code.element(), schema::Warning::NonSpecField);
    warnings.insert(first_currency.element(), schema::Warning::NullField);
    warnings.insert(second_currency.element(), schema::Warning::DuplicateField);

    let ids: Vec<String> = warnings
        .warnings_at("$.currency")
        .map(|warning| warning.id().as_str().to_owned())
        .collect();
    assert_eq!(ids, ["null_field", "duplicate_field"]);
}