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::from_json(doc, Version::V221).into_parts();
13
14    let report = tariff::lint(&tariff);
15
16    // The two sets are reported separately: the schema walk's warnings came back from
17    // `from_json`, and the report holds only what linting added.
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        warning::SetWriter::new(warnings)
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        warning::SetWriter::new(warnings)
45    );
46}