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.
See the price::Report for a detailed list of all the fields that help analyze and valitate the pricing of a CDR.
- Use the
cdr::parseandtariff::parsefunction to parse and guess which OCPI version of a CDR or tariff you have. - Use the
cdr::parse_with_versionandtariff::parse_with_versionfunctions to parse a CDR of tariff as the 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 report = cdr::parse_with_version(CDR_JSON, Version::V211)?;
let cdr::ParseReport {
cdr,
unexpected_fields,
} = report;
let report = cdr::price(cdr, price::TariffSource::UseCdr, chrono_tz::Tz::Europe__Amsterdam)?;
if !report.warnings.is_empty() {
eprintln!("Pricing the CDR resulted in `{}` warnings", report.warnings.len());
for price::WarningReport { kind } in &report.warnings {
eprintln!(" - {kind}");
}
}
§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 report = cdr::parse_with_version(CDR_JSON, Version::V211)?;
let cdr::ParseReport {
cdr,
unexpected_fields,
} = report;
let report = cdr::price(cdr, price::TariffSource::Override(vec![TARIFF_JSON.to_string()]), chrono_tz::Tz::Europe__Amsterdam)?;
if !report.warnings.is_empty() {
eprintln!("Pricing the CDR resulted in `{}` warnings", report.warnings.len());
for price::WarningReport { kind } in &report.warnings {
eprintln!(" - {kind}");
}
}
§Lint a tariff
let report = tariff::parse_and_report(TARIFF_JSON)?;
let guess::Report {
unexpected_fields,
version,
} = report;
if !unexpected_fields.is_empty() {
eprintln!("Strange... there are fields in the tariff that are not defined in the spec.");
for path in &unexpected_fields {
eprintln!(" * {path}");
}
eprintln!();
}
let guess::Version::Certain(tariff) = version else {
return Err("Unable to guess the version of given CDR JSON.".into());
};
let report = tariff::lint(&tariff)?;
let warnings = report.into_warning_report();
eprintln!("`{}` lint warnings found", warnings.len());
for warning::ElementReport { element, warnings } in warnings.iter(tariff.as_element()) {
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-1 country 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. - guess
- Guess the
Versionof the givenCDRor tariff. - json
- Types and functions for parsing JSON in a flexible and pedantic manner.
- 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.
- 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.
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 dependant on the specified tariff.
- OutOf
Range - Out of range error type used in various converting APIs
- Parse
Error - Errors that can happen if a JSON str is parsed.
- Price
- A price consisting of a value including VAT, and a value excluding VAT.
- Vat
- A VAT percentage.
- Warning
- Groups together a module’s
Kindand the associatedjson::Element.
Enums§
- Object
Type - The type of OCPI objects that can be parsed.
- Parse
Error Kind - The kind of Error that occurred.
- VatApplicable
- A VAT percentage with embedded information about whether it’s applicable, inapplicable or unknown.
- Version
- The OCPI versions supported by this crate
Traits§
- Cost
- A item that has a cost.
- Unversioned
- An object with an uncertain
Version. - Verdict
Ext Verdictspecific extinsion methods for theResulttype.- Versioned
- An object for a specific OCPI
Version.
Type Aliases§
- Tariff
Id - The Id for a tariff used in the pricing of a CDR.
- Unexpected
Fields - Set of unexpected fields encountered while parsing a CDR or tariff.
- Verdict
- A
Verdictis a standardResultwithWarnings potentially issued for both theOkandErrvariants.