ocpi-tariffs 0.52.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"
}"#;

/// Warnings are grouped by the `json::Element` they were reported against, and the groups
/// are emitted in `ElemId` order - the order the elements appear in the document.
///
/// The warnings are inserted directly rather than obtained from a feature, so this test
/// covers `Set` alone and does not move when a feature changes what it reports.
#[test]
fn should_group_by_elem() {
    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();

    // Insert against `currency` first, to prove the iteration order comes from the element's
    // position in the document and not from insertion order.
    warnings.insert(currency, schema::Warning::NullField);
    warnings.insert(currency, schema::Warning::UnexpectedField);
    warnings.insert(country_code, schema::Warning::NonSpecField);

    let mut iter = warnings.iter();

    let (country_code_elem, country_code_warnings) = iter.next().unwrap().to_parts();
    assert_eq!(country_code_elem.path.as_str(), "$.country_code");
    let ids: Vec<String> = country_code_warnings
        .iter()
        .map(|w| w.id().as_str().to_owned())
        .collect();
    assert_eq!(ids, ["non_spec_field"]);

    let (currency_elem, currency_warnings) = iter.next().unwrap().to_parts();
    assert_eq!(currency_elem.path.as_str(), "$.currency");
    // Two warnings reported against one element share a single group.
    let ids: Vec<String> = currency_warnings
        .iter()
        .map(|w| w.id().as_str().to_owned())
        .collect();
    assert_eq!(ids, ["null_field", "unexpected_field"]);

    assert!(iter.next().is_none(), "No more warning groups expected");

    // `country_code` appears earlier in the JSON document, so it holds the lower `ElemId`.
    assert!(country_code_elem.id < currency_elem.id);
}