use alloc::borrow::Cow;
use core::fmt;
use crate::json::RawStr;
fn decoded_or_raw<'a, T>(
decoded: &str,
raw: &'a str,
table: impl Fn(&str) -> Option<T>,
other: fn(&'a str) -> T,
) -> T {
table(decoded).unwrap_or_else(|| other(raw))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MeterState {
NotPresent,
Ok,
Timeout,
Disconnected,
NotFound,
Manipulated,
Exchanged,
Incompatible,
OutOfRange,
Substitute,
OtherError,
ReadError,
Undefined(char),
}
impl MeterState {
#[must_use]
pub fn parse(s: &str) -> Self {
let mut chars = s.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => match c {
'N' => Self::NotPresent,
'G' => Self::Ok,
'T' => Self::Timeout,
'D' => Self::Disconnected,
'R' => Self::NotFound,
'M' => Self::Manipulated,
'X' => Self::Exchanged,
'I' => Self::Incompatible,
'O' => Self::OutOfRange,
'S' => Self::Substitute,
'E' => Self::OtherError,
'F' => Self::ReadError,
other => Self::Undefined(other),
},
_ => Self::Undefined('?'),
}
}
#[must_use]
pub const fn is_defined(self) -> bool {
!matches!(self, Self::Undefined(_))
}
#[must_use]
pub const fn letter(self) -> char {
match self {
Self::NotPresent => 'N',
Self::Ok => 'G',
Self::Timeout => 'T',
Self::Disconnected => 'D',
Self::NotFound => 'R',
Self::Manipulated => 'M',
Self::Exchanged => 'X',
Self::Incompatible => 'I',
Self::OutOfRange => 'O',
Self::Substitute => 'S',
Self::OtherError => 'E',
Self::ReadError => 'F',
Self::Undefined(c) => c,
}
}
#[must_use]
pub const fn is_ok(self) -> bool {
matches!(self, Self::Ok)
}
#[must_use]
pub const fn identifier(self) -> &'static str {
match self {
Self::NotPresent => "NOT_PRESENT",
Self::Ok => "OK",
Self::Timeout => "TIMEOUT",
Self::Disconnected => "DISCONNECTED",
Self::NotFound => "NOT_FOUND",
Self::Manipulated => "MANIPULATED",
Self::Exchanged => "EXCHANGED",
Self::Incompatible => "INCOMPATIBLE",
Self::OutOfRange => "OUT_OF_RANGE",
Self::Substitute => "SUBSTITUTE",
Self::OtherError => "OTHER_ERROR",
Self::ReadError => "READ_ERROR",
Self::Undefined(_) => "UNDEFINED",
}
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for MeterState {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&alloc::string::String::from(self.letter()))
}
}
impl fmt::Display for MeterState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.letter())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TransactionMarker {
Begin,
Charging,
Exception,
End,
EndedLocally,
EndedRemotely,
Aborted,
PowerFailure,
Suspended,
TariffChange,
Undefined(char),
}
impl TransactionMarker {
#[must_use]
pub fn parse(s: &str) -> Self {
let mut chars = s.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => match c {
'B' => Self::Begin,
'C' => Self::Charging,
'X' => Self::Exception,
'E' => Self::End,
'L' => Self::EndedLocally,
'R' => Self::EndedRemotely,
'A' => Self::Aborted,
'P' => Self::PowerFailure,
'S' => Self::Suspended,
'T' => Self::TariffChange,
other => Self::Undefined(other),
},
_ => Self::Undefined('?'),
}
}
#[must_use]
pub const fn is_defined(self) -> bool {
!matches!(self, Self::Undefined(_))
}
#[must_use]
pub const fn letter(self) -> char {
match self {
Self::Begin => 'B',
Self::Charging => 'C',
Self::Exception => 'X',
Self::End => 'E',
Self::EndedLocally => 'L',
Self::EndedRemotely => 'R',
Self::Aborted => 'A',
Self::PowerFailure => 'P',
Self::Suspended => 'S',
Self::TariffChange => 'T',
Self::Undefined(c) => c,
}
}
#[must_use]
pub const fn is_begin(self) -> bool {
matches!(self, Self::Begin)
}
#[must_use]
pub const fn is_end(self) -> bool {
matches!(
self,
Self::End
| Self::EndedLocally
| Self::EndedRemotely
| Self::Aborted
| Self::PowerFailure
)
}
#[must_use]
pub const fn is_fault(self) -> bool {
matches!(self, Self::Aborted | Self::PowerFailure | Self::Exception)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for TransactionMarker {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&alloc::string::String::from(self.letter()))
}
}
impl fmt::Display for TransactionMarker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.letter())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ErrorFlags<'a> {
raw: Cow<'a, str>,
}
impl<'a> ErrorFlags<'a> {
#[must_use]
pub const fn new(raw: Cow<'a, str>) -> Self {
Self { raw }
}
#[must_use]
pub const fn borrowed(raw: &'a str) -> Self {
Self {
raw: Cow::Borrowed(raw),
}
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.raw
}
#[must_use]
pub fn energy_unusable(&self) -> bool {
self.raw.contains('E')
}
#[must_use]
pub fn time_unusable(&self) -> bool {
self.raw.contains('t')
}
#[must_use]
pub fn any(&self) -> bool {
!self.raw.is_empty()
}
pub fn undefined(&self) -> impl Iterator<Item = char> + '_ {
self.raw.chars().filter(|c| !matches!(c, 'E' | 't'))
}
}
impl fmt::Display for ErrorFlags<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.raw)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Unit<'a> {
KWh,
Wh,
MilliOhm,
MicroOhm,
Other(&'a str),
}
impl<'a> Unit<'a> {
#[must_use]
pub const fn parse(s: &'a str) -> Self {
if str_eq(s, "kWh") {
Self::KWh
} else if str_eq(s, "Wh") {
Self::Wh
} else if str_eq(s, "mOhm") {
Self::MilliOhm
} else if str_eq(s, "uOhm") {
Self::MicroOhm
} else {
Self::Other(s)
}
}
#[must_use]
pub fn parse_decoded(s: &RawStr<'a>) -> Self {
Self::from_parts(&s.decode(), s.as_raw())
}
#[must_use]
pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
decoded_or_raw(
decoded,
raw,
|d| match d {
"kWh" => Some(Self::KWh),
"Wh" => Some(Self::Wh),
"mOhm" => Some(Self::MilliOhm),
"uOhm" => Some(Self::MicroOhm),
_ => None,
},
Self::Other,
)
}
#[must_use]
pub const fn is_defined(&self) -> bool {
!matches!(self, Self::Other(_))
}
#[must_use]
pub const fn as_str(&self) -> &'a str {
match self {
Self::KWh => "kWh",
Self::Wh => "Wh",
Self::MilliOhm => "mOhm",
Self::MicroOhm => "uOhm",
Self::Other(s) => s,
}
}
#[must_use]
pub const fn is_energy(&self) -> bool {
matches!(self, Self::KWh | Self::Wh)
}
#[must_use]
pub const fn watt_hours_scale(&self) -> Option<u32> {
match self {
Self::KWh => Some(1000),
Self::Wh => Some(1),
_ => None,
}
}
}
const fn str_eq(a: &str, b: &str) -> bool {
let (a, b) = (a.as_bytes(), b.as_bytes());
if a.len() != b.len() {
return false;
}
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}
impl fmt::Display for Unit<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CurrentType<'a> {
Ac,
Dc,
Other(&'a str),
}
impl<'a> CurrentType<'a> {
#[must_use]
pub const fn parse(s: &'a str) -> Self {
if str_eq(s, "AC") {
Self::Ac
} else if str_eq(s, "DC") {
Self::Dc
} else {
Self::Other(s)
}
}
#[must_use]
pub fn parse_decoded(s: &RawStr<'a>) -> Self {
Self::from_parts(&s.decode(), s.as_raw())
}
#[must_use]
pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
decoded_or_raw(
decoded,
raw,
|d| match d {
"AC" => Some(Self::Ac),
"DC" => Some(Self::Dc),
_ => None,
},
Self::Other,
)
}
#[must_use]
pub const fn is_defined(&self) -> bool {
!matches!(self, Self::Other(_))
}
#[must_use]
pub const fn as_str(&self) -> &'a str {
match self {
Self::Ac => "AC",
Self::Dc => "DC",
Self::Other(s) => s,
}
}
}
impl fmt::Display for CurrentType<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IdentificationLevel<'a> {
None,
Hearsay,
Trusted,
Verified,
Certified,
Secure,
Mismatch,
Invalid,
Outdated,
Unknown,
Other(&'a str),
}
impl<'a> IdentificationLevel<'a> {
#[must_use]
pub fn parse(s: &'a str) -> Self {
match s {
"NONE" => Self::None,
"HEARSAY" => Self::Hearsay,
"TRUSTED" => Self::Trusted,
"VERIFIED" => Self::Verified,
"CERTIFIED" => Self::Certified,
"SECURE" => Self::Secure,
"MISMATCH" => Self::Mismatch,
"INVALID" => Self::Invalid,
"OUTDATED" => Self::Outdated,
"UNKNOWN" => Self::Unknown,
other => Self::Other(other),
}
}
#[must_use]
pub fn parse_decoded(s: &RawStr<'a>) -> Self {
Self::from_parts(&s.decode(), s.as_raw())
}
#[must_use]
pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
decoded_or_raw(
decoded,
raw,
|d| {
Some(match d {
"NONE" => Self::None,
"HEARSAY" => Self::Hearsay,
"TRUSTED" => Self::Trusted,
"VERIFIED" => Self::Verified,
"CERTIFIED" => Self::Certified,
"SECURE" => Self::Secure,
"MISMATCH" => Self::Mismatch,
"INVALID" => Self::Invalid,
"OUTDATED" => Self::Outdated,
"UNKNOWN" => Self::Unknown,
_ => return None,
})
},
Self::Other,
)
}
#[must_use]
pub const fn is_defined(&self) -> bool {
!matches!(self, Self::Other(_))
}
#[must_use]
pub const fn as_str(&self) -> &'a str {
match self {
Self::None => "NONE",
Self::Hearsay => "HEARSAY",
Self::Trusted => "TRUSTED",
Self::Verified => "VERIFIED",
Self::Certified => "CERTIFIED",
Self::Secure => "SECURE",
Self::Mismatch => "MISMATCH",
Self::Invalid => "INVALID",
Self::Outdated => "OUTDATED",
Self::Unknown => "UNKNOWN",
Self::Other(s) => s,
}
}
#[must_use]
pub const fn is_error(&self) -> bool {
matches!(
self,
Self::Mismatch | Self::Invalid | Self::Outdated | Self::Unknown
)
}
}
impl fmt::Display for IdentificationLevel<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IdentificationType<'a> {
None,
Denied,
Undefined,
Iso14443,
Iso15693,
Emaid,
Evccid,
Evcoid,
Iso7812,
CardTxnNr,
Central,
Central1,
Central2,
Local,
Local1,
Local2,
PhoneNumber,
KeyCode,
Other(&'a str),
}
impl<'a> IdentificationType<'a> {
#[must_use]
pub fn parse(s: &'a str) -> Self {
match s {
"NONE" => Self::None,
"DENIED" => Self::Denied,
"UNDEFINED" => Self::Undefined,
"ISO14443" => Self::Iso14443,
"ISO15693" => Self::Iso15693,
"EMAID" => Self::Emaid,
"EVCCID" => Self::Evccid,
"EVCOID" => Self::Evcoid,
"ISO7812" => Self::Iso7812,
"CARD_TXN_NR" => Self::CardTxnNr,
"CENTRAL" => Self::Central,
"CENTRAL_1" => Self::Central1,
"CENTRAL_2" => Self::Central2,
"LOCAL" => Self::Local,
"LOCAL_1" => Self::Local1,
"LOCAL_2" => Self::Local2,
"PHONE_NUMBER" => Self::PhoneNumber,
"KEY_CODE" => Self::KeyCode,
other => Self::Other(other),
}
}
#[must_use]
pub fn parse_decoded(s: &RawStr<'a>) -> Self {
Self::from_parts(&s.decode(), s.as_raw())
}
#[must_use]
pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
decoded_or_raw(
decoded,
raw,
|d| {
Some(match d {
"NONE" => Self::None,
"DENIED" => Self::Denied,
"UNDEFINED" => Self::Undefined,
"ISO14443" => Self::Iso14443,
"ISO15693" => Self::Iso15693,
"EMAID" => Self::Emaid,
"EVCCID" => Self::Evccid,
"EVCOID" => Self::Evcoid,
"ISO7812" => Self::Iso7812,
"CARD_TXN_NR" => Self::CardTxnNr,
"CENTRAL" => Self::Central,
"CENTRAL_1" => Self::Central1,
"CENTRAL_2" => Self::Central2,
"LOCAL" => Self::Local,
"LOCAL_1" => Self::Local1,
"LOCAL_2" => Self::Local2,
"PHONE_NUMBER" => Self::PhoneNumber,
"KEY_CODE" => Self::KeyCode,
_ => return None,
})
},
Self::Other,
)
}
#[must_use]
pub const fn is_defined(&self) -> bool {
!matches!(self, Self::Other(_))
}
#[must_use]
pub fn data_is_well_formed(&self, data: &str) -> Option<bool> {
let hex =
|n: [usize; 2]| n.contains(&data.len()) && data.bytes().all(|c| c.is_ascii_hexdigit());
Some(match self {
Self::Iso14443 => hex([8, 14]),
Self::Iso15693 => hex([16, 16]),
Self::Emaid => matches!(data.chars().count(), 14 | 15),
Self::Evccid => data.chars().count() <= 6,
Self::PhoneNumber => data.starts_with('+'),
_ => return None,
})
}
#[must_use]
pub const fn as_str(&self) -> &'a str {
match self {
Self::None => "NONE",
Self::Denied => "DENIED",
Self::Undefined => "UNDEFINED",
Self::Iso14443 => "ISO14443",
Self::Iso15693 => "ISO15693",
Self::Emaid => "EMAID",
Self::Evccid => "EVCCID",
Self::Evcoid => "EVCOID",
Self::Iso7812 => "ISO7812",
Self::CardTxnNr => "CARD_TXN_NR",
Self::Central => "CENTRAL",
Self::Central1 => "CENTRAL_1",
Self::Central2 => "CENTRAL_2",
Self::Local => "LOCAL",
Self::Local1 => "LOCAL_1",
Self::Local2 => "LOCAL_2",
Self::PhoneNumber => "PHONE_NUMBER",
Self::KeyCode => "KEY_CODE",
Self::Other(s) => s,
}
}
}
impl fmt::Display for IdentificationType<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IdentificationFlag<'a> {
Rfid(&'a str),
Ocpp(&'a str),
Iso15118(&'a str),
Plmn(&'a str),
Other(&'a str),
}
impl<'a> IdentificationFlag<'a> {
pub const DEFINED: [&'static str; 17] = [
"RFID_NONE",
"RFID_PLAIN",
"RFID_RELATED",
"RFID_PSK",
"OCPP_NONE",
"OCPP_RS",
"OCPP_AUTH",
"OCPP_RS_TLS",
"OCPP_AUTH_TLS",
"OCPP_CACHE",
"OCPP_WHITELIST",
"OCPP_CERTIFIED",
"ISO15118_NONE",
"ISO15118_PNC",
"PLMN_NONE",
"PLMN_RING",
"PLMN_SMS",
];
#[must_use]
pub fn parse(s: &'a str) -> Self {
if !Self::DEFINED.contains(&s) {
return Self::Other(s);
}
if s.starts_with("RFID_") {
Self::Rfid(s)
} else if s.starts_with("OCPP_") {
Self::Ocpp(s)
} else if s.starts_with("ISO15118_") {
Self::Iso15118(s)
} else {
Self::Plmn(s)
}
}
#[must_use]
pub fn parse_decoded(s: &RawStr<'a>) -> Self {
let raw = s.as_raw();
let decoded = s.decode();
let group = match IdentificationFlag::parse(decoded.as_ref()) {
IdentificationFlag::Rfid(_) => 0u8,
IdentificationFlag::Ocpp(_) => 1,
IdentificationFlag::Iso15118(_) => 2,
IdentificationFlag::Plmn(_) => 3,
IdentificationFlag::Other(_) => 4,
};
match group {
0 => Self::Rfid(raw),
1 => Self::Ocpp(raw),
2 => Self::Iso15118(raw),
3 => Self::Plmn(raw),
_ => Self::Other(raw),
}
}
#[must_use]
pub const fn group(&self) -> Option<u8> {
Some(match self {
Self::Rfid(_) => 0,
Self::Ocpp(_) => 1,
Self::Iso15118(_) => 2,
Self::Plmn(_) => 3,
Self::Other(_) => return None,
})
}
#[must_use]
pub const fn is_defined(&self) -> bool {
!matches!(self, Self::Other(_))
}
#[must_use]
pub const fn as_str(&self) -> &'a str {
match self {
Self::Rfid(s) | Self::Ocpp(s) | Self::Iso15118(s) | Self::Plmn(s) | Self::Other(s) => s,
}
}
}
impl fmt::Display for IdentificationFlag<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ChargePointIdType<'a> {
EvseId,
ChargeBoxAndConnector,
Other(&'a str),
}
impl<'a> ChargePointIdType<'a> {
#[must_use]
pub fn parse(s: &'a str) -> Self {
match s {
"EVSEID" => Self::EvseId,
"CBIDC" => Self::ChargeBoxAndConnector,
other => Self::Other(other),
}
}
#[must_use]
pub fn parse_decoded(s: &RawStr<'a>) -> Self {
Self::from_parts(&s.decode(), s.as_raw())
}
#[must_use]
pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
decoded_or_raw(
decoded,
raw,
|d| match d {
"EVSEID" => Some(Self::EvseId),
"CBIDC" => Some(Self::ChargeBoxAndConnector),
_ => None,
},
Self::Other,
)
}
#[must_use]
pub const fn is_defined(&self) -> bool {
!matches!(self, Self::Other(_))
}
#[must_use]
pub fn id_is_well_formed(&self, id: &str) -> Option<bool> {
match self {
Self::ChargeBoxAndConnector => {
Some(id.split_once(' ').is_some_and(|(box_id, connector)| {
!box_id.is_empty() && !connector.is_empty() && !connector.contains(' ')
}))
}
_ => None,
}
}
#[must_use]
pub const fn as_str(&self) -> &'a str {
match self {
Self::EvseId => "EVSEID",
Self::ChargeBoxAndConnector => "CBIDC",
Self::Other(s) => s,
}
}
}
impl fmt::Display for ChargePointIdType<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
#[test]
fn every_meter_state_letter_round_trips() {
for c in "NGTDRMXIOSEF".chars() {
let st = MeterState::parse(&c.to_string());
assert_eq!(st.letter(), c);
assert_eq!(st.is_ok(), c == 'G');
}
}
#[test]
fn an_undefined_meter_state_is_never_ok() {
let st = MeterState::parse("Q");
assert_eq!(st, MeterState::Undefined('Q'));
assert!(!st.is_ok());
assert_eq!(st.letter(), 'Q');
}
#[test]
fn all_five_spellings_of_an_ending_are_endings() {
for c in "ELRAP".chars() {
assert!(TransactionMarker::parse(&c.to_string()).is_end(), "{c}");
}
for c in "BCXST".chars() {
assert!(!TransactionMarker::parse(&c.to_string()).is_end(), "{c}");
}
assert!(TransactionMarker::parse("B").is_begin());
assert!(!TransactionMarker::parse("Q").is_begin());
}
#[test]
fn error_flags_keep_what_they_cannot_interpret() {
let f = ErrorFlags::borrowed("Et");
assert!(f.energy_unusable() && f.time_unusable() && f.any());
let f = ErrorFlags::borrowed("");
assert!(!f.any());
let f = ErrorFlags::borrowed("Q");
assert!(f.any(), "an undefined flag is still a fault");
assert!(!f.energy_unusable());
assert_eq!(f.undefined().collect::<alloc::vec::Vec<_>>(), ['Q']);
}
#[test]
fn an_unknown_unit_is_not_energy() {
assert!(Unit::parse("kWh").is_energy());
assert!(Unit::parse("Wh").is_energy());
assert!(!Unit::parse("mOhm").is_energy());
assert!(!Unit::parse("MWh").is_energy());
assert_eq!(Unit::parse("MWh"), Unit::Other("MWh"));
}
#[test]
fn the_four_error_levels_are_the_ones_the_reference_refuses() {
for s in ["MISMATCH", "INVALID", "OUTDATED", "UNKNOWN"] {
assert!(IdentificationLevel::parse(s).is_error(), "{s}");
}
for s in [
"NONE",
"HEARSAY",
"TRUSTED",
"VERIFIED",
"CERTIFIED",
"SECURE",
] {
assert!(!IdentificationLevel::parse(s).is_error(), "{s}");
}
}
#[test]
fn identification_flags_are_grouped_by_their_table() {
assert!(matches!(
IdentificationFlag::parse("RFID_PLAIN"),
IdentificationFlag::Rfid(_)
));
assert!(matches!(
IdentificationFlag::parse("OCPP_RS_TLS"),
IdentificationFlag::Ocpp(_)
));
assert!(matches!(
IdentificationFlag::parse("ISO15118_PNC"),
IdentificationFlag::Iso15118(_)
));
assert!(matches!(
IdentificationFlag::parse("PLMN_SMS"),
IdentificationFlag::Plmn(_)
));
assert!(matches!(
IdentificationFlag::parse("VENDOR_X"),
IdentificationFlag::Other(_)
));
}
#[test]
fn an_escaped_table_value_still_matches_its_table() {
let raw = crate::json::parse_value(
r#"{"RU":"kWh","IT":"ISO14443","IL":"VERIFIED","CT":"EVSEID","IF":"RFID_PLAIN"}"#,
0,
&crate::Limits::DEFAULT,
&mut alloc::vec::Vec::new(),
)
.unwrap();
let o = raw.as_object().unwrap();
let f = |k: &str| o.get(k).unwrap().as_str().unwrap();
assert_eq!(Unit::parse_decoded(f("RU")), Unit::KWh);
assert!(Unit::parse_decoded(f("RU")).is_energy());
assert_eq!(
IdentificationType::parse_decoded(f("IT")),
IdentificationType::Iso14443
);
assert_eq!(
IdentificationLevel::parse_decoded(f("IL")),
IdentificationLevel::Verified
);
assert_eq!(
ChargePointIdType::parse_decoded(f("CT")),
ChargePointIdType::EvseId
);
assert!(matches!(
IdentificationFlag::parse_decoded(f("IF")),
IdentificationFlag::Rfid(_)
));
let raw = crate::json::parse_value(
r#"{"RU":"MWh"}"#,
0,
&crate::Limits::DEFAULT,
&mut alloc::vec::Vec::new(),
)
.unwrap();
let u = Unit::parse_decoded(
raw.as_object()
.unwrap()
.get("RU")
.unwrap()
.as_str()
.unwrap(),
);
assert_eq!(u, Unit::Other("MWh"));
}
#[test]
fn all_eighteen_identification_types_round_trip() {
let all = [
"NONE",
"DENIED",
"UNDEFINED",
"ISO14443",
"ISO15693",
"EMAID",
"EVCCID",
"EVCOID",
"ISO7812",
"CARD_TXN_NR",
"CENTRAL",
"CENTRAL_1",
"CENTRAL_2",
"LOCAL",
"LOCAL_1",
"LOCAL_2",
"PHONE_NUMBER",
"KEY_CODE",
];
assert_eq!(all.len(), 18, "[OCMF Tab. 17] has 18 rows");
for s in all {
assert_eq!(IdentificationType::parse(s).as_str(), s);
assert!(!matches!(
IdentificationType::parse(s),
IdentificationType::Other(_)
));
}
}
}