Skip to main content

generate_cdr_from_tariff/
generate_cdr_from_tariff.rs

1#![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 jiff::Timestamp;
8use ocpi_tariffs::{Version, cdr, generate, json, tariff, version::Versioned as _, warning};
9use rust_decimal::Decimal;
10
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}