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.warnings);
30}
31
32fn print_lint_warnings(
33    tariff: &tariff::Versioned<'_>,
34    warnings: &warning::Set<lint::tariff::WarningKind>,
35) {
36    if warnings.is_empty() {
37        return;
38    }
39
40    eprintln!(
41        "WARN: {} warnings from the linting:\n {}",
42        warnings.len(),
43        warning::SetWriter::new(tariff.as_element(), warnings)
44    );
45}