ocpi-tariffs 0.55.0

OCPI tariff calculations
Documentation
use std::assert_matches;

use crate::{
    country, json, schema, test,
    warning::{self, Gather as _},
};

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

/// The gatherer adds a second warning on an element after the warning it already holds for that
/// element.
#[test]
fn should_keep_every_warning_raised_on_one_element() {
    test::setup();

    let doc = json::parse_object(TARIFF).unwrap();
    let currency = doc.root().find_field("currency").unwrap().element();

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

    let Ok(caveat) = warnings.into_verdict(Ok(())) else {
        panic!("a successful outcome should become a caveat");
    };
    let at_currency: Vec<_> = caveat.warnings().warnings_at("$.currency").collect();
    assert_matches!(
        at_currency.as_slice(),
        [schema::Warning::NonSpecField, schema::Warning::NullField]
    );
}

/// A `Converting` view converts each warning to the type of the gatherer it wraps.
#[test]
fn should_convert_a_warning_raised_through_a_view() {
    test::setup();

    let doc = json::parse_object(TARIFF).unwrap();
    let country_code = doc.root().find_field("country_code").unwrap().element();

    let mut warnings = warning::Gatherer::<country::Warning>::new();
    warning::Converting::new(&mut warnings)
        .insert(country_code, json::decode::Warning::InvalidEscape(1));

    let Ok(caveat) = warnings.into_verdict(Ok(())) else {
        panic!("a successful outcome should become a caveat");
    };
    let at_country_code: Vec<_> = caveat.warnings().warnings_at("$.country_code").collect();
    assert_matches!(
        at_country_code.as_slice(),
        [country::Warning::Decode(
            json::decode::Warning::InvalidEscape(1)
        )]
    );
}