Skip to main content

Set

Struct Set 

Source
pub struct Set<W: Warning>(/* private fields */);
Expand description

A set of Warnings transported through the system using a Caveat.

Implementations§

Source§

impl Set<Warning>

Source

pub fn unexpected_fields(&self) -> PathSet<'_>

Collect the field paths of all Warning::UnexpectedField warnings into a set of json::Paths.

Source

pub fn non_spec_fields(&self) -> PathSet<'_>

Collect the field paths of all Warning::NonSpecField warnings into a set of json::Paths.

Source

pub fn missing_fields(&self) -> PathSet<'_>

Collect the field paths of all Warning::MissingField warnings into a set of json::Paths.

Source

pub fn remove_unexpected_fields(&mut self)

Remove all Warning::UnexpectedField warnings from the set.

Source

pub fn remove_missing_fields(&mut self)

Remove all Warning::MissingField warnings from the set.

Source

pub fn remove_invalid_types(&mut self)

Remove all Warning::InvalidType warnings from the set.

Source

pub fn remove_null_fields(&mut self)

Remove all Warning::NullField warnings from the set.

Source

pub fn remove_cardinalities(&mut self)

Remove all Warning::Cardinality warnings from the set.

Source

pub fn remove_string_too_longs(&mut self)

Remove all Warning::StringTooLong warnings from the set.

Source§

impl<W: Warning> Set<W>

Source

pub fn remove_rejected(&mut self)

Remove every Rejected marker from the set (see Warning::is_rejected).

The schema walk already reported the located structural cause, so a consumer that has those warnings can drop these follow-on markers.

Source

pub fn retain<F>(&mut self, keep: F)
where F: FnMut(&W) -> bool,

Retain only the Warnings for which keep returns true.

Any Group left without warnings is removed from the set.

Source

pub fn is_empty(&self) -> bool

Return true if the Warning set is empty.

Examples found in repository?
examples/lint_tariff.rs (line 24)
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}
More examples
Hide additional examples
examples/price_cdr_with_known_version.rs (line 14)
6fn main() {
7    const CDR_JSON: &str = include_str!("data/cdr_time_and_parking_time.json");
8
9    // If you know the version of a CDR you can parse it into a `json::Document`, check it
10    // against the schema for that version and then build it.
11    let doc = json::parse_object(CDR_JSON).expect("unable to parse CDR");
12    let (cdr, schema_warnings) = cdr::check(doc, Version::V211).into_parts();
13
14    if !schema_warnings.is_empty() {
15        eprintln!(
16            "The CDR has `{}` schema warnings.",
17            schema_warnings.len_warnings()
18        );
19    }
20
21    let (cdr, warnings) = cdr::build(cdr)
22        .expect("unable to build the CDR")
23        .into_parts();
24
25    if !warnings.is_empty() {
26        eprintln!("The CDR has `{}` warnings.", warnings.len_warnings());
27    }
28
29    let (tariffs, warnings) = cdr
30        .tariffs()
31        .expect("unable to build the tariffs the CDR contains")
32        .into_parts();
33
34    if !warnings.is_empty() {
35        eprintln!(
36            "The tariffs the CDR contains have `{}` warnings.",
37            warnings.len_warnings()
38        );
39    }
40
41    let report = match cdr::price(&cdr, &tariffs, &jiff::tz::get!("Europe/Amsterdam")) {
42        Ok(r) => r,
43        Err(failure) => {
44            let (error, warnings) = failure.into_parts();
45            print_pricing_error(&error);
46            print_pricing_warnings(&warnings);
47            return;
48        }
49    };
50
51    let (report, warnings) = report.into_parts();
52    print_pricing_warnings(&warnings);
53
54    // The various fields of the `price::Report` can be examined or converted to JSON.
55    let price::Report {
56        periods: _,
57        tariff_used: _,
58        timezone: _,
59        billed_energy: _,
60        billed_idle_time: _,
61        total_charging_time: _,
62        billed_charging_time: _,
63        total_cost: _,
64        total_fixed_cost: _,
65        total_time: _,
66        total_charging_time_cost: _,
67        total_energy: _,
68        total_energy_cost: _,
69        total_idle_time: _,
70        total_idle_cost: _,
71    } = report;
72}
73
74/// Print `price::Warning` that halted the example to `stderr`.
75fn print_pricing_error(error: &warning::Error<price::Warning>) {
76    eprintln!(
77        "ERR: Unable to price CDR due to error at path `{}`: {}",
78        error.element().path,
79        error.warning()
80    );
81}
82
83/// Print `price::Warning`s to `stderr`.
84fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
85    if warnings.is_empty() {
86        return;
87    }
88
89    eprintln!(
90        "WARN: {} warnings from the pricing:\n{:#?}",
91        warnings.len_warnings(),
92        warnings.path_msg_map()
93    );
94}
examples/price_cdr_with_unknown_version.rs (line 33)
6fn main() {
7    const CDR_JSON: &str = include_str!("data/cdr_time_and_parking_time.json");
8
9    // First the raw JSON is parsed into a `json::Document`,
10    // and `cdr::infer_version` guesses the CDR's OCPI version from its fields.
11    let cdr = json::parse_object(CDR_JSON).expect("Unable to parse CDR JSON");
12    let cdr = cdr::infer_version(cdr);
13
14    // The guess can be either certain or uncertain.
15    // If the version is uncertain then fallback to presuming the CDR is v211.
16    let cdr = cdr.certain_or(Version::V211);
17    let version = cdr.version();
18    let cdr = cdr::check(cdr.into_doc(), version).ignore_warnings();
19
20    // The timezone can be inferred or found in the CDR, but a checked CDR is required.
21    let timezone = match timezone::find_or_infer(&cdr) {
22        Ok(tz) => tz,
23        Err(failure) => {
24            let (error, warnings) = failure.into_parts();
25            eprintln!("Unable to infer timezone");
26            print_timezone_error(&error);
27            print_timezone_warnings(&warnings);
28            return;
29        }
30    };
31    let (timezone_source, warnings) = timezone.into_parts();
32
33    if !warnings.is_empty() {
34        print_timezone_warnings(&warnings);
35    }
36
37    // We don't care whether the timezone was found or inferred.
38    let timezone = timezone_source.into_timezone();
39
40    let cdr = cdr::build(cdr)
41        .expect("unable to build the CDR")
42        .ignore_warnings();
43
44    let tariffs = cdr
45        .tariffs()
46        .expect("unable to build the tariffs the CDR contains")
47        .ignore_warnings();
48
49    let report = match cdr::price(&cdr, &tariffs, &timezone) {
50        Ok(r) => r,
51        Err(failure) => {
52            let (error, warnings) = failure.into_parts();
53            print_pricing_error(&error);
54            print_pricing_warnings(&warnings);
55            return;
56        }
57    };
58
59    let (report, warnings) = report.into_parts();
60
61    print_pricing_warnings(&warnings);
62
63    // The various fields of the `price::Report` can be examined or converted to JSON.
64    let price::Report {
65        periods: _,
66        tariff_used: _,
67        timezone: _,
68        billed_energy: _,
69        billed_idle_time: _,
70        total_charging_time: _,
71        billed_charging_time: _,
72        total_cost: _,
73        total_fixed_cost: _,
74        total_time: _,
75        total_charging_time_cost: _,
76        total_energy: _,
77        total_energy_cost: _,
78        total_idle_time: _,
79        total_idle_cost: _,
80    } = report;
81}
82
83/// Print the `timezone::Warning` that halted the example to `stderr`.
84fn print_timezone_error(error: &warning::Error<timezone::Warning>) {
85    eprintln!(
86        "ERR: Unable to find timezone due to error at path `{}`: {}",
87        error.element().path,
88        error.warning()
89    );
90}
91
92/// Print the `timezone::Warning`s to `stderr`.
93fn print_timezone_warnings(warnings: &warning::Set<timezone::Warning>) {
94    if warnings.is_empty() {
95        return;
96    }
97
98    eprintln!(
99        "WARN: {} warnings from the timezone search:\n{:#?}",
100        warnings.len_warnings(),
101        warnings.path_msg_map()
102    );
103}
104
105/// Print the `price::Warning` that halted the process to `stderr`.
106fn print_pricing_error(error: &warning::Error<price::Warning>) {
107    eprintln!(
108        "ERR: Unable to price CDR due to error at path `{}`: {}",
109        error.element().path,
110        error.warning()
111    );
112}
113
114/// Print the `price::Warning`s to `stderr`.
115fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
116    if warnings.is_empty() {
117        return;
118    }
119
120    eprintln!(
121        "WARN: {} warnings from the pricing:\n{:#?}",
122        warnings.len_warnings(),
123        warnings.path_msg_map()
124    );
125}
examples/generate_cdr_from_tariff.rs (line 24)
11fn main() {
12    const TARIFF_JSON: &str = include_str!("data/tariff_time_and_parking_time_separate.json");
13
14    // Parse the raw JSON into a `json::Document` and guess the OCPI version of the tariff, falling
15    // back to `v2.2.1` when the version is uncertain.
16    let doc = json::parse_object(TARIFF_JSON).unwrap();
17    let tariff = tariff::infer_version(doc).certain_or(Version::V221);
18    let version = tariff.version();
19
20    // Check the tariff against the OCPI tariff schema. When `tariff::build` rejects the tariff,
21    // a schema warning names the field it couldn't use.
22    let (tariff, schema_warnings) = tariff::check(tariff.into_doc(), version).into_parts();
23
24    if !schema_warnings.is_empty() {
25        eprintln!(
26            "WARN: {} warnings from checking the tariff:\n{:#?}",
27            schema_warnings.len_warnings(),
28            schema_warnings.path_msg_map()
29        );
30    }
31
32    let tariff = match tariff::build(tariff) {
33        Ok(tariff) => tariff.ignore_warnings(),
34        Err(failure) => {
35            let (error, warnings) = failure.into_parts();
36            eprintln!(
37                "ERR: Unable to build the tariff due to error at path `{}`: {}",
38                error.element().path,
39                error.warning()
40            );
41            eprintln!(
42                "WARN: {} warnings from building the tariff:\n{:#?}",
43                warnings.len_warnings(),
44                warnings.path_msg_map()
45            );
46            return;
47        }
48    };
49
50    let config = generate::Config {
51        timezone: jiff::tz::get!("Europe/Amsterdam"),
52        start_date_time: Timestamp::from_str("2025-06-12 18:22:33+00:00").unwrap(),
53        end_date_time: Timestamp::from_str("2025-06-12 22:33:44+00:00").unwrap(),
54        max_current_supply_amp: Decimal::from(4),
55        requested_kwh: Decimal::from(24),
56        max_power_supply_kw: Decimal::from(80),
57    };
58    let report = match cdr::generate_from_tariff(&tariff, &config) {
59        Ok(r) => r,
60        Err(failure) => {
61            let (error, warnings) = failure.into_parts();
62            print_error(&error);
63            print_warnings(&warnings);
64            return;
65        }
66    };
67    let (report, warnings) = report.into_parts();
68
69    print_warnings(&warnings);
70
71    let generate::Report {
72        tariff_id,
73        tariff_currency_code,
74        partial_cdr,
75    } = report;
76
77    println!(
78        "CDR generated for tariff with id: `{tariff_id}` and currency code: `{tariff_currency_code}`"
79    );
80    println!("{partial_cdr:#?}");
81}
82
83/// Print `generate::Warning` that halted the example to `stderr`.
84fn print_error(error: &warning::Error<generate::Warning>) {
85    eprintln!(
86        "ERR: Unable to generate CDR due to error at path `{}`: {}",
87        error.element().path,
88        error.warning()
89    );
90}
91
92/// Print `generate::Warning`s to `stderr`.
93fn print_warnings(warnings: &warning::Set<generate::Warning>) {
94    if warnings.is_empty() {
95        return;
96    }
97
98    eprintln!(
99        "WARN: {} warnings from generating the CDR:\n{:#?}",
100        warnings.len_warnings(),
101        warnings.path_msg_map()
102    );
103}
Source

pub fn iter_warnings(&self) -> impl Iterator<Item = (&Element, &W)>

Return an iterator over every Warning in the set, each paired with the Element it was reported against.

This is Set::iter with each Group flattened into its warnings. The warnings come in document order of their elements, and in the order they were inserted for one element. A predicate on one warning goes straight into filter or find:

fn paths_outside_cdr(warnings: &warning::Set<cdr::Warning>) -> Vec<&str> {
    warnings
        .iter_warnings()
        .filter(|(_element, warning)| {
            matches!(
                warning,
                cdr::Warning::PeriodsOutsideStartEndDateTime { .. }
            )
        })
        .map(|(element, _warning)| element.path.as_str())
        .collect()
}
Source

pub fn contains<F>(&self, predicate: F) -> bool
where F: FnMut(&W) -> bool,

Return true if any Warning in the set makes predicate return true.

A warning can hold fields, so whether two warnings are the same depends on which fields a caller cares about. The predicate leaves that choice to the caller, who usually looks for a variant with matches!:

fn periods_state_no_time(warnings: &warning::Set<price::Warning>) -> bool {
    warnings.contains(|warning| matches!(warning, price::Warning::PeriodsStateNoTime))
}
Source

pub fn find<F>(&self, predicate: F) -> Option<(&Element, &W)>
where F: FnMut(&W) -> bool,

Return the first Warning in the set that makes predicate return true, paired with the Element it was reported against.

“First” means first in the order Set::iter_warnings yields. The Element tells a caller where in the document the warning sits:

fn path_of_periods_state_no_time(warnings: &warning::Set<price::Warning>) -> Option<&str> {
    let (element, _warning) =
        warnings.find(|warning| matches!(warning, price::Warning::PeriodsStateNoTime))?;
    Some(element.path.as_str())
}
Source

pub fn ids(&self) -> BTreeSet<Id>

Return every distinct Id of the Warnings in the set.

A caller can compare the returned IDs with a list of the IDs it accepts, rather than matching on the variants of each warning.

Some IDs include a value from the warning, such as the field name in missing_field(country_code). Two warnings of one variant can then give two IDs.

Source

pub fn warnings_at<'set>( &'set self, path: &str, ) -> impl Iterator<Item = &'set W>

Return an iterator over the Warnings reported against the element at path, such as $.charging_periods.

Duplicate JSON keys give several elements the same path. The iterator yields the warnings of every one of them, in document order, where Set::path_map would keep only the last.

Source

pub fn len_elements(&self) -> usize

Return the amount of Elements in this set.

Each Element can have many Warnings associated with it.

Source

pub fn len_warnings(&self) -> usize

Return the total amount of Warnings in this set for all Elements.

Examples found in repository?
examples/lint_tariff.rs (line 30)
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}
More examples
Hide additional examples
examples/price_cdr_with_unknown_version.rs (line 100)
93fn print_timezone_warnings(warnings: &warning::Set<timezone::Warning>) {
94    if warnings.is_empty() {
95        return;
96    }
97
98    eprintln!(
99        "WARN: {} warnings from the timezone search:\n{:#?}",
100        warnings.len_warnings(),
101        warnings.path_msg_map()
102    );
103}
104
105/// Print the `price::Warning` that halted the process to `stderr`.
106fn print_pricing_error(error: &warning::Error<price::Warning>) {
107    eprintln!(
108        "ERR: Unable to price CDR due to error at path `{}`: {}",
109        error.element().path,
110        error.warning()
111    );
112}
113
114/// Print the `price::Warning`s to `stderr`.
115fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
116    if warnings.is_empty() {
117        return;
118    }
119
120    eprintln!(
121        "WARN: {} warnings from the pricing:\n{:#?}",
122        warnings.len_warnings(),
123        warnings.path_msg_map()
124    );
125}
examples/price_cdr_with_known_version.rs (line 17)
6fn main() {
7    const CDR_JSON: &str = include_str!("data/cdr_time_and_parking_time.json");
8
9    // If you know the version of a CDR you can parse it into a `json::Document`, check it
10    // against the schema for that version and then build it.
11    let doc = json::parse_object(CDR_JSON).expect("unable to parse CDR");
12    let (cdr, schema_warnings) = cdr::check(doc, Version::V211).into_parts();
13
14    if !schema_warnings.is_empty() {
15        eprintln!(
16            "The CDR has `{}` schema warnings.",
17            schema_warnings.len_warnings()
18        );
19    }
20
21    let (cdr, warnings) = cdr::build(cdr)
22        .expect("unable to build the CDR")
23        .into_parts();
24
25    if !warnings.is_empty() {
26        eprintln!("The CDR has `{}` warnings.", warnings.len_warnings());
27    }
28
29    let (tariffs, warnings) = cdr
30        .tariffs()
31        .expect("unable to build the tariffs the CDR contains")
32        .into_parts();
33
34    if !warnings.is_empty() {
35        eprintln!(
36            "The tariffs the CDR contains have `{}` warnings.",
37            warnings.len_warnings()
38        );
39    }
40
41    let report = match cdr::price(&cdr, &tariffs, &jiff::tz::get!("Europe/Amsterdam")) {
42        Ok(r) => r,
43        Err(failure) => {
44            let (error, warnings) = failure.into_parts();
45            print_pricing_error(&error);
46            print_pricing_warnings(&warnings);
47            return;
48        }
49    };
50
51    let (report, warnings) = report.into_parts();
52    print_pricing_warnings(&warnings);
53
54    // The various fields of the `price::Report` can be examined or converted to JSON.
55    let price::Report {
56        periods: _,
57        tariff_used: _,
58        timezone: _,
59        billed_energy: _,
60        billed_idle_time: _,
61        total_charging_time: _,
62        billed_charging_time: _,
63        total_cost: _,
64        total_fixed_cost: _,
65        total_time: _,
66        total_charging_time_cost: _,
67        total_energy: _,
68        total_energy_cost: _,
69        total_idle_time: _,
70        total_idle_cost: _,
71    } = report;
72}
73
74/// Print `price::Warning` that halted the example to `stderr`.
75fn print_pricing_error(error: &warning::Error<price::Warning>) {
76    eprintln!(
77        "ERR: Unable to price CDR due to error at path `{}`: {}",
78        error.element().path,
79        error.warning()
80    );
81}
82
83/// Print `price::Warning`s to `stderr`.
84fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
85    if warnings.is_empty() {
86        return;
87    }
88
89    eprintln!(
90        "WARN: {} warnings from the pricing:\n{:#?}",
91        warnings.len_warnings(),
92        warnings.path_msg_map()
93    );
94}
examples/generate_cdr_from_tariff.rs (line 27)
11fn main() {
12    const TARIFF_JSON: &str = include_str!("data/tariff_time_and_parking_time_separate.json");
13
14    // Parse the raw JSON into a `json::Document` and guess the OCPI version of the tariff, falling
15    // back to `v2.2.1` when the version is uncertain.
16    let doc = json::parse_object(TARIFF_JSON).unwrap();
17    let tariff = tariff::infer_version(doc).certain_or(Version::V221);
18    let version = tariff.version();
19
20    // Check the tariff against the OCPI tariff schema. When `tariff::build` rejects the tariff,
21    // a schema warning names the field it couldn't use.
22    let (tariff, schema_warnings) = tariff::check(tariff.into_doc(), version).into_parts();
23
24    if !schema_warnings.is_empty() {
25        eprintln!(
26            "WARN: {} warnings from checking the tariff:\n{:#?}",
27            schema_warnings.len_warnings(),
28            schema_warnings.path_msg_map()
29        );
30    }
31
32    let tariff = match tariff::build(tariff) {
33        Ok(tariff) => tariff.ignore_warnings(),
34        Err(failure) => {
35            let (error, warnings) = failure.into_parts();
36            eprintln!(
37                "ERR: Unable to build the tariff due to error at path `{}`: {}",
38                error.element().path,
39                error.warning()
40            );
41            eprintln!(
42                "WARN: {} warnings from building the tariff:\n{:#?}",
43                warnings.len_warnings(),
44                warnings.path_msg_map()
45            );
46            return;
47        }
48    };
49
50    let config = generate::Config {
51        timezone: jiff::tz::get!("Europe/Amsterdam"),
52        start_date_time: Timestamp::from_str("2025-06-12 18:22:33+00:00").unwrap(),
53        end_date_time: Timestamp::from_str("2025-06-12 22:33:44+00:00").unwrap(),
54        max_current_supply_amp: Decimal::from(4),
55        requested_kwh: Decimal::from(24),
56        max_power_supply_kw: Decimal::from(80),
57    };
58    let report = match cdr::generate_from_tariff(&tariff, &config) {
59        Ok(r) => r,
60        Err(failure) => {
61            let (error, warnings) = failure.into_parts();
62            print_error(&error);
63            print_warnings(&warnings);
64            return;
65        }
66    };
67    let (report, warnings) = report.into_parts();
68
69    print_warnings(&warnings);
70
71    let generate::Report {
72        tariff_id,
73        tariff_currency_code,
74        partial_cdr,
75    } = report;
76
77    println!(
78        "CDR generated for tariff with id: `{tariff_id}` and currency code: `{tariff_currency_code}`"
79    );
80    println!("{partial_cdr:#?}");
81}
82
83/// Print `generate::Warning` that halted the example to `stderr`.
84fn print_error(error: &warning::Error<generate::Warning>) {
85    eprintln!(
86        "ERR: Unable to generate CDR due to error at path `{}`: {}",
87        error.element().path,
88        error.warning()
89    );
90}
91
92/// Print `generate::Warning`s to `stderr`.
93fn print_warnings(warnings: &warning::Set<generate::Warning>) {
94    if warnings.is_empty() {
95        return;
96    }
97
98    eprintln!(
99        "WARN: {} warnings from generating the CDR:\n{:#?}",
100        warnings.len_warnings(),
101        warnings.path_msg_map()
102    );
103}
Source

pub fn iter(&self) -> Iter<'_, W> ⓘ

Return an iterator of Warnings grouped by JSON element.

Source

pub fn id_path_map(&self, config: Limit) -> IdPathMap<'_>

Return a collection of Ids mapped to the paths they occurred at.

Source

pub fn msg_path_map(&self, config: Limit) -> MsgPathMap<'_>

Return a collection of warning messages mapped to the paths they occurred at.

Source

pub fn path_map(&self) -> BTreeMap<&str, Vec<&W>>

Return a map of element paths to a list of Warning.

This is designed to be used to print out maps of warnings associated with elements. You can use the debug alternate format {:#?} to print the map ‘pretty’ over multiple lines with indentation.

§A path does not identify an element

The set is keyed by json::ElemId, this map is keyed by path. Duplicate JSON keys are legal, and every occurrence of one has the same path, so several elements can compete for a single entry. The entries are built in ElemId order and collected, so the last occurrence in the document wins and the earlier ones’ warnings are absent from the map.

Nothing is lost from the set itself, so a caller that iterates groups - as cli::print::warning_set does - still sees every warning. Reach for Set::iter rather than this map when a warning may be reported against more than one occurrence of a field; Warning::DuplicateField is the case that makes this reachable in practice.

Source

pub fn into_path_map(self) -> BTreeMap<Path, Vec<W>>

Consume the Set and return a map of element paths to a list of Warnings.

This is designed to be used to print out maps of warnings associated with elements.

Source

pub fn path_id_map(&self) -> BTreeMap<&str, Vec<Id>>

Return a map of element paths to a list of Warning ids as Strings.

This is designed to be used to print out maps of warnings associated with elements. You can use the debug alternate format {:#?} to print the map ‘pretty’ over multiple lines with indentation.

Note: This representation is also valid JSON and can be copied directly to a test expectation file.

Source

pub fn path_msg_map(&self) -> BTreeMap<&str, Vec<String>>

Return a map of element paths to a list of Warning messages as Strings.

This is designed to be used to print out maps of warnings associated with elements. You can use the debug alternate format {:#?} to print the map ‘pretty’ over multiple lines with indentation.

Examples found in repository?
examples/price_cdr_with_known_version.rs (line 92)
84fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
85    if warnings.is_empty() {
86        return;
87    }
88
89    eprintln!(
90        "WARN: {} warnings from the pricing:\n{:#?}",
91        warnings.len_warnings(),
92        warnings.path_msg_map()
93    );
94}
More examples
Hide additional examples
examples/lint_tariff.rs (line 31)
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}
examples/price_cdr_with_unknown_version.rs (line 101)
93fn print_timezone_warnings(warnings: &warning::Set<timezone::Warning>) {
94    if warnings.is_empty() {
95        return;
96    }
97
98    eprintln!(
99        "WARN: {} warnings from the timezone search:\n{:#?}",
100        warnings.len_warnings(),
101        warnings.path_msg_map()
102    );
103}
104
105/// Print the `price::Warning` that halted the process to `stderr`.
106fn print_pricing_error(error: &warning::Error<price::Warning>) {
107    eprintln!(
108        "ERR: Unable to price CDR due to error at path `{}`: {}",
109        error.element().path,
110        error.warning()
111    );
112}
113
114/// Print the `price::Warning`s to `stderr`.
115fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
116    if warnings.is_empty() {
117        return;
118    }
119
120    eprintln!(
121        "WARN: {} warnings from the pricing:\n{:#?}",
122        warnings.len_warnings(),
123        warnings.path_msg_map()
124    );
125}
examples/generate_cdr_from_tariff.rs (line 28)
11fn main() {
12    const TARIFF_JSON: &str = include_str!("data/tariff_time_and_parking_time_separate.json");
13
14    // Parse the raw JSON into a `json::Document` and guess the OCPI version of the tariff, falling
15    // back to `v2.2.1` when the version is uncertain.
16    let doc = json::parse_object(TARIFF_JSON).unwrap();
17    let tariff = tariff::infer_version(doc).certain_or(Version::V221);
18    let version = tariff.version();
19
20    // Check the tariff against the OCPI tariff schema. When `tariff::build` rejects the tariff,
21    // a schema warning names the field it couldn't use.
22    let (tariff, schema_warnings) = tariff::check(tariff.into_doc(), version).into_parts();
23
24    if !schema_warnings.is_empty() {
25        eprintln!(
26            "WARN: {} warnings from checking the tariff:\n{:#?}",
27            schema_warnings.len_warnings(),
28            schema_warnings.path_msg_map()
29        );
30    }
31
32    let tariff = match tariff::build(tariff) {
33        Ok(tariff) => tariff.ignore_warnings(),
34        Err(failure) => {
35            let (error, warnings) = failure.into_parts();
36            eprintln!(
37                "ERR: Unable to build the tariff due to error at path `{}`: {}",
38                error.element().path,
39                error.warning()
40            );
41            eprintln!(
42                "WARN: {} warnings from building the tariff:\n{:#?}",
43                warnings.len_warnings(),
44                warnings.path_msg_map()
45            );
46            return;
47        }
48    };
49
50    let config = generate::Config {
51        timezone: jiff::tz::get!("Europe/Amsterdam"),
52        start_date_time: Timestamp::from_str("2025-06-12 18:22:33+00:00").unwrap(),
53        end_date_time: Timestamp::from_str("2025-06-12 22:33:44+00:00").unwrap(),
54        max_current_supply_amp: Decimal::from(4),
55        requested_kwh: Decimal::from(24),
56        max_power_supply_kw: Decimal::from(80),
57    };
58    let report = match cdr::generate_from_tariff(&tariff, &config) {
59        Ok(r) => r,
60        Err(failure) => {
61            let (error, warnings) = failure.into_parts();
62            print_error(&error);
63            print_warnings(&warnings);
64            return;
65        }
66    };
67    let (report, warnings) = report.into_parts();
68
69    print_warnings(&warnings);
70
71    let generate::Report {
72        tariff_id,
73        tariff_currency_code,
74        partial_cdr,
75    } = report;
76
77    println!(
78        "CDR generated for tariff with id: `{tariff_id}` and currency code: `{tariff_currency_code}`"
79    );
80    println!("{partial_cdr:#?}");
81}
82
83/// Print `generate::Warning` that halted the example to `stderr`.
84fn print_error(error: &warning::Error<generate::Warning>) {
85    eprintln!(
86        "ERR: Unable to generate CDR due to error at path `{}`: {}",
87        error.element().path,
88        error.warning()
89    );
90}
91
92/// Print `generate::Warning`s to `stderr`.
93fn print_warnings(warnings: &warning::Set<generate::Warning>) {
94    if warnings.is_empty() {
95        return;
96    }
97
98    eprintln!(
99        "WARN: {} warnings from generating the CDR:\n{:#?}",
100        warnings.len_warnings(),
101        warnings.path_msg_map()
102    );
103}

Trait Implementations§

Source§

impl<W: Debug + Warning> Debug for Set<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<W: Warning> IntoIterator for Set<W>

Source§

type Item = Group<W>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<W>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, W: Warning> IntoIterator for &'a Set<W>

Source§

type Item = &'a Group<W>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, W>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<W> Forget for Set<W>

§

impl<W> Freeze for Set<W>
where *const LeafNode<ElemId, Group<W>> is !null: Freeze, PhantomData<(Owned, LeafOrInternal)>: Freeze, PhantomData<Box<(ElemId, Group<W>)>>: Freeze,

§

impl<W> Move for Set<W>

§

impl<W> RefUnwindSafe for Set<W>
where W: RefUnwindSafe,

§

impl<W> Send for Set<W>
where NodeRef<Owned, ElemId, Group<W>, LeafOrInternal>: Send, BoxRaw<(ElemId, Group<W>)>: Send,

§

impl<W> Sync for Set<W>
where NodeRef<Owned, ElemId, Group<W>, LeafOrInternal>: Sync, BoxRaw<(ElemId, Group<W>)>: Sync,

§

impl<W> Unpin for Set<W>
where *const LeafNode<ElemId, Group<W>> is !null: Unpin, Box<(ElemId, Group<W>)>: Unpin,

§

impl<W> UnsafeUnpin for Set<W>
where *const LeafNode<ElemId, Group<W>> is !null: UnsafeUnpin, PhantomData<(Owned, LeafOrInternal)>: UnsafeUnpin, PhantomData<Box<(ElemId, Group<W>)>>: UnsafeUnpin,

§

impl<W> UnwindSafe for Set<W>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more