generate_cdr_from_tariff/
generate_cdr_from_tariff.rs1#![expect(clippy::unwrap_used, reason = "examples can panic")]
2#![expect(clippy::print_stdout, reason = "examples can log to stdout")]
3#![expect(clippy::print_stderr, reason = "examples can log to stderr")]
4
5use std::str::FromStr as _;
6
7use chrono::{DateTime, Utc};
8use ocpi_tariffs::{cdr, generate, guess, tariff, warning, Version};
9use rust_decimal::Decimal;
10
11fn main() {
12 const TARIFF_JSON: &str = include_str!(
13 "../test_data/v211/real_world/time_and_parking_time_separate_tariff/tariff.json"
14 );
15
16 let report = tariff::parse_and_report(TARIFF_JSON).unwrap();
17 let guess::Report {
18 unexpected_fields,
19 version,
20 } = report;
21
22 if !unexpected_fields.is_empty() {
23 eprintln!("Strange... there are fields in the tariff that are not defined in the spec.");
24
25 for path in &unexpected_fields {
26 eprintln!(" * {path}");
27 }
28
29 eprintln!();
30 }
31
32 let tariff = version.certain_or(Version::V221);
33
34 let config = generate::Config {
35 timezone: chrono_tz::Europe::Amsterdam,
36 start_date_time: DateTime::<Utc>::from_str("2025-06-12 18:22:33+00:00").unwrap(),
37 end_date_time: DateTime::<Utc>::from_str("2025-06-12 22:33:44+00:00").unwrap(),
38 max_current_supply_amp: Decimal::from(4),
39 max_energy_battery_kwh: Decimal::from(24),
40 max_power_supply_kw: Decimal::from(80),
41 };
42 let report = cdr::generate_from_tariff(&tariff, config).unwrap();
43 let (report, warnings) = report.into_parts();
44
45 print_warnings(&tariff, &warnings);
46
47 let generate::Report {
48 tariff_id,
49 tariff_currency_code,
50 partial_cdr,
51 } = report;
52
53 println!("CDR genereated for tariff with id: `{tariff_id}` and currency code: `{tariff_currency_code}`");
54 println!("{partial_cdr:#?}");
55}
56
57fn print_warnings(tariff: &tariff::Versioned<'_>, warnings: &warning::Set<tariff::WarningKind>) {
58 if warnings.is_empty() {
59 return;
60 }
61
62 eprintln!(
63 "WARN: {} warnings from the linting:\n {}",
64 warnings.len(),
65 warning::SetWriter::new(tariff.as_element(), warnings)
66 );
67}