ocpi-tariffs 0.54.0

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

/// Two `currency` fields, so two elements share the path `$.currency`.
const TARIFF: &str = r#"{
    "party_id": "ENE",
    "currency": "EUR",
    "currency": "USD"
}"#;

/// A `Set` is keyed by `json::ElemId`, but `path_map` is keyed by path. Duplicate JSON keys
/// give several elements the same path, so the map can only hold one of them. The entries are
/// built in `ElemId` order and collected, so the last element in document order wins and the
/// earlier one's warnings are dropped from the map.
///
/// The `Set` itself keeps both, so this loses nothing that the callers iterating groups can
/// see - `cli::print::warning_set` is one of them.
#[test]
fn duplicate_paths_collapse_to_the_last_element() {
    test::setup();

    let doc = json::parse_object(TARIFF).unwrap();
    let fields = doc.root().as_object_fields().unwrap();
    assert_eq!(fields.len(), 3, "one party_id and two currency fields");
    let first_currency = fields[1].element();
    let second_currency = fields[2].element();

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

    // Both warnings are held, against two distinct elements.
    assert_eq!(warnings.len_warnings(), 2);
    assert_eq!(warnings.iter().count(), 2);

    // The path map has a single entry for the shared path, carrying the later element's
    // warnings; the earlier element's are not reachable through it at all.
    let by_path = warnings.path_map();
    assert_eq!(by_path.len(), 1);
    assert_eq!(
        by_path["$.currency"],
        vec![&schema::Warning::UnexpectedField]
    );
}