ocpi-tariffs 0.53.0

OCPI tariff calculations
Documentation
use super::Code;

impl Code {
    /// Return a `country::Code` for the given `str`.
    ///
    /// # Panics
    ///
    /// Will panic when the given `str` is not two bytes long or an unknown country code.
    pub fn from_alpha_2_str(code: &str) -> Self {
        let bytes = code.as_bytes();

        // ISO 3166-1 is expected to be 2 chars.
        let [a, b] = bytes else {
            panic!(
                "Unable to parse country code. Expected length of 2 chars. It has length: `{}`",
                code.len()
            );
        };

        let couplet: [u8; 2] = [a.to_ascii_uppercase(), b.to_ascii_uppercase()];

        let Some(code) = Code::from_alpha_2(couplet) else {
            panic!("Unknown country code `{code}`");
        };

        code
    }
}

#[test]
fn alpha2_country_code_matches() {
    let code = Code::from_alpha_2(*b"NL").unwrap();

    assert_eq!(Code::Nl, code);
    assert_eq!("NL", code.into_alpha_2_str());
}

#[test]
fn alpha3_country_code_matches() {
    let code = Code::from_alpha_3(*b"NLD").unwrap();

    assert_eq!(Code::Nl, code);
}