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"
}"#;

/// `contains` has to check every warning of an element, not only the first,
/// because a caller can't know which element the warning it looks for was reported against,
/// or where in that element's list the warning sits.
#[test]
fn should_find_a_warning_behind_another_on_the_same_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(country_code, schema::Warning::NonSpecField);
    warnings.insert(currency, schema::Warning::NullField);
    warnings.insert(currency, schema::Warning::UnexpectedField);

    let contains_unexpected =
        warnings.contains(|warning| matches!(warning, schema::Warning::UnexpectedField));
    assert!(contains_unexpected);
}

#[test]
fn should_not_find_a_warning_the_set_lacks() {
    test::setup();

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

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

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

    let contains_unexpected =
        warnings.contains(|warning| matches!(warning, schema::Warning::UnexpectedField));
    assert!(!contains_unexpected);
}