1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! # OCPI Tariffs library
//!
//! Calculate the (sub)totals of a [charge session](https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/mod_cdrs.asciidoc)
//! using the [`cdr::price`] function and use the generated [`price::Report`] to review and compare the calculated
//! totals versus the sources from the `CDR`.
//!
//! - Use [`json::parse_object`] to parse a CDR or tariff `&str` into a [`json::Document`].
//! - Use [`cdr::infer_version`] or [`tariff::infer_version`] to guess which OCPI [`Version`] a CDR or tariff is.
//! - Use the [`cdr::build`] and [`tariff::build`] functions to check a [`json::Document`] against the schema for a given version.
//! - Use the [`tariff::lint`] to lint a tariff: flag common errors, bugs, dangerous constructs and stylistic flaws in the tariff.
//!
//! # Examples
//!
//! ## Price a CDR with embedded tariff
//!
//! If you have a CDR JSON with an embedded tariff you can price the CDR with the following code:
//!
//! ```rust
//! # use ocpi_tariffs::{cdr, json, price, warning, Version};
//! #
//! # const CDR_JSON: &str = include_str!("cdr.json");
//!
//! let doc = json::parse_object(CDR_JSON)?;
//! let (cdr, _warnings) = cdr::build(doc, Version::V211).into_parts();
//!
//! let report = cdr::price(&cdr, price::TariffSource::UseCdr, chrono_tz::Tz::Europe__Amsterdam).unwrap();
//! let (report, warnings) = report.into_parts();
//!
//! if !warnings.is_empty() {
//! eprintln!("Pricing the CDR resulted in `{}` warnings", warnings.len_warnings());
//!
//! for group in warnings {
//! let (element, warnings) = group.to_parts();
//! eprintln!(" {}", element.path);
//!
//! for warning in warnings {
//! eprintln!(" - {warning}");
//! }
//! }
//! }
//!
//! # Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
//! ```
//!
//! ## Price a CDR using tariff in separate JSON file
//!
//! If you have a CDR JSON with a tariff in a separate JSON file you can price the CDR with the
//! following code:
//!
//! ```rust
//! # use ocpi_tariffs::{cdr, json, price, tariff, warning, Version};
//! #
//! # const CDR_JSON: &str = include_str!("cdr.json");
//! # const TARIFF_JSON: &str = include_str!("tariff.json");
//!
//! let cdr_doc = json::parse_object(CDR_JSON)?;
//! let (cdr, _cdr_warnings) = cdr::build(cdr_doc, Version::V211).into_parts();
//!
//! let tariff_doc = json::parse_object(TARIFF_JSON)?;
//! let (tariff, _tariff_warnings) = tariff::build(tariff_doc, Version::V211).into_parts();
//!
//! let report = cdr::price(&cdr, price::TariffSource::Override(vec![tariff]), chrono_tz::Tz::Europe__Amsterdam).unwrap();
//! let (report, warnings) = report.into_parts();
//!
//! if !warnings.is_empty() {
//! eprintln!("Pricing the CDR resulted in `{}` warnings", warnings.len_warnings());
//!
//! for group in warnings {
//! let (element, warnings) = group.to_parts();
//! eprintln!(" {}", element.path);
//!
//! for warning in warnings {
//! eprintln!(" - {warning}");
//! }
//! }
//! }
//!
//! # Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
//! ```
//!
//! ## Lint a tariff
//!
//! ```rust
//! # use ocpi_tariffs::{guess, json, tariff, warning};
//! #
//! # const TARIFF_JSON: &str = include_str!("tariff.json");
//!
//! let doc = json::parse_object(TARIFF_JSON)?;
//! let guess::Version::Certain(tariff) = tariff::infer_version(doc) else {
//! return Err("Unable to guess the version of given tariff JSON.".into());
//! };
//! let tariff = tariff::build_versioned(tariff).ignore_warnings();
//!
//! let report = tariff::lint(&tariff);
//!
//! eprintln!("`{}` lint warnings found", report.warnings.len_warnings());
//!
//! for group in report.warnings {
//! let (element, warnings) = group.to_parts();
//! eprintln!(
//! "Warnings reported for `json::Element` at path: `{}`",
//! element.path
//! );
//!
//! for warning in warnings {
//! eprintln!(" * {warning}");
//! }
//!
//! eprintln!();
//! }
//!
//! # Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
//! ```
use fmt;
pub use ;
pub use ;
use ;
pub use ;
use IntoCaveat;
pub use ;
use Weekday;
/// The Id for a tariff used in the pricing of a CDR.
pub type TariffId = String;
/// The OCPI versions supported by this crate.
/// An object for a specific OCPI [`Version`].
/// An object with an uncertain [`Version`].
/// Add two types together and saturate to max if the addition operation overflows.
///
/// This is private to the crate as `ocpi-tarifffs` does not want to provide numerical types for use by other crates.
/// Subtract two types from each other and saturate to zero if the subtraction operation overflows.
///
/// This is private to the crate as `ocpi-tarifffs` does not want to provide numerical types for use by other crates.
/// A debug utility to `Display` an `Option<T>` as either `Display::fmt(T)` or the null set `∅`.
where
T: Display;