Expand description
§OCPI Tariffs library
Calculate the (sub)totals of a charge session
using the cdr::price function and use the generated price::Report to review and compare the calculated
totals versus the sources from the CDR.
A CDR or a tariff goes through four steps: parse, check, build and use. Each step after parsing returns its own warnings, so a caller can tell which step found a fault.
&str
| json::parse_object
v
json::Document
| cdr::check, tariff::check
v
cdr::Checked, tariff::Checked --> tariff::lint, tariff::fix, timezone::find_or_infer
| cdr::build, tariff::build
v
cdr::Cdr, tariff::Tariff --> cdr::price, tariff::explain, cdr::generate_from_tariffThe check takes the OCPI version as well as the document.
When the version isn’t known, cdr::infer_version or tariff::infer_version guesses it
from the document first.
A built cdr::Cdr builds the tariffs it contains with cdr::Cdr::tariffs.
- Use
json::parse_objectto parse a CDR or tariff&strinto ajson::Document. - Use the
cdr::checkandtariff::checkfunctions to check ajson::Documentagainst the schema for a given version. - Use
cdr::buildto read a CDR into thecdr::Cdrthatcdr::pricetakes. - Use
tariff::buildto read a tariff into thetariff::Tariffthattariff::explainandcdr::generate_from_tarifftake. - Use
tariff::lintto lint a tariff: flag the constructs a schema-valid tariff can still get wrong. - Use
tariff::fixto apply the fixes for the schema and lint warnings, and get the fixed JSON back. - Use
timezone::find_or_inferto find the timezone thatcdr::priceneeds, from a checked CDR. - Use
tariff::explainto describe a tariff in human language. Finding fault with a tariff istariff::lint’s job.
§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:
let doc = json::parse_object(CDR_JSON)?;
let cdr = cdr::check(doc, Version::V211).ignore_warnings();
let Ok(cdr) = cdr::build(cdr) else {
return Err("The CDR could not be read well enough to price.".into());
};
let cdr = cdr.ignore_warnings();
let Ok(tariffs) = cdr.tariffs() else {
return Err("The tariffs the CDR contains could not be read well enough to price.".into());
};
let tariffs = tariffs.ignore_warnings();
let Ok(report) = cdr::price(&cdr, &tariffs, &jiff::tz::get!("Europe/Amsterdam")) else {
return Err("The CDR could not be priced.".into());
};
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}");
}
}
}
§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:
let cdr_doc = json::parse_object(CDR_JSON)?;
let cdr = cdr::check(cdr_doc, Version::V211).ignore_warnings();
let Ok(cdr) = cdr::build(cdr) else {
return Err("The CDR could not be read well enough to price.".into());
};
let cdr = cdr.ignore_warnings();
let tariff_doc = json::parse_object(TARIFF_JSON)?;
let (tariff, _tariff_warnings) = tariff::check(tariff_doc, Version::V211).into_parts();
let Ok(tariff) = tariff::build(tariff) else {
return Err("The tariff could not be read well enough to price.".into());
};
let tariff = tariff.ignore_warnings();
let Ok(report) = cdr::price(&cdr, &[tariff], &jiff::tz::get!("Europe/Amsterdam")) else {
return Err("The CDR could not be priced.".into());
};
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}");
}
}
}
Modules§
- cdr
- Parse a CDR and price the result with a tariff.
- country
- An
ISO 3166-1country code. - currency
- An ISO 4217 currency code.
- datetime
- Timestamps are formatted as a string with a max length of 25 chars. Each timestamp follows RFC 3339, with some additional limitations. All timestamps are expected to be in UTC. The absence of the timezone designator implies a UTC timestamp. Fractional seconds may be used.
- duration
- The OCPI spec represents some durations as fractional hours, where this crate represents all
durations using
SignedDuration. TheToDurationandToHoursDecimaltraits can be used to convert aSignedDurationinto aDecimaland vice versa. - explain
- Explain an OCPI object in human language.
- fix
- The errors and refusals that fixing a tariff reports.
- generate
- Generate a CDR from a tariff.
- json
- JSON parsing and typed decoding for the OCPI CDR pricing/generating and CDR/Tariff linting pipeline.
- lint
- A tariff can be linted - a term borrowed from software development where linting is the act of flagging common errors, bugs, dangerous constructs and stylistic flaws in some code or data.
- money
- Various monetary types.
- number
- We represent the OCPI spec Number as a
Decimaland serialize and deserialize to the precision defined in the OCPI spec. - price
- Price a CDR using a tariff and compare the prices embedded in the CDR with the prices computed here.
- schema
- Check a
json::Documentagainst the OCPI schema for a given version. - string
- Case Insensitive String. Only printable ASCII allowed.
- tariff
- Parse a tariff.
- timezone
- Parse an IANA Timezone from JSON or find a timezone in a CDR.
- version
- The OCPI versions this crate supports, and the guess at which version a CDR or tariff is.
- warning
- These types are the basis for writing functions that can emit a set of
Warnings based on the value they are trying to create. - weekday
- The Warning infrastructure for the OCPI
DayOfWeektype.
Structs§
- Ampere
- A value of amperes.
- Caveat
- A value that may have associated
Warnings. - Failure
- A failed attempt to make a value: the
Errorthat stopped it, and everyWarningraised before it. - Kw
- A value of kilo watts.
- Kwh
- A value of kilo watt hours.
- Money
- A monetary amount, the currency is dependent on the specified tariff.
- Price
- A price consisting of a value including VAT, and a value excluding VAT.
- Vat
- A VAT percentage.
Enums§
- Language
- A language an explanation can be rendered in. All supported languages are Germanic.
- Version
- The OCPI versions supported by this crate.
Traits§
- Cost
- An item that has a cost.
- ToDuration
- Convert a
Decimalamount of hours to aSignedDuration. - ToHours
Decimal - Convert a
SignedDurationinto aDecimalbased amount of hours. - Verdict
Ext Verdictspecific extension methods for theResulttype.- Warning
- Each mod defines warnings for the type that it’s trying to parse or lint from a JSON element.