Crate ocpi_tariffs

Crate ocpi_tariffs 

Source
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.

§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. The ToDuration and ToHoursDecimal traits can be used to convert a TimeDelta into a Decimal and vice versa.
guess
Guess the Version of the given CDR or 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 Decimal and 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 DayOfWeek type.

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.
OutOfRange
Out of range error type used in various converting APIs
ParseError
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 Kind and the associated json::Element.

Enums§

ObjectType
The type of OCPI objects that can be parsed.
ParseErrorKind
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.
VerdictExt
Verdict specific extinsion methods for the Result type.
Versioned
An object for a specific OCPI Version.

Type Aliases§

TariffId
The Id for a tariff used in the pricing of a CDR.
UnexpectedFields
Set of unexpected fields encountered while parsing a CDR or tariff.
Verdict
A Verdict is a standard Result with Warnings potentially issued for both the Ok and Err variants.