pub struct Set<W: Warning>(/* private fields */);Implementations§
Source§impl Set<Warning>
impl Set<Warning>
Sourcepub fn unexpected_fields(&self) -> PathSet<'_>
pub fn unexpected_fields(&self) -> PathSet<'_>
Collect the field paths of all Warning::UnexpectedField warnings into a set of json::Paths.
Sourcepub fn non_spec_fields(&self) -> PathSet<'_>
pub fn non_spec_fields(&self) -> PathSet<'_>
Collect the field paths of all Warning::NonSpecField warnings into a set of json::Paths.
Sourcepub fn missing_fields(&self) -> PathSet<'_>
pub fn missing_fields(&self) -> PathSet<'_>
Collect the field paths of all Warning::MissingField warnings into a set of json::Paths.
Sourcepub fn remove_unexpected_fields(&mut self)
pub fn remove_unexpected_fields(&mut self)
Remove all Warning::UnexpectedField warnings from the set.
Sourcepub fn remove_missing_fields(&mut self)
pub fn remove_missing_fields(&mut self)
Remove all Warning::MissingField warnings from the set.
Sourcepub fn remove_invalid_types(&mut self)
pub fn remove_invalid_types(&mut self)
Remove all Warning::InvalidType warnings from the set.
Sourcepub fn remove_null_fields(&mut self)
pub fn remove_null_fields(&mut self)
Remove all Warning::NullField warnings from the set.
Sourcepub fn remove_cardinalities(&mut self)
pub fn remove_cardinalities(&mut self)
Remove all Warning::Cardinality warnings from the set.
Sourcepub fn remove_string_too_longs(&mut self)
pub fn remove_string_too_longs(&mut self)
Remove all Warning::StringTooLong warnings from the set.
Source§impl<W: Warning> Set<W>
impl<W: Warning> Set<W>
Sourcepub fn remove_rejected(&mut self)
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.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return true if the Warning set is empty.
Examples found in repository?
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
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}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}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}Sourcepub fn iter_warnings(&self) -> impl Iterator<Item = (&Element, &W)>
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()
}Sourcepub fn contains<F>(&self, predicate: F) -> bool
pub fn contains<F>(&self, predicate: F) -> 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))
}Sourcepub fn find<F>(&self, predicate: F) -> Option<(&Element, &W)>
pub fn find<F>(&self, predicate: F) -> Option<(&Element, &W)>
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())
}Sourcepub fn ids(&self) -> BTreeSet<Id>
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.
Sourcepub fn warnings_at<'set>(
&'set self,
path: &str,
) -> impl Iterator<Item = &'set W>
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.
Sourcepub fn len_elements(&self) -> usize
pub fn len_elements(&self) -> usize
Sourcepub fn len_warnings(&self) -> usize
pub fn len_warnings(&self) -> usize
Examples found in repository?
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
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}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}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}Sourcepub fn id_path_map(&self, config: Limit) -> IdPathMap<'_>
pub fn id_path_map(&self, config: Limit) -> IdPathMap<'_>
Return a collection of Ids mapped to the paths they occurred at.
Sourcepub fn msg_path_map(&self, config: Limit) -> MsgPathMap<'_>
pub fn msg_path_map(&self, config: Limit) -> MsgPathMap<'_>
Return a collection of warning messages mapped to the paths they occurred at.
Sourcepub fn path_map(&self) -> BTreeMap<&str, Vec<&W>>
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.
Sourcepub fn into_path_map(self) -> BTreeMap<Path, Vec<W>>
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.
Sourcepub fn path_id_map(&self) -> BTreeMap<&str, Vec<Id>>
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.
Sourcepub fn path_msg_map(&self) -> BTreeMap<&str, Vec<String>>
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?
More examples
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}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}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}