pub struct Error<W: Warning> { /* private fields */ }Expand description
The warning that caused an operation to fail.
The warning is stored apart from the source set so it is clear which warning has been escalated to an error.
Implementations§
Source§impl<W: Warning> Error<W>
impl<W: Warning> Error<W>
Sourcepub fn warning(&self) -> &W
pub fn warning(&self) -> &W
Return reference to the Warning.
Examples found in repository?
More examples
examples/price_cdr_with_unknown_version.rs (line 88)
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}examples/generate_cdr_from_tariff.rs (line 39)
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}Sourcepub fn into_warning(self) -> W
pub fn into_warning(self) -> W
Consume the Error and return the Warning.
Sourcepub fn element(&self) -> &Element
pub fn element(&self) -> &Element
Examples found in repository?
More examples
examples/price_cdr_with_unknown_version.rs (line 87)
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}examples/generate_cdr_from_tariff.rs (line 38)
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}Sourcepub fn into_parts(self) -> (W, Element)
pub fn into_parts(self) -> (W, Element)
Consume the Cause and return the constituent parts.
Trait Implementations§
Source§impl<W: Warning> Error for Error<W>
impl<W: Warning> Error for Error<W>
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Auto Trait Implementations§
impl<W> Forget for Error<W>
impl<W> Freeze for Error<W>
impl<W> Move for Error<W>
impl<W> RefUnwindSafe for Error<W>where
W: RefUnwindSafe,
impl<W> Send for Error<W>where
RawVecInner: Send,
impl<W> Sync for Error<W>where
RawVecInner: Sync,
impl<W> Unpin for Error<W>
impl<W> UnsafeUnpin for Error<W>
impl<W> UnwindSafe for Error<W>where
W: UnwindSafe,
RawVecInner: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more