ocpi-tariffs 0.55.0

OCPI tariff calculations
Documentation
use std::assert_matches;

use crate::{Version, country, json, tariff};

/// `tariff::build` returns every warning it raised before it rejected the given tariff.
#[test]
fn a_rejected_tariff_keeps_the_earlier_warnings() {
    // `build` raises `IncorrectCase` for the `country_code` of `de`, then rejects the
    // tariff because its `elements` array is empty.
    const TARIFF_JSON: &str = r#"{
    "country_code": "de",
    "party_id": "ALL",
    "id": "1",
    "currency": "EUR",
    "type": "REGULAR",
    "elements": [],
    "last_updated": "2018-12-05T12:01:09Z"
}
"#;

    let doc = json::parse_object(TARIFF_JSON).unwrap();
    let tariff = tariff::check(doc, Version::V221).ignore_warnings();
    let failure = tariff::build(tariff).unwrap_err();
    let (error, warnings) = failure.into_parts();

    assert_matches!(error.into_warning(), tariff::Warning::NoElements);
    let (element, _warning) = warnings
        .find(|warning| {
            matches!(
                warning,
                tariff::Warning::Country(country::Warning::IncorrectCase)
            )
        })
        .expect("the rejection should keep the tariff's warning");
    assert_eq!(element.path.as_str(), "$.country_code");
}