lint_tariff/
lint_tariff.rs

1#![expect(clippy::unwrap_used, reason = "examples can panic")]
2#![expect(clippy::print_stderr, reason = "examples can log to stderr")]
3
4use ocpi_tariffs::{guess, lint, tariff, warning, Version};
5
6fn main() {
7    const TARIFF_JSON: &str =
8        include_str!("../test_data/v221/real_world/misspelled_field/tariff.json");
9
10    let report = tariff::parse_and_report(TARIFF_JSON).unwrap();
11    let guess::Report {
12        unexpected_fields,
13        version,
14    } = report;
15
16    if !unexpected_fields.is_empty() {
17        eprintln!("Strange... there are fields in the tariff that are not defined in the spec.");
18
19        for path in &unexpected_fields {
20            eprintln!("  * {path}");
21        }
22
23        eprintln!();
24    }
25
26    let tariff = version.certain_or(Version::V221);
27    let report = tariff::lint(&tariff).unwrap();
28
29    print_lint_warnings(&tariff, &report.into_warning_report());
30}
31
32fn print_lint_warnings(
33    tariff: &tariff::Versioned<'_>,
34    warnings: &warning::Report<lint::tariff::WarningKind>,
35) {
36    if warnings.is_empty() {
37        return;
38    }
39
40    eprintln!("WARN: {} warnings from the linting", warnings.len());
41
42    for warning::ElementReport { element, warnings } in warnings.iter(tariff.as_element()) {
43        eprintln!(
44            "Warnings reported for `json::Element` at path: `{}`",
45            element.path()
46        );
47
48        for warning in warnings {
49            eprintln!("  * {warning}");
50        }
51
52        eprintln!();
53    }
54}