ocpi-tariffs 0.52.0

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

use crate::{
    guess, tariff,
    test::{self},
    Version, Versioned as _,
};

#[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(tariff_json, path, Version::V211);
}

#[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(tariff_json, path, Version::V221);
}

#[track_caller]
fn parse_tariff(tariff_json: &str, path: &Path, version: Version) {
    {
        // Assert that all the tariffs in the real_world folder are inferred/guessed correctly.
        let doc = crate::json::parse_object(tariff_json).unwrap();
        let guess = tariff::infer_version(doc);
        let guess::Version::Certain(tariff) = &guess else {
            panic!("expected a certain version, got {guess:?}");
        };
        assert_eq!(tariff.version(), version);
    }

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

    let doc = crate::json::parse_object(tariff_json).unwrap();
    let (tariff_ir, schema_warnings) = tariff::from_json(doc, version).into_parts();

    {
        let schema_expect = test::parse_expect_json(expect_json.as_deref());
        tariff::test::assert_schema_warnings(&schema_warnings, schema_expect);
    }

    let tariff = tariff_ir.to_v221().unwrap();
    let (_tariff, domain_warnings) = tariff.into_parts();

    let domain_expect = test::parse_expect_json(expect_json.as_deref());
    tariff::test::assert_domain_warnings(&domain_warnings, domain_expect);
}