Skip to main content

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::{json, lint, schema, tariff, warning, Version};
5
6fn main() {
7    const TARIFF_JSON: &str = include_str!("data/tariff_misspelled_field.json");
8
9    // Parse the raw JSON and validate it against the `v2.2.1` tariff schema. Any unexpected,
10    // misspelled, missing, or wrongly typed fields are reported as schema warnings.
11    let doc = json::parse_object(TARIFF_JSON).unwrap();
12    let (tariff, warnings) = tariff::build(doc, Version::V221).into_parts();
13
14    print_schema_warnings(&warnings);
15
16    let report = tariff::lint(&tariff);
17
18    print_lint_warnings(&report.warnings);
19}
20
21/// Print `schema::Warning`s to `stderr`.
22fn print_schema_warnings(warnings: &warning::Set<schema::Warning>) {
23    if warnings.is_empty() {
24        return;
25    }
26
27    eprintln!(
28        "WARN: {} schema warnings from the tariff:\n {}",
29        warnings.len_warnings(),
30        warning::SetWriter::new(warnings)
31    );
32}
33
34/// Print `lint::tariff::Warning`s to `stderr`.
35fn print_lint_warnings(warnings: &warning::Set<lint::tariff::Warning>) {
36    if warnings.is_empty() {
37        return;
38    }
39
40    eprintln!(
41        "WARN: {} warnings from the linting:\n {}",
42        warnings.len_warnings(),
43        warning::SetWriter::new(warnings)
44    );
45}