pub struct Failure<W: Warning> { /* private fields */ }Expand description
A failed attempt to make a value: the Error that stopped it, and every
Warning raised before it.
A Caveat is a value that was made, with the warnings to bear in mind when using it. A
Failure is the attempt that didn’t make one, so it’s the Err of a Verdict. A caller
reads a Failure’s warnings with the same methods as a Caveat’s.
Implementations§
Source§impl<W> Failure<W>where
W: Warning,
impl<W> Failure<W>where
W: Warning,
Sourcepub fn warnings(&self) -> &Set<W>
pub fn warnings(&self) -> &Set<W>
Return the warnings raised before the failure, without the Error.
Sourcepub fn into_parts(self) -> (Error<W>, Set<W>)
pub fn into_parts(self) -> (Error<W>, Set<W>)
Consume the Failure and return the Error and the warnings raised before it.
Examples found in repository?
examples/price_cdr_with_known_version.rs (line 44)
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}More examples
examples/price_cdr_with_unknown_version.rs (line 24)
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}examples/generate_cdr_from_tariff.rs (line 35)
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}Sourcepub fn ignore_warnings(self) -> Error<W>
pub fn ignore_warnings(self) -> Error<W>
Return the Error and drop the warnings raised before it.
Trait Implementations§
Auto Trait Implementations§
impl<W> Forget for Failure<W>
impl<W> Freeze for Failure<W>
impl<W> Move for Failure<W>
impl<W> RefUnwindSafe for Failure<W>where
W: RefUnwindSafe,
impl<W> Send for Failure<W>
impl<W> Sync for Failure<W>
impl<W> Unpin for Failure<W>
impl<W> UnsafeUnpin for Failure<W>where
*const Error<W> is !null: UnsafeUnpin,
PhantomData<Error<W>>: UnsafeUnpin,
*const LeafNode<ElemId, Group<W>> is !null: UnsafeUnpin,
PhantomData<(Owned, LeafOrInternal)>: UnsafeUnpin,
PhantomData<Box<(ElemId, Group<W>)>>: UnsafeUnpin,
impl<W> UnwindSafe for Failure<W>
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