ocpi-tariffs 0.52.0

OCPI tariff calculations
Documentation
#![allow(clippy::missing_panics_doc, reason = "tests are allowed to panic")]
#![allow(clippy::panic, reason = "tests are allowed panic")]

use crate::{
    schema,
    test::{self, ExpectFile, Expectation},
    warning,
};

/// The `warnings_schema` field of an `output_parse__*.json` file.
///
/// Turning a tariff document into a tariff has two stages that report in different
/// vocabularies, so each has its own field and its own view of the file. The schema walk
/// reports [`schema::Warning`]s about the shape of the JSON; see [`DomainExpect`] for the
/// other stage.
#[derive(Debug, serde::Deserialize)]
pub struct SchemaExpect {
    /// What the schema walk in [`tariff::from_json`](crate::tariff::from_json) should report.
    #[serde(default)]
    warnings_schema: Expectation<test::WarningMap>,
}

/// The `warnings_domain` field of an `output_parse__*.json` file.
///
/// This stage reports [`tariff::Warning`](super::Warning)s about what the tariff means, rather
/// than about the shape of the JSON it was read from; see [`SchemaExpect`].
#[derive(Debug, serde::Deserialize)]
pub struct DomainExpect {
    /// What lowering the schema IR into the normalized v221 tariff should report.
    #[serde(default)]
    warnings_domain: Expectation<test::WarningMap>,
}

/// Assert that the schema walk reports the warnings listed under `warnings_schema`.
///
/// Assert this before lowering the IR: lowering rejects an object whose required fields the
/// walk found missing or unusable, and these warnings are the located cause of that.
#[track_caller]
pub(crate) fn assert_schema_warnings(
    warnings: &warning::Set<schema::Warning>,
    expect: ExpectFile<SchemaExpect>,
) {
    let ExpectFile {
        value,
        expect_file_name,
    } = expect;

    // With no expect file the stage may not report anything. Passing `Absent` through rather
    // than returning early makes the panic print the field that would silence these warnings,
    // which can be copied into a new `output_parse__*.json` file.
    let warnings_schema = value
        .map(|SchemaExpect { warnings_schema }| warnings_schema)
        .unwrap_or(Expectation::Absent);

    warning::test::assert_warnings(
        &format!("`warnings_schema` in the `{expect_file_name}` file"),
        warnings,
        warnings_schema,
    );
}

/// Assert that lowering the schema IR reports the warnings listed under `warnings_domain`.
#[track_caller]
pub(crate) fn assert_domain_warnings(
    warnings: &warning::Set<super::Warning>,
    expect: ExpectFile<DomainExpect>,
) {
    let ExpectFile {
        value,
        expect_file_name,
    } = expect;

    let warnings_domain = value
        .map(|DomainExpect { warnings_domain }| warnings_domain)
        .unwrap_or(Expectation::Absent);

    warning::test::assert_warnings(
        &format!("`warnings_domain` in the `{expect_file_name}` file"),
        warnings,
        warnings_domain,
    );
}