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.
- Use
json::parse_objectto parse a CDR or tariff&strinto ajson::Document. - Use
cdr::infer_versionortariff::infer_versionto guess which OCPIVersiona CDR or tariff is. - Use the
cdr::buildandtariff::buildfunctions to check ajson::Documentagainst the schema for a given version. - Use the
tariff::lintto 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:
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}");
}
}
}
§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_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}");
}
}
}
§Lint a tariff
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!();
}
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
TimeDelta. TheToDurationandToHoursDecimaltraits can be used to convert aTimeDeltainto aDecimaland vice versa. - enumeration
- Types to parse all OCPI enumerations.
- explain
- Explain an OCPI object in human language.
- generate
- Generate a CDR from a tariff.
- guess
- Guess the
Versionof the givenCDRor tariff. - json
- JSON parsing and typed decoding for the OCPI CDR pricing/generating and CDR/Tariff linting pipeline.
- lint
- Both tariffs and CDRs 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 a 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
- string
- Case Insensitive String. Only printable ASCII allowed.
- tariff
- Parse a tariff and lint the result.
- timezone
- Parse an IANA Timezone from JSON or find a timezone in a CDR.
- 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.
Macros§
- define_
enum_ from_ json - This macro defines the
FromJsonimpl for the given enum.
Structs§
- Ampere
- A value of amperes.
- Caveat
- A value that may have associated
Warnings. - 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§
- Version
- The OCPI versions supported by this crate.
Traits§
- Cost
- An item that has a cost.
- ToDuration
- Convert a
Decimalamount of hours to aTimeDelta. - ToHours
Decimal - Convert a
TimeDeltainto aDecimalbased amount of hours. - Unversioned
- An object with an uncertain
Version. - Verdict
Ext Verdictspecific extension methods for theResulttype.- Versioned
- An object for a specific OCPI
Version. - Warning
- Each mod defines warnings for the type that it’s trying to parse or lint from a
json::Element.