ocpi-tariffs 0.45.0

OCPI tariff calculations
Documentation
use std::path::Path;

use assert_matches::assert_matches;

use crate::{
    guess,
    json::FromJson,
    tariff,
    test::{self, VersionedType},
    Versioned,
};

#[test_each::file(
    glob = "ocpi-tariffs/test_data/v211/real_world/*/tariff*.json",
    name(segments = 2)
)]
fn should_parse_v211_tariff(tariff_json: &str, path: &Path) {
    test::setup();

    parse_tariff::<super::v211::Tariff<'_>>(tariff_json, path);
}

#[test_each::file(
    glob = "ocpi-tariffs/test_data/v221/real_world/*/tariff*.json",
    name(segments = 2)
)]
fn should_parse_v221_tariff(tariff_json: &str, path: &Path) {
    test::setup();

    parse_tariff::<super::v221::Tariff<'_>>(tariff_json, path);
}

#[track_caller]
fn parse_tariff<'buf, T>(tariff_json: &'buf str, path: &Path)
where
    T: VersionedType + FromJson<'buf, Warning = tariff::Warning>,
{
    {
        // Assert that all the tariffs in the real_world folder are inferred/guessed correctly.
        let report = tariff::parse_and_report(tariff_json).unwrap();
        let tariff = assert_matches!(&report.version, guess::Version::Certain(tariff) => tariff);
        assert_eq!(tariff.version(), T::VERSION);
    }

    let expect_json = test::read_expect_json(path, "parse");

    let tariff::ParseReport {
        tariff: tariff_elem,
        unexpected_fields,
    } = tariff::parse_with_version(tariff_json, T::VERSION).unwrap();

    {
        let parse_expect = test::parse_expect_json(expect_json.as_deref());
        tariff::test::assert_parse_report(unexpected_fields, parse_expect);
    }

    let tariff = T::from_json(tariff_elem.as_element()).unwrap();
    let (_tariff, warnings) = tariff.into_parts();
    drop(tariff_elem);

    let from_json_expect = test::parse_expect_json(expect_json.as_deref());
    tariff::test::assert_from_json_warnings(&warnings, from_json_expect);
}