ocpi-tariffs 0.55.0

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

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

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

    price_cdr(Version::V211, cdr_json, path);
}

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

    price_cdr(Version::V221, cdr_json, path);
}

#[track_caller]
fn price_cdr(version: Version, cdr_json: &str, path: &Path) {
    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_check_expect,
        tariff_build_expect,
        cdr_check_expect,
        cdr_build_expect,
        cdr_tariffs_expect,
        cdr_price_expect,
    } = expect.into_fields();

    let tariff_json = std::fs::read_to_string(path.parent().unwrap().join("tariff.json")).ok();
    let tariff = if let Some(tariff_json) = tariff_json.as_deref() {
        let doc = crate::json::parse_object(tariff_json).unwrap();
        let (tariff, warnings) = tariff::check(doc, version).into_parts();
        price::test::assert_step_warnings("tariff_check.warnings", &warnings, tariff_check_expect);

        let (tariff, warnings) = tariff::build(tariff)
            .unwrap_or_else(|failure| panic!("Unable to build the tariff:\n{failure:#?}"))
            .into_parts();
        price::test::assert_step_warnings("tariff_build.warnings", &warnings, tariff_build_expect);

        Some(tariff)
    } else {
        assert!(
            tariff_check_expect.value.is_none(),
            "There is no separate tariff to check so there is no need to define a `tariff_check` expectation"
        );
        assert!(
            tariff_build_expect.value.is_none(),
            "There is no separate tariff to build so there is no need to define a `tariff_build` expectation"
        );
        None
    };

    let cdr_json = test::strip_comment_lines(cdr_json);
    let doc = crate::json::parse_object(&cdr_json).unwrap();
    let (cdr, warnings) = cdr::check(doc, version).into_parts();
    price::test::assert_step_warnings("cdr_check.warnings", &warnings, cdr_check_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 (cdr, warnings) = cdr::build(cdr)
        .unwrap_or_else(|failure| panic!("Unable to build the CDR:\n{failure:#?}"))
        .into_parts();
    price::test::assert_step_warnings("cdr_build.warnings", &warnings, cdr_build_expect);

    let tariffs = if let Some(tariff) = tariff {
        assert!(
            cdr_tariffs_expect.value.is_none(),
            "The CDR is priced with a separate tariff, so there is no need to define a `cdr_tariffs` expectation"
        );
        vec![tariff]
    } else {
        let (tariffs, warnings) = cdr
            .tariffs()
            .unwrap_or_else(|failure| panic!("Unable to build the CDR's tariffs:\n{failure:#?}"))
            .into_parts();
        price::test::assert_step_warnings("cdr_tariffs.warnings", &warnings, cdr_tariffs_expect);
        tariffs
    };

    // The v221 tariff location does not contain a timezone field, this timezone should be
    // used from the `Location`.
    //
    // The tariff's time related fields are in UTC and can be converted to local time by using
    // the timezone from the `Location` object.
    //
    // > `start_date_time`:
    // >
    // > The time when this tariff becomes active, in UTC, time_zone field of the Location can be used to convert to local time.
    // >
    // > See: <https://github.com/ocpi/ocpi/blob/a57ecb624fbe0f19537ac7956a11f3019a65018f/mod_tariffs.asciidoc#mod_tariffs_tariff_object>
    //
    // See: <https://github.com/ocpi/ocpi/blob/a57ecb624fbe0f19537ac7956a11f3019a65018f/mod_locations.asciidoc>
    let report = cdr::price(&cdr, &tariffs, &timezone_source.into_timezone())
        .unwrap_report(cdr.as_json_str());
    price::test::assert_price_report(report, cdr_price_expect);
}