Skip to main content

ocpi_tariffs/
tariff.rs

1//! Parse a tariff.
2
3#[cfg(test)]
4pub(crate) mod test;
5
6#[cfg(test)]
7mod test_build;
8
9#[cfg(test)]
10mod test_real_world;
11
12pub(crate) mod v211;
13pub(crate) mod v221;
14pub(crate) mod v2x;
15
16use std::{borrow::Cow, fmt};
17
18use crate::{
19    FromSchema as _, Verdict, country, currency, datetime, duration, explain, from_warning_all,
20    json, lint, money, number, schema, string, version,
21    warning::{self, Caveat},
22};
23
24#[derive(Debug)]
25/// The warnings that happen when reading a tariff.
26pub enum Warning {
27    /// Raised while reading the tariff's `country_code`.
28    Country(country::Warning),
29    /// Raised while reading the tariff's currency code.
30    Currency(currency::Warning),
31    /// Raised while reading a timestamp.
32    DateTime(datetime::Warning),
33    /// Raised while decoding a JSON string's escape sequences.
34    Decode(json::decode::Warning),
35    /// Raised while reading a duration.
36    Duration(duration::Warning),
37
38    /// A field in the tariff doesn't have the expected value.
39    FieldInvalidValue {
40        /// The value encountered.
41        value: String,
42
43        /// A message about what values are expected for this field.
44        message: Cow<'static, str>,
45    },
46
47    /// Raised while reading a price.
48    Money(money::Warning),
49
50    /// The tariff has no `Element`s.
51    NoElements,
52
53    /// Raised while reading a number.
54    Number(number::Warning),
55
56    /// Raised while reading a string.
57    String(string::Warning),
58
59    /// Raised when [`build`] rejects a tariff object because a field the object must have is
60    /// missing, is `null` or has a value [`check`] can't read. The warning sits at the
61    /// field, or at the object when the field is missing. A [`schema::Warning`] that
62    /// [`check`] returns says what is wrong with the field (see [`warning::Rejected`]).
63    Rejected,
64}
65
66impl Warning {
67    /// Create a new `Warning::FieldInvalidValue` where the field is built from the given `json::Element`.
68    fn field_invalid_value(
69        value: impl Into<String>,
70        message: impl Into<Cow<'static, str>>,
71    ) -> Self {
72        Warning::FieldInvalidValue {
73            value: value.into(),
74            message: message.into(),
75        }
76    }
77}
78
79impl fmt::Display for Warning {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            Self::String(warning_kind) => write!(f, "{warning_kind}"),
83            Self::Country(warning_kind) => write!(f, "{warning_kind}"),
84            Self::Currency(warning_kind) => write!(f, "{warning_kind}"),
85            Self::DateTime(warning_kind) => write!(f, "{warning_kind}"),
86            Self::Decode(warning_kind) => write!(f, "{warning_kind}"),
87            Self::Duration(warning_kind) => write!(f, "{warning_kind}"),
88            Self::FieldInvalidValue { value, message } => {
89                write!(f, "Field has invalid value `{value}`: {message}")
90            }
91            Self::Money(warning_kind) => write!(f, "{warning_kind}"),
92            Self::NoElements => f.write_str("The tariff has no `elements`"),
93            Self::Number(warning_kind) => write!(f, "{warning_kind}"),
94            Self::Rejected => f.write_str(
95                "The schema IR for a tariff object was rejected; see the schema \
96                 validation warnings.",
97            ),
98        }
99    }
100}
101
102impl crate::Warning for Warning {
103    fn id(&self) -> warning::Id {
104        match self {
105            Self::String(warning) => warning.id(),
106            Self::Country(warning) => warning.id(),
107            Self::Currency(warning) => warning.id(),
108            Self::DateTime(warning) => warning.id(),
109            Self::Decode(warning) => warning.id(),
110            Self::Duration(warning) => warning.id(),
111            Self::FieldInvalidValue { value, message: _ } => {
112                warning::Id::from_string(format!("field_invalid_value({value})"))
113            }
114            Self::Money(warning) => warning.id(),
115            Self::NoElements => warning::Id::from_static("no_elements"),
116            Self::Number(warning) => warning.id(),
117            Self::Rejected => warning::Id::from_static("rejected"),
118        }
119    }
120
121    fn is_rejected(&self) -> bool {
122        matches!(self, Self::Rejected)
123    }
124}
125
126impl From<warning::Rejected> for Warning {
127    fn from(_: warning::Rejected) -> Self {
128        Self::Rejected
129    }
130}
131
132from_warning_all!(
133    country::Warning => Warning::Country,
134    currency::Warning => Warning::Currency,
135    datetime::Warning => Warning::DateTime,
136    duration::Warning => Warning::Duration,
137    json::decode::Warning => Warning::Decode,
138    money::Warning => Warning::Money,
139    number::Warning => Warning::Number,
140    string::Warning => Warning::String
141);
142
143/// The five character ID of the CPO.
144///
145/// The first two characters are the ISO-3166 alpha-2 country code of the CPO.
146/// The remaining three characters are the ISO-15118 ID of the CPO.
147#[derive(Clone, Debug)]
148pub(crate) struct CpoId<'buf> {
149    /// The ISO-3166 alpha-2 country code.
150    pub country_code: country::Code,
151
152    /// The ISO-15118 ID.
153    pub id: string::AsciiExactLen<'buf, 3>,
154}
155
156/// Infer which OCPI [`Version`](crate::Version) a tariff [`json::Document`] is, without checking
157/// the document against the schema.
158///
159/// Use this when the version of the tariff is not known up front.
160/// A caller gets the [`json::Document`] from [`json::parse_object`].
161/// The returned [`VersionGuess`] is [`Certain`](version::Guess::Certain) when the tariff's
162/// fields settle which version it is, and [`Uncertain`](version::Guess::Uncertain) otherwise.
163///
164/// To check the tariff against the OCPI schema, pass the guessed document and version to [`check`].
165///
166/// # Examples
167///
168/// ```rust
169/// use ocpi_tariffs::{json, tariff, Version, version::Versioned as _};
170/// #
171/// # const TARIFF_JSON: &str = include_str!("tariff.json");
172///
173/// let doc = json::parse_object(TARIFF_JSON)?;
174/// let tariff = tariff::infer_version(doc).certain_or(Version::V221);
175/// let version = tariff.version();
176/// let (tariff, warnings) = tariff::check(tariff.into_doc(), version).into_parts();
177///
178/// # Ok::<(), json::ParseError>(())
179/// ```
180pub fn infer_version(json: json::Document<'_>) -> VersionGuess<'_> {
181    version::tariff_version(json)
182}
183
184/// What [`infer_version`] returns: the tariff document, paired with its version when the
185/// tariff's fields settle which version it is.
186pub type VersionGuess<'buf> = version::Guess<VersionedJson<'buf>, Unversioned<'buf>>;
187
188/// Check a [`json::Document`] against the OCPI tariff schema for the given
189/// [`Version`](crate::Version)[^spec-v211][^spec-v221].
190///
191/// `check` is the second step, after [`json::parse_object`] parses the document.
192/// It reports each fault in the document's structure as a [`schema::Warning`]: a field that is
193/// unexpected, missing or of the wrong JSON type, or a value of the wrong form.
194/// `check` never fails, so the returned [`Caveat`] holds a [`Checked`]
195/// however many faults it found.
196///
197/// The next step is [`build`], which reads the checked tariff into the [`Tariff`] that
198/// [`explain`](explain()) and [`cdr::generate_from_tariff`](crate::cdr::generate_from_tariff)
199/// take. The features [`lint`](lint()) and [`fix`](fix()) take the checked tariff itself.
200///
201/// # Examples
202///
203/// ```rust
204/// # use ocpi_tariffs::{json, tariff, Version};
205/// #
206/// # const TARIFF_JSON: &str = include_str!("tariff.json");
207///
208/// let doc = json::parse_object(TARIFF_JSON)?;
209/// let (tariff, warnings) = tariff::check(doc, Version::V211).into_parts();
210///
211/// if !warnings.is_empty() {
212///     eprintln!("The tariff has `{}` schema warnings.", warnings.len_warnings());
213/// }
214///
215/// # Ok::<(), json::ParseError>(())
216/// ```
217///
218/// [^spec-v211]: <https://github.com/ocpi/ocpi/blob/28ea45329a50c3dbfd790505b97860854db1d4a6/mod_tariffs.md>
219/// [^spec-v221]: <https://github.com/ocpi/ocpi/blob/a57ecb624fbe0f19537ac7956a11f3019a65018f/mod_tariffs.asciidoc>
220pub fn check(
221    json: json::Document<'_>,
222    version: crate::Version,
223) -> Caveat<Checked<'_>, schema::Warning> {
224    let mut warnings = warning::Gatherer::new();
225    let version = match version {
226        crate::Version::V221 => Version::V221(schema::v221::build_tariff(&json, &mut warnings)),
227        crate::Version::V211 => Version::V211(schema::v211::build_tariff(&json, &mut warnings)),
228    };
229    let checked = Checked { doc: json, version };
230    Caveat::new(checked, warnings.into_set())
231}
232
233/// Build a [`Tariff`] from a checked tariff, the [`Checked`] that [`check`]
234/// returns[^spec-v211][^spec-v221].
235///
236/// [`json::parse_object`] parses a tariff document and [`check`] checks it against the
237/// OCPI tariff schema. The features [`lint`](lint()) and [`fix`](fix()) take the checked
238/// tariff. The features [`explain`](explain()) and
239/// [`cdr::generate_from_tariff`](crate::cdr::generate_from_tariff) take the [`Tariff`] that
240/// `build` makes from it.
241///
242/// The check and the build judge different things:
243///
244/// - The check judges the document's shape: which fields it has, their JSON types and the form
245///   of their values, such as a string's length or a number's precision. It reports each fault
246///   as a [`schema::Warning`], and returns a [`Checked`] however many faults it found.
247/// - `build` reads each field's value into the type a [`Tariff`] holds, such as a timestamp, a
248///   price or a country code, and judges the tariff as a whole. It reports each fault as a
249///   [`Warning`], and fails if it can't make a [`Tariff`].
250///
251/// `build` fails if a field the tariff must have is missing or can't be read, or if the tariff
252/// has no elements. The [`Failure`](warning::Failure) then holds every warning `build` raises
253/// before it fails. A required field that is missing, is `null` or has a value [`check`]
254/// can't read makes `build` fail with [`Warning::Rejected`]. The error sits at the field, or
255/// at the object that contains it when the field is missing. A [`schema::Warning`] that
256/// [`check`] returns says what is wrong with the field.
257///
258/// # Examples
259///
260/// ```rust
261/// # use ocpi_tariffs::{json, tariff, Language, Version};
262/// #
263/// # const TARIFF_JSON: &str = include_str!("tariff.json");
264///
265/// let doc = json::parse_object(TARIFF_JSON)?;
266/// let (tariff, schema_warnings) = tariff::check(doc, Version::V211).into_parts();
267///
268/// if !schema_warnings.is_empty() {
269///     eprintln!("The tariff has `{}` schema warnings.", schema_warnings.len_warnings());
270/// }
271///
272/// let Ok(tariff) = tariff::build(tariff) else {
273///     return Err("The tariff could not be read well enough to use.".into());
274/// };
275/// let (tariff, warnings) = tariff.into_parts();
276///
277/// if !warnings.is_empty() {
278///     eprintln!("The tariff has `{}` warnings.", warnings.len_warnings());
279/// }
280///
281/// println!("{}", tariff::explain(&tariff, Language::EnUS));
282///
283/// # Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
284/// ```
285///
286/// [^spec-v211]: <https://github.com/ocpi/ocpi/blob/28ea45329a50c3dbfd790505b97860854db1d4a6/mod_tariffs.md>
287/// [^spec-v221]: <https://github.com/ocpi/ocpi/blob/a57ecb624fbe0f19537ac7956a11f3019a65018f/mod_tariffs.asciidoc>
288pub fn build(tariff: Checked<'_>) -> Verdict<Tariff<'_>, Warning> {
289    let mut warnings = warning::Gatherer::new();
290    let v221 = tariff.lower_to_v221(&mut warnings);
291    let element = tariff.into_doc().into_root();
292    let tariff = v221.map(|tariff| Tariff { tariff, element });
293    warnings.into_verdict(tariff)
294}
295
296/// A tariff that [`build`] makes from a checked tariff. A feature such as
297/// [`explain`](explain()) or [`cdr::generate_from_tariff`](crate::cdr::generate_from_tariff)
298/// takes it.
299///
300/// A `v2.1.1` tariff is held as its `v2.2.1` equivalent.
301pub struct Tariff<'buf> {
302    tariff: v221::Tariff<'buf>,
303
304    /// The JSON element of the tariff,
305    /// which a feature uses to report a warning about the tariff as a whole.
306    element: json::Element<'buf>,
307}
308
309impl fmt::Debug for Tariff<'_> {
310    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
311        fmt::Debug::fmt(&self.tariff, f)
312    }
313}
314
315impl<'buf> Tariff<'buf> {
316    pub(crate) fn new(tariff: v221::Tariff<'buf>, element: json::Element<'buf>) -> Self {
317        Self { tariff, element }
318    }
319
320    pub(crate) fn as_v221(&self) -> &v221::Tariff<'buf> {
321        &self.tariff
322    }
323
324    pub(crate) fn as_element(&self) -> &json::Element<'buf> {
325        &self.element
326    }
327}
328
329/// A `json::Document` that has been processed by [`infer_version`] and has been identified
330/// as being a concrete [`Version`](crate::Version).
331#[derive(Clone)]
332pub struct VersionedJson<'buf> {
333    /// The parsed JSON.
334    doc: json::Document<'buf>,
335
336    /// The `Version` of the tariff, determined during parsing.
337    version: crate::Version,
338}
339
340/// A tariff document checked against the OCPI schema, as [`check`] returns it.
341#[derive(Clone)]
342pub struct Checked<'buf> {
343    /// The parsed JSON.
344    doc: json::Document<'buf>,
345
346    /// The `Version` of the tariff, determined during parsing.
347    version: Version<'buf>,
348}
349
350impl fmt::Debug for Checked<'_> {
351    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
352        if f.alternate() {
353            match &self.version {
354                Version::V211(tariff) => fmt::Debug::fmt(&tariff, f),
355                Version::V221(tariff) => fmt::Debug::fmt(&tariff, f),
356            }
357        } else {
358            match &self.version {
359                Version::V211(_) => f.write_str("V211"),
360                Version::V221(_) => f.write_str("V221"),
361            }
362        }
363    }
364}
365
366impl version::Versioned for Checked<'_> {
367    fn version(&self) -> crate::Version {
368        match self.version {
369            Version::V211(_) => crate::Version::V211,
370            Version::V221(_) => crate::Version::V221,
371        }
372    }
373}
374
375impl<'buf> Checked<'buf> {
376    /// Lower the schema IR into the "normalized" `v221` tariff,
377    /// gathering its warnings into `warnings`.
378    ///
379    /// A `v211` tariff is parsed as `v211` and then converted, because the two versions
380    /// differ in more than field names.
381    ///
382    /// A tariff with no elements is rejected, because it prices every session at zero, so no
383    /// feature can use it. The check lives here rather than in the `LowerSchema` impls, because
384    /// only this layer has an element to anchor the error to: the tariff itself. The located
385    /// cause is the schema walk's `Cardinality` warning on the `elements` array.
386    pub(crate) fn lower_to_v221(
387        &self,
388        warnings: &mut dyn warning::Gather<Warning>,
389    ) -> Result<v221::Tariff<'buf>, warning::Error<Warning>> {
390        let tariff = match &self.version {
391            Version::V211(tariff) => {
392                let tariff = v211::Tariff::from_schema(tariff, warnings)?;
393
394                v221::Tariff::from(tariff)
395            }
396            Version::V221(tariff) => v221::Tariff::from_schema(tariff, warnings)?,
397        };
398
399        if tariff.elements.is_empty() {
400            return Err(warning::Error::new(self.as_element(), Warning::NoElements));
401        }
402
403        Ok(tariff)
404    }
405
406    /// Borrow the schema intermediate representation this tariff was built into.
407    ///
408    /// The linter reads this to inspect the document field by field without re-walking the
409    /// JSON; see [`lint::tariff`](mod@crate::lint::tariff).
410    pub(crate) fn schema(&self) -> &Version<'buf> {
411        &self.version
412    }
413
414    /// Return the inner [`json::Document`] and discard the version info.
415    pub fn into_doc(self) -> json::Document<'buf> {
416        self.doc
417    }
418
419    /// Return the inner [`json::Element`] and discard the version info.
420    pub(crate) fn as_element(&self) -> &json::Element<'buf> {
421        self.doc.root()
422    }
423
424    /// Return the inner [`json::Document`] and discard the version info.
425    pub fn as_doc(&self) -> &json::Document<'buf> {
426        &self.doc
427    }
428
429    /// Return the inner JSON `str` and discard the version info.
430    pub fn as_json_str(&self) -> &'buf str {
431        self.doc.source()
432    }
433}
434
435#[expect(
436    clippy::large_enum_variant,
437    reason = "the v2.1.1 and v2.2.1 tariff IRs differ in size; this short-lived versioned \
438              value is not worth boxing"
439)]
440#[derive(Clone)]
441pub(crate) enum Version<'buf> {
442    V211(schema::v211::Tariff<'buf>),
443    V221(schema::v221::Tariff<'buf>),
444}
445
446impl fmt::Debug for VersionedJson<'_> {
447    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
448        if f.alternate() {
449            fmt::Debug::fmt(&self.doc, f)
450        } else {
451            match self.version {
452                crate::Version::V211 => f.write_str("V211"),
453                crate::Version::V221 => f.write_str("V221"),
454            }
455        }
456    }
457}
458
459impl version::Versioned for VersionedJson<'_> {
460    fn version(&self) -> crate::Version {
461        self.version
462    }
463}
464
465impl<'buf> VersionedJson<'buf> {
466    /// Create a new `Versioned` object.
467    pub(crate) fn new(doc: json::Document<'buf>, version: crate::Version) -> Self {
468        Self { doc, version }
469    }
470
471    /// Return the inner [`json::Document`] and discard the version info.
472    pub fn into_doc(self) -> json::Document<'buf> {
473        self.doc
474    }
475
476    /// Return the inner [`json::Document`] and discard the version info.
477    pub fn as_doc(&self) -> &json::Document<'buf> {
478        &self.doc
479    }
480
481    /// Return the inner JSON `str` and discard the version info.
482    pub fn as_json_str(&self) -> &'buf str {
483        self.doc.source()
484    }
485}
486
487/// A [`json::Document`] that has been processed by [`infer_version`]
488/// and was determined to not be one of the supported [`Version`](crate::Version)s.
489#[derive(Debug)]
490pub struct Unversioned<'buf> {
491    doc: json::Document<'buf>,
492}
493
494impl<'buf> Unversioned<'buf> {
495    /// Create an unversioned [`json::Element`].
496    pub(crate) fn new(elem: json::Document<'buf>) -> Self {
497        Self { doc: elem }
498    }
499
500    /// Return the inner [`json::Document`] and discard the version info.
501    pub fn into_doc(self) -> json::Document<'buf> {
502        self.doc
503    }
504}
505
506impl<'buf> version::Unversioned for Unversioned<'buf> {
507    type Versioned = VersionedJson<'buf>;
508
509    fn force_into_versioned(self, version: crate::Version) -> VersionedJson<'buf> {
510        let Self { doc } = self;
511        VersionedJson { doc, version }
512    }
513}
514
515/// Lint the given tariff and return a [`lint::tariff::Report`] of any `Warning`s found.
516///
517/// A caller reaches `lint` by passing the tariff through [`json::parse_object`] and [`check`].
518/// `lint` takes the checked tariff rather than a built one, so it can report on a tariff that
519/// [`build`] would refuse. It reports only what linting adds, because [`check`] returns the
520/// schema warnings.
521///
522/// # Examples
523///
524/// ```rust
525/// # use ocpi_tariffs::{json, tariff, warning, version::Versioned as _};
526/// #
527/// # const TARIFF_JSON: &str = include_str!("tariff.json");
528///
529/// let doc = json::parse_object(TARIFF_JSON)?;
530/// let tariff::VersionGuess::Certain(tariff) = tariff::infer_version(doc) else {
531///     return Err("Unable to guess the version of given tariff JSON.".into());
532/// };
533/// let version = tariff.version();
534/// let (tariff, schema_warnings) = tariff::check(tariff.into_doc(), version).into_parts();
535///
536/// let report = tariff::lint(&tariff);
537///
538/// eprintln!("`{}` schema warnings found", schema_warnings.len_warnings());
539/// eprintln!("`{}` lint warnings found", report.warnings.len_warnings());
540///
541/// for group in report.warnings {
542///     let (element, warnings) = group.to_parts();
543///     eprintln!(
544///         "Warnings reported for `json::Element` at path: `{}`",
545///         element.path
546///     );
547///
548///     for warning in warnings {
549///         eprintln!("  * {warning}");
550///     }
551///
552///     eprintln!();
553/// }
554///
555/// # Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
556/// ```
557pub fn lint(tariff: &Checked<'_>) -> lint::tariff::Report {
558    lint::tariff(tariff)
559}
560
561/// Fix the warnings raised from linting the given `tariff` and return the fixed JSON.
562///
563/// A caller reaches `fix` by passing the tariff through [`json::parse_object`] and [`check`],
564/// as for [`lint`](lint()).
565///
566/// Both the schema warnings and the lint warnings are fixed, and the document is walked again
567/// after each round of edits, because one fix can raise more warnings. The fixing stops when a
568/// walk doesn't propose any edit that `options` allows. See [`lint::fix::Outcome`].
569///
570/// `options` says which fixes to apply. By default the fixing leaves out every edit that changes
571/// what the tariff means under OCPI, and [`lint::fix::Fixed::changes_held_back`] counts the
572/// edits it left out.
573///
574/// Whatever the options, the fixing refuses a rename whose new name would clash with the
575/// name of another field in the same object, and [`lint::fix::Fixed::refused`] lists it.
576///
577/// Nothing is written: the fixed JSON is returned as [`lint::fix::Fixed::source`], and a tariff
578/// with nothing to fix returns its own source unchanged.
579///
580/// # Examples
581///
582/// ```rust
583/// # use ocpi_tariffs::{json, lint, tariff, Version};
584/// #
585/// # const TARIFF_JSON: &str = include_str!("tariff.json");
586///
587/// let doc = json::parse_object(TARIFF_JSON)?;
588/// let tariff = tariff::check(doc, Version::V221).ignore_warnings();
589///
590/// let options =
591///     lint::fix::Options::default().unexpected_fields(lint::fix::UnexpectedFields::Remove);
592/// let fixed = tariff::fix(&tariff, options)?;
593///
594/// eprintln!(
595///     "Applied {} edit(s) over {} pass(es), held back {}",
596///     fixed.edits, fixed.passes, fixed.changes_held_back
597/// );
598///
599/// println!("{}", fixed.source);
600///
601/// # Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
602/// ```
603pub fn fix(
604    tariff: &Checked<'_>,
605    options: lint::fix::Options,
606) -> Result<lint::fix::Fixed, crate::fix::Error> {
607    lint::fix::tariff(tariff, options)
608}
609
610/// Explain the given tariff in the given language, returning the explanation as Markdown.
611///
612/// A caller reaches `explain` by passing the tariff through [`json::parse_object`], [`check`]
613/// and [`build`].
614///
615/// A `v2.1.1` tariff is explained as its `v2.2.1` equivalent, because [`build`] converts a
616/// `v2.1.1` tariff to `v2.2.1`.
617///
618/// `explain` describes the tariff as given. It finds no fault in the tariff and proposes no edit to
619/// it, so an element whose restrictions exclude every session is described like any other.
620/// Reporting that such an element is pointless belongs to [`tariff::lint`](lint()), and repairing
621/// the tariff belongs to [`tariff::fix`](fix()). The [`explain`](mod@crate::explain) module
622/// documentation lists what the explanation does leave out.
623///
624/// # Examples
625///
626/// ```rust
627/// # use ocpi_tariffs::{json, tariff, Language, Version};
628/// #
629/// # const TARIFF_JSON: &str = include_str!("tariff.json");
630///
631/// let doc = json::parse_object(TARIFF_JSON)?;
632/// let tariff = tariff::check(doc, Version::V211).ignore_warnings();
633/// let Ok(tariff) = tariff::build(tariff) else {
634///     return Err("The tariff could not be read well enough to explain.".into());
635/// };
636///
637/// println!("{}", tariff::explain(&tariff.ignore_warnings(), Language::EnUS));
638///
639/// # Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
640/// ```
641pub fn explain(tariff: &Tariff<'_>, language: crate::Language) -> String {
642    explain::tariff(tariff.as_v221(), language)
643}