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::{Version, json, lint, schema, tariff, warning};
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::check(doc, Version::V221).into_parts();
13
14    let report = tariff::lint(&tariff);
15
16    // The two sets are reported separately: `tariff::check` returns the schema walk's warnings,
17    // and the report holds only what linting adds.
18    print_schema_warnings(&warnings);
19    print_lint_warnings(&report.warnings);
20}
21
22/// Print `schema::Warning`s to `stderr`.
23fn print_schema_warnings(warnings: &warning::Set<schema::Warning>) {
24    if warnings.is_empty() {
25        return;
26    }
27
28    eprintln!(
29        "WARN: {} schema warnings from the tariff:\n{:#?}",
30        warnings.len_warnings(),
31        warnings.path_msg_map()
32    );
33}
34
35/// Print `lint::tariff::Warning`s to `stderr`.
36fn print_lint_warnings(warnings: &warning::Set<lint::tariff::Warning>) {
37    if warnings.is_empty() {
38        return;
39    }
40
41    eprintln!(
42        "WARN: {} warnings from the linting:\n{:#?}",
43        warnings.len_warnings(),
44        warnings.path_msg_map()
45    );
46}