price_cdr_with_known_version/
price_cdr_with_known_version.rs1#![expect(clippy::expect_used, reason = "examples can panic")]
2#![expect(clippy::print_stderr, reason = "examples can log to stderr")]
3
4use chrono_tz::Tz;
5
6use ocpi_tariffs::{cdr, price, warning, Version};
7
8fn main() {
9 const CDR_JSON: &str =
10 include_str!("../test_data/v211/real_world/time_and_parking_time/cdr.json");
11
12 let report = cdr::parse_with_version(CDR_JSON, Version::V211).expect("unable to parse CDR");
14 let cdr::ParseReport {
15 cdr,
16 unexpected_fields,
17 } = report;
18
19 if !unexpected_fields.is_empty() {
20 eprintln!("Strange... there are fields in the CDR that are not defined in the spec.");
21
22 for path in &unexpected_fields {
23 eprintln!("{path}");
24 }
25 }
26
27 let report = match cdr::price(&cdr, price::TariffSource::UseCdr, Tz::Europe__Amsterdam) {
28 Ok(r) => r,
29 Err(set) => {
30 let (error, warnings) = set.into_parts();
31 print_pricing_error(&error);
32 print_pricing_warnings(&warnings);
33 return;
34 }
35 };
36
37 let (report, warnings) = report.into_parts();
38 print_pricing_warnings(&warnings);
39
40 let price::Report {
42 periods: _,
43 tariff_used: _,
44 tariff_reports: _,
45 timezone: _,
46 billed_energy: _,
47 billed_parking_time: _,
48 total_charging_time: _,
49 billed_charging_time: _,
50 total_cost: _,
51 total_fixed_cost: _,
52 total_time: _,
53 total_time_cost: _,
54 total_energy: _,
55 total_energy_cost: _,
56 total_parking_time: _,
57 total_parking_cost: _,
58 total_reservation_cost: _,
59 } = report;
60}
61
62fn print_pricing_error(error: &warning::Error<price::Warning>) {
63 eprintln!(
64 "ERR: Unable to price CDR due to error at path `{}`: {}",
65 error.element().path,
66 error.warning()
67 );
68}
69
70fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
71 if warnings.is_empty() {
72 return;
73 }
74
75 eprintln!(
76 "WARN: {} warnings from the pricing:\n {}",
77 warnings.len_warnings(),
78 warning::SetWriter::new(warnings)
79 );
80}