ocpi-tariffs 0.43.0

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

use crate::{
    cdr,
    price::{
        self,
        test::{Expect, ExpectFields, UnwrapReport},
    },
    tariff,
    test::{self, IntoFields as _},
    timezone, ObjectType, Version,
};

#[test_each::file(
    glob = "ocpi-tariffs/test_data/v211/real_world/*/cdr*.json",
    name(segments = 2)
)]
fn should_price_cdr(cdr_json: &str, path: &Path) {
    const VERSION: Version = Version::V211;

    test::setup();

    let expect_json = test::read_expect_json(path, "price");
    let expect = test::parse_expect_json::<Expect>(expect_json.as_deref());

    let ExpectFields {
        timezone_find_expect,
        tariff_parse_expect,
        cdr_parse_expect,
        cdr_price_expect,
    } = expect.into_fields();

    let tariff_json = std::fs::read_to_string(path.parent().unwrap().join("tariff.json")).ok();
    let tariff = tariff_json
        .as_deref()
        .map(|json| tariff::parse_with_version(json, VERSION))
        .transpose()
        .unwrap();

    let tariff = if let Some(parse_report) = tariff {
        let tariff::ParseReport {
            tariff,
            unexpected_fields,
        } = parse_report;
        price::test::assert_parse_report(
            ObjectType::Tariff,
            unexpected_fields,
            tariff_parse_expect,
        );
        price::TariffSource::Override(vec![tariff])
    } else {
        assert!(tariff_parse_expect.value.is_none(), "There is no separate tariff to parse so there is no need to define a `tariff_parse` expectation");
        price::TariffSource::UseCdr
    };

    let report = cdr::parse_with_version(cdr_json, VERSION).unwrap();
    let cdr::ParseReport {
        cdr,
        unexpected_fields,
    } = report;
    price::test::assert_parse_report(ObjectType::Cdr, unexpected_fields, cdr_parse_expect);

    let (timezone_source, warnings) = timezone::find_or_infer(&cdr).unwrap().into_parts();

    timezone::test::assert_find_or_infer_outcome(timezone_source, timezone_find_expect, &warnings);

    let report =
        cdr::price(&cdr, tariff, timezone_source.into_timezone()).unwrap_report(cdr.as_json_str());
    price::test::assert_price_report(report, cdr_price_expect);
}