ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
use rust_decimal_macros::dec;

use crate::{Kwh, Money};

/// `{:.4}` truncates, so a value printed straight from a tariff lost its last digit while
/// the same value printed after being rounded on the way out kept it, and one report showed two
/// answers for one number.
#[test]
fn should_round_rather_than_truncate() {
    assert_eq!(Money::from(dec!(0.50019)).to_string(), "0.5002¤");
    assert_eq!(Money::from(dec!(0.20839)).to_string(), "0.2084¤");
    assert_eq!(Kwh::from(dec!(0.00018)).to_string(), "0.0002kWh");
}

/// A tie is where the choice of strategy shows; `number::SCALE` states why it goes even.
#[test]
fn should_print_a_tie_rounded_to_the_even_digit() {
    assert_eq!(Money::from(dec!(0.50015)).to_string(), "0.5002¤");
    assert_eq!(Money::from(dec!(0.25005)).to_string(), "0.2500¤");
    assert_eq!(Kwh::from(dec!(0.00015)).to_string(), "0.0002kWh");
    assert_eq!(Kwh::from(dec!(0.00025)).to_string(), "0.0002kWh");
}

#[test]
fn should_print_a_negative_tie_rounded_to_the_even_digit() {
    assert_eq!(Money::from(dec!(-0.50015)).to_string(), "-0.5002¤");
    assert_eq!(Money::from(dec!(-0.25005)).to_string(), "-0.2500¤");
}

/// The alternate form drops the unit and must round identically.
#[test]
fn should_round_the_alternate_form_the_same_way() {
    assert_eq!(format!("{:#}", Money::from(dec!(0.50015))), "0.5002");
    assert_eq!(format!("{:#}", Money::from(dec!(0.25005))), "0.2500");
}

/// A rate too small to survive the scale must still print as a number. Testing the
/// rounded value for zero instead of the given one would collapse this to a bare "0" and read as
/// free.
#[test]
fn should_not_print_a_tiny_rate_as_a_bare_zero() {
    assert_eq!(Money::from(dec!(0.00004)).to_string(), "0.0000¤");
    assert_eq!(Kwh::from(dec!(0.00001)).to_string(), "0.0000kWh");
}

/// An exact zero keeps its short form, so an unpriced dimension does not read as a rate.
#[test]
fn should_print_an_exact_zero_without_decimals() {
    assert_eq!(Money::from(dec!(0)).to_string(), "0¤");
    assert_eq!(format!("{:#}", Money::from(dec!(0))), "0");
}