1use alloc::borrow::Cow;
12use core::fmt;
13
14use crate::json::RawStr;
15
16fn decoded_or_raw<'a, T>(
23 decoded: &str,
24 raw: &'a str,
25 table: impl Fn(&str) -> Option<T>,
26 other: fn(&'a str) -> T,
27) -> T {
28 table(decoded).unwrap_or_else(|| other(raw))
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33#[non_exhaustive]
34pub enum MeterState {
35 NotPresent,
37 Ok,
39 Timeout,
41 Disconnected,
43 NotFound,
45 Manipulated,
47 Exchanged,
49 Incompatible,
51 OutOfRange,
53 Substitute,
55 OtherError,
57 ReadError,
59 Undefined(char),
61}
62
63impl MeterState {
64 #[must_use]
66 pub fn parse(s: &str) -> Self {
67 let mut chars = s.chars();
68 match (chars.next(), chars.next()) {
69 (Some(c), None) => match c {
70 'N' => Self::NotPresent,
71 'G' => Self::Ok,
72 'T' => Self::Timeout,
73 'D' => Self::Disconnected,
74 'R' => Self::NotFound,
75 'M' => Self::Manipulated,
76 'X' => Self::Exchanged,
77 'I' => Self::Incompatible,
78 'O' => Self::OutOfRange,
79 'S' => Self::Substitute,
80 'E' => Self::OtherError,
81 'F' => Self::ReadError,
82 other => Self::Undefined(other),
83 },
84 _ => Self::Undefined('?'),
85 }
86 }
87
88 #[must_use]
90 pub const fn is_defined(self) -> bool {
91 !matches!(self, Self::Undefined(_))
92 }
93
94 #[must_use]
96 pub const fn letter(self) -> char {
97 match self {
98 Self::NotPresent => 'N',
99 Self::Ok => 'G',
100 Self::Timeout => 'T',
101 Self::Disconnected => 'D',
102 Self::NotFound => 'R',
103 Self::Manipulated => 'M',
104 Self::Exchanged => 'X',
105 Self::Incompatible => 'I',
106 Self::OutOfRange => 'O',
107 Self::Substitute => 'S',
108 Self::OtherError => 'E',
109 Self::ReadError => 'F',
110 Self::Undefined(c) => c,
111 }
112 }
113
114 #[must_use]
119 pub const fn is_ok(self) -> bool {
120 matches!(self, Self::Ok)
121 }
122
123 #[must_use]
125 pub const fn identifier(self) -> &'static str {
126 match self {
127 Self::NotPresent => "NOT_PRESENT",
128 Self::Ok => "OK",
129 Self::Timeout => "TIMEOUT",
130 Self::Disconnected => "DISCONNECTED",
131 Self::NotFound => "NOT_FOUND",
132 Self::Manipulated => "MANIPULATED",
133 Self::Exchanged => "EXCHANGED",
134 Self::Incompatible => "INCOMPATIBLE",
135 Self::OutOfRange => "OUT_OF_RANGE",
136 Self::Substitute => "SUBSTITUTE",
137 Self::OtherError => "OTHER_ERROR",
138 Self::ReadError => "READ_ERROR",
139 Self::Undefined(_) => "UNDEFINED",
140 }
141 }
142}
143
144#[cfg(feature = "serde")]
145impl serde::Serialize for MeterState {
146 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
149 s.serialize_str(&alloc::string::String::from(self.letter()))
150 }
151}
152
153impl fmt::Display for MeterState {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 write!(f, "{}", self.letter())
156 }
157}
158
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
162#[non_exhaustive]
163pub enum TransactionMarker {
164 Begin,
166 Charging,
168 Exception,
170 End,
172 EndedLocally,
174 EndedRemotely,
176 Aborted,
178 PowerFailure,
180 Suspended,
182 TariffChange,
184 Undefined(char),
186}
187
188impl TransactionMarker {
189 #[must_use]
191 pub fn parse(s: &str) -> Self {
192 let mut chars = s.chars();
193 match (chars.next(), chars.next()) {
194 (Some(c), None) => match c {
195 'B' => Self::Begin,
196 'C' => Self::Charging,
197 'X' => Self::Exception,
198 'E' => Self::End,
199 'L' => Self::EndedLocally,
200 'R' => Self::EndedRemotely,
201 'A' => Self::Aborted,
202 'P' => Self::PowerFailure,
203 'S' => Self::Suspended,
204 'T' => Self::TariffChange,
205 other => Self::Undefined(other),
206 },
207 _ => Self::Undefined('?'),
208 }
209 }
210
211 #[must_use]
213 pub const fn is_defined(self) -> bool {
214 !matches!(self, Self::Undefined(_))
215 }
216
217 #[must_use]
219 pub const fn letter(self) -> char {
220 match self {
221 Self::Begin => 'B',
222 Self::Charging => 'C',
223 Self::Exception => 'X',
224 Self::End => 'E',
225 Self::EndedLocally => 'L',
226 Self::EndedRemotely => 'R',
227 Self::Aborted => 'A',
228 Self::PowerFailure => 'P',
229 Self::Suspended => 'S',
230 Self::TariffChange => 'T',
231 Self::Undefined(c) => c,
232 }
233 }
234
235 #[must_use]
237 pub const fn is_begin(self) -> bool {
238 matches!(self, Self::Begin)
239 }
240
241 #[must_use]
244 pub const fn is_end(self) -> bool {
245 matches!(
246 self,
247 Self::End
248 | Self::EndedLocally
249 | Self::EndedRemotely
250 | Self::Aborted
251 | Self::PowerFailure
252 )
253 }
254
255 #[must_use]
258 pub const fn is_fault(self) -> bool {
259 matches!(self, Self::Aborted | Self::PowerFailure | Self::Exception)
260 }
261}
262
263#[cfg(feature = "serde")]
264impl serde::Serialize for TransactionMarker {
265 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
267 s.serialize_str(&alloc::string::String::from(self.letter()))
268 }
269}
270
271impl fmt::Display for TransactionMarker {
272 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
273 write!(f, "{}", self.letter())
274 }
275}
276
277#[derive(Debug, Clone, PartialEq, Eq, Default)]
286pub struct ErrorFlags<'a> {
287 raw: Cow<'a, str>,
288}
289
290impl<'a> ErrorFlags<'a> {
291 #[must_use]
293 pub const fn new(raw: Cow<'a, str>) -> Self {
294 Self { raw }
295 }
296
297 #[must_use]
299 pub const fn borrowed(raw: &'a str) -> Self {
300 Self {
301 raw: Cow::Borrowed(raw),
302 }
303 }
304
305 #[must_use]
307 pub fn as_str(&self) -> &str {
308 &self.raw
309 }
310
311 #[must_use]
313 pub fn energy_unusable(&self) -> bool {
314 self.raw.contains('E')
315 }
316
317 #[must_use]
319 pub fn time_unusable(&self) -> bool {
320 self.raw.contains('t')
321 }
322
323 #[must_use]
325 pub fn any(&self) -> bool {
326 !self.raw.is_empty()
327 }
328
329 pub fn undefined(&self) -> impl Iterator<Item = char> + '_ {
331 self.raw.chars().filter(|c| !matches!(c, 'E' | 't'))
332 }
333}
334
335impl fmt::Display for ErrorFlags<'_> {
336 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
337 f.write_str(&self.raw)
338 }
339}
340
341#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
343#[non_exhaustive]
344pub enum Unit<'a> {
345 KWh,
347 Wh,
349 MilliOhm,
351 MicroOhm,
353 Other(&'a str),
355}
356
357impl<'a> Unit<'a> {
358 #[must_use]
360 pub const fn parse(s: &'a str) -> Self {
361 if str_eq(s, "kWh") {
363 Self::KWh
364 } else if str_eq(s, "Wh") {
365 Self::Wh
366 } else if str_eq(s, "mOhm") {
367 Self::MilliOhm
368 } else if str_eq(s, "uOhm") {
369 Self::MicroOhm
370 } else {
371 Self::Other(s)
372 }
373 }
374
375 #[must_use]
378 pub fn parse_decoded(s: &RawStr<'a>) -> Self {
379 Self::from_parts(&s.decode(), s.as_raw())
380 }
381
382 #[must_use]
388 pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
389 decoded_or_raw(
390 decoded,
391 raw,
392 |d| match d {
393 "kWh" => Some(Self::KWh),
394 "Wh" => Some(Self::Wh),
395 "mOhm" => Some(Self::MilliOhm),
396 "uOhm" => Some(Self::MicroOhm),
397 _ => None,
398 },
399 Self::Other,
400 )
401 }
402
403 #[must_use]
405 pub const fn is_defined(&self) -> bool {
406 !matches!(self, Self::Other(_))
407 }
408
409 #[must_use]
411 pub const fn as_str(&self) -> &'a str {
412 match self {
413 Self::KWh => "kWh",
414 Self::Wh => "Wh",
415 Self::MilliOhm => "mOhm",
416 Self::MicroOhm => "uOhm",
417 Self::Other(s) => s,
418 }
419 }
420
421 #[must_use]
426 pub const fn is_energy(&self) -> bool {
427 matches!(self, Self::KWh | Self::Wh)
428 }
429
430 #[must_use]
432 pub const fn watt_hours_scale(&self) -> Option<u32> {
433 match self {
434 Self::KWh => Some(1000),
435 Self::Wh => Some(1),
436 _ => None,
437 }
438 }
439}
440
441const fn str_eq(a: &str, b: &str) -> bool {
442 let (a, b) = (a.as_bytes(), b.as_bytes());
443 if a.len() != b.len() {
444 return false;
445 }
446 let mut i = 0;
447 while i < a.len() {
448 if a[i] != b[i] {
449 return false;
450 }
451 i += 1;
452 }
453 true
454}
455
456impl fmt::Display for Unit<'_> {
457 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
458 f.write_str(self.as_str())
459 }
460}
461
462#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
464#[non_exhaustive]
465pub enum CurrentType<'a> {
466 Ac,
468 Dc,
470 Other(&'a str),
472}
473
474impl<'a> CurrentType<'a> {
475 #[must_use]
477 pub const fn parse(s: &'a str) -> Self {
478 if str_eq(s, "AC") {
479 Self::Ac
480 } else if str_eq(s, "DC") {
481 Self::Dc
482 } else {
483 Self::Other(s)
484 }
485 }
486
487 #[must_use]
490 pub fn parse_decoded(s: &RawStr<'a>) -> Self {
491 Self::from_parts(&s.decode(), s.as_raw())
492 }
493
494 #[must_use]
500 pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
501 decoded_or_raw(
502 decoded,
503 raw,
504 |d| match d {
505 "AC" => Some(Self::Ac),
506 "DC" => Some(Self::Dc),
507 _ => None,
508 },
509 Self::Other,
510 )
511 }
512
513 #[must_use]
515 pub const fn is_defined(&self) -> bool {
516 !matches!(self, Self::Other(_))
517 }
518
519 #[must_use]
521 pub const fn as_str(&self) -> &'a str {
522 match self {
523 Self::Ac => "AC",
524 Self::Dc => "DC",
525 Self::Other(s) => s,
526 }
527 }
528}
529
530impl fmt::Display for CurrentType<'_> {
531 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
532 f.write_str(self.as_str())
533 }
534}
535
536#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
538#[non_exhaustive]
539pub enum IdentificationLevel<'a> {
540 None,
542 Hearsay,
544 Trusted,
547 Verified,
549 Certified,
552 Secure,
554 Mismatch,
556 Invalid,
558 Outdated,
560 Unknown,
562 Other(&'a str),
564}
565
566impl<'a> IdentificationLevel<'a> {
567 #[must_use]
569 pub fn parse(s: &'a str) -> Self {
570 match s {
571 "NONE" => Self::None,
572 "HEARSAY" => Self::Hearsay,
573 "TRUSTED" => Self::Trusted,
574 "VERIFIED" => Self::Verified,
575 "CERTIFIED" => Self::Certified,
576 "SECURE" => Self::Secure,
577 "MISMATCH" => Self::Mismatch,
578 "INVALID" => Self::Invalid,
579 "OUTDATED" => Self::Outdated,
580 "UNKNOWN" => Self::Unknown,
581 other => Self::Other(other),
582 }
583 }
584
585 #[must_use]
588 pub fn parse_decoded(s: &RawStr<'a>) -> Self {
589 Self::from_parts(&s.decode(), s.as_raw())
590 }
591
592 #[must_use]
598 pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
599 decoded_or_raw(
600 decoded,
601 raw,
602 |d| {
603 Some(match d {
604 "NONE" => Self::None,
605 "HEARSAY" => Self::Hearsay,
606 "TRUSTED" => Self::Trusted,
607 "VERIFIED" => Self::Verified,
608 "CERTIFIED" => Self::Certified,
609 "SECURE" => Self::Secure,
610 "MISMATCH" => Self::Mismatch,
611 "INVALID" => Self::Invalid,
612 "OUTDATED" => Self::Outdated,
613 "UNKNOWN" => Self::Unknown,
614 _ => return None,
615 })
616 },
617 Self::Other,
618 )
619 }
620
621 #[must_use]
623 pub const fn is_defined(&self) -> bool {
624 !matches!(self, Self::Other(_))
625 }
626
627 #[must_use]
629 pub const fn as_str(&self) -> &'a str {
630 match self {
631 Self::None => "NONE",
632 Self::Hearsay => "HEARSAY",
633 Self::Trusted => "TRUSTED",
634 Self::Verified => "VERIFIED",
635 Self::Certified => "CERTIFIED",
636 Self::Secure => "SECURE",
637 Self::Mismatch => "MISMATCH",
638 Self::Invalid => "INVALID",
639 Self::Outdated => "OUTDATED",
640 Self::Unknown => "UNKNOWN",
641 Self::Other(s) => s,
642 }
643 }
644
645 #[must_use]
651 pub const fn is_error(&self) -> bool {
652 matches!(
653 self,
654 Self::Mismatch | Self::Invalid | Self::Outdated | Self::Unknown
655 )
656 }
657}
658
659impl fmt::Display for IdentificationLevel<'_> {
660 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
661 f.write_str(self.as_str())
662 }
663}
664
665#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
667#[non_exhaustive]
668pub enum IdentificationType<'a> {
669 None,
671 Denied,
673 Undefined,
675 Iso14443,
677 Iso15693,
679 Emaid,
681 Evccid,
683 Evcoid,
685 Iso7812,
687 CardTxnNr,
689 Central,
691 Central1,
693 Central2,
695 Local,
697 Local1,
699 Local2,
701 PhoneNumber,
703 KeyCode,
705 Other(&'a str),
707}
708
709impl<'a> IdentificationType<'a> {
710 #[must_use]
712 pub fn parse(s: &'a str) -> Self {
713 match s {
714 "NONE" => Self::None,
715 "DENIED" => Self::Denied,
716 "UNDEFINED" => Self::Undefined,
717 "ISO14443" => Self::Iso14443,
718 "ISO15693" => Self::Iso15693,
719 "EMAID" => Self::Emaid,
720 "EVCCID" => Self::Evccid,
721 "EVCOID" => Self::Evcoid,
722 "ISO7812" => Self::Iso7812,
723 "CARD_TXN_NR" => Self::CardTxnNr,
724 "CENTRAL" => Self::Central,
725 "CENTRAL_1" => Self::Central1,
726 "CENTRAL_2" => Self::Central2,
727 "LOCAL" => Self::Local,
728 "LOCAL_1" => Self::Local1,
729 "LOCAL_2" => Self::Local2,
730 "PHONE_NUMBER" => Self::PhoneNumber,
731 "KEY_CODE" => Self::KeyCode,
732 other => Self::Other(other),
733 }
734 }
735
736 #[must_use]
739 pub fn parse_decoded(s: &RawStr<'a>) -> Self {
740 Self::from_parts(&s.decode(), s.as_raw())
741 }
742
743 #[must_use]
749 pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
750 decoded_or_raw(
751 decoded,
752 raw,
753 |d| {
754 Some(match d {
755 "NONE" => Self::None,
756 "DENIED" => Self::Denied,
757 "UNDEFINED" => Self::Undefined,
758 "ISO14443" => Self::Iso14443,
759 "ISO15693" => Self::Iso15693,
760 "EMAID" => Self::Emaid,
761 "EVCCID" => Self::Evccid,
762 "EVCOID" => Self::Evcoid,
763 "ISO7812" => Self::Iso7812,
764 "CARD_TXN_NR" => Self::CardTxnNr,
765 "CENTRAL" => Self::Central,
766 "CENTRAL_1" => Self::Central1,
767 "CENTRAL_2" => Self::Central2,
768 "LOCAL" => Self::Local,
769 "LOCAL_1" => Self::Local1,
770 "LOCAL_2" => Self::Local2,
771 "PHONE_NUMBER" => Self::PhoneNumber,
772 "KEY_CODE" => Self::KeyCode,
773 _ => return None,
774 })
775 },
776 Self::Other,
777 )
778 }
779
780 #[must_use]
782 pub const fn is_defined(&self) -> bool {
783 !matches!(self, Self::Other(_))
784 }
785
786 #[must_use]
794 pub fn data_is_well_formed(&self, data: &str) -> Option<bool> {
795 let hex =
796 |n: [usize; 2]| n.contains(&data.len()) && data.bytes().all(|c| c.is_ascii_hexdigit());
797 Some(match self {
798 Self::Iso14443 => hex([8, 14]),
800 Self::Iso15693 => hex([16, 16]),
802 Self::Emaid => matches!(data.chars().count(), 14 | 15),
804 Self::Evccid => data.chars().count() <= 6,
806 Self::PhoneNumber => data.starts_with('+'),
808 _ => return None,
809 })
810 }
811
812 #[must_use]
814 pub const fn as_str(&self) -> &'a str {
815 match self {
816 Self::None => "NONE",
817 Self::Denied => "DENIED",
818 Self::Undefined => "UNDEFINED",
819 Self::Iso14443 => "ISO14443",
820 Self::Iso15693 => "ISO15693",
821 Self::Emaid => "EMAID",
822 Self::Evccid => "EVCCID",
823 Self::Evcoid => "EVCOID",
824 Self::Iso7812 => "ISO7812",
825 Self::CardTxnNr => "CARD_TXN_NR",
826 Self::Central => "CENTRAL",
827 Self::Central1 => "CENTRAL_1",
828 Self::Central2 => "CENTRAL_2",
829 Self::Local => "LOCAL",
830 Self::Local1 => "LOCAL_1",
831 Self::Local2 => "LOCAL_2",
832 Self::PhoneNumber => "PHONE_NUMBER",
833 Self::KeyCode => "KEY_CODE",
834 Self::Other(s) => s,
835 }
836 }
837}
838
839impl fmt::Display for IdentificationType<'_> {
840 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
841 f.write_str(self.as_str())
842 }
843}
844
845#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
850#[non_exhaustive]
851pub enum IdentificationFlag<'a> {
852 Rfid(&'a str),
854 Ocpp(&'a str),
856 Iso15118(&'a str),
858 Plmn(&'a str),
860 Other(&'a str),
862}
863
864impl<'a> IdentificationFlag<'a> {
865 pub const DEFINED: [&'static str; 17] = [
867 "RFID_NONE",
868 "RFID_PLAIN",
869 "RFID_RELATED",
870 "RFID_PSK",
871 "OCPP_NONE",
872 "OCPP_RS",
873 "OCPP_AUTH",
874 "OCPP_RS_TLS",
875 "OCPP_AUTH_TLS",
876 "OCPP_CACHE",
877 "OCPP_WHITELIST",
878 "OCPP_CERTIFIED",
879 "ISO15118_NONE",
880 "ISO15118_PNC",
881 "PLMN_NONE",
882 "PLMN_RING",
883 "PLMN_SMS",
884 ];
885
886 #[must_use]
888 pub fn parse(s: &'a str) -> Self {
889 if !Self::DEFINED.contains(&s) {
890 return Self::Other(s);
891 }
892 if s.starts_with("RFID_") {
893 Self::Rfid(s)
894 } else if s.starts_with("OCPP_") {
895 Self::Ocpp(s)
896 } else if s.starts_with("ISO15118_") {
897 Self::Iso15118(s)
898 } else {
899 Self::Plmn(s)
900 }
901 }
902
903 #[must_use]
910 pub fn parse_decoded(s: &RawStr<'a>) -> Self {
911 let raw = s.as_raw();
912 let decoded = s.decode();
913 let group = match IdentificationFlag::parse(decoded.as_ref()) {
914 IdentificationFlag::Rfid(_) => 0u8,
915 IdentificationFlag::Ocpp(_) => 1,
916 IdentificationFlag::Iso15118(_) => 2,
917 IdentificationFlag::Plmn(_) => 3,
918 IdentificationFlag::Other(_) => 4,
919 };
920 match group {
921 0 => Self::Rfid(raw),
922 1 => Self::Ocpp(raw),
923 2 => Self::Iso15118(raw),
924 3 => Self::Plmn(raw),
925 _ => Self::Other(raw),
926 }
927 }
928
929 #[must_use]
935 pub const fn group(&self) -> Option<u8> {
936 Some(match self {
937 Self::Rfid(_) => 0,
938 Self::Ocpp(_) => 1,
939 Self::Iso15118(_) => 2,
940 Self::Plmn(_) => 3,
941 Self::Other(_) => return None,
942 })
943 }
944
945 #[must_use]
947 pub const fn is_defined(&self) -> bool {
948 !matches!(self, Self::Other(_))
949 }
950
951 #[must_use]
953 pub const fn as_str(&self) -> &'a str {
954 match self {
955 Self::Rfid(s) | Self::Ocpp(s) | Self::Iso15118(s) | Self::Plmn(s) | Self::Other(s) => s,
956 }
957 }
958}
959
960impl fmt::Display for IdentificationFlag<'_> {
961 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
962 f.write_str(self.as_str())
963 }
964}
965
966#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
968#[non_exhaustive]
969pub enum ChargePointIdType<'a> {
970 EvseId,
972 ChargeBoxAndConnector,
974 Other(&'a str),
976}
977
978impl<'a> ChargePointIdType<'a> {
979 #[must_use]
981 pub fn parse(s: &'a str) -> Self {
982 match s {
983 "EVSEID" => Self::EvseId,
984 "CBIDC" => Self::ChargeBoxAndConnector,
985 other => Self::Other(other),
986 }
987 }
988
989 #[must_use]
992 pub fn parse_decoded(s: &RawStr<'a>) -> Self {
993 Self::from_parts(&s.decode(), s.as_raw())
994 }
995
996 #[must_use]
1002 pub fn from_parts(decoded: &str, raw: &'a str) -> Self {
1003 decoded_or_raw(
1004 decoded,
1005 raw,
1006 |d| match d {
1007 "EVSEID" => Some(Self::EvseId),
1008 "CBIDC" => Some(Self::ChargeBoxAndConnector),
1009 _ => None,
1010 },
1011 Self::Other,
1012 )
1013 }
1014
1015 #[must_use]
1017 pub const fn is_defined(&self) -> bool {
1018 !matches!(self, Self::Other(_))
1019 }
1020
1021 #[must_use]
1027 pub fn id_is_well_formed(&self, id: &str) -> Option<bool> {
1028 match self {
1029 Self::ChargeBoxAndConnector => {
1032 Some(id.split_once(' ').is_some_and(|(box_id, connector)| {
1033 !box_id.is_empty() && !connector.is_empty() && !connector.contains(' ')
1034 }))
1035 }
1036 _ => None,
1037 }
1038 }
1039
1040 #[must_use]
1042 pub const fn as_str(&self) -> &'a str {
1043 match self {
1044 Self::EvseId => "EVSEID",
1045 Self::ChargeBoxAndConnector => "CBIDC",
1046 Self::Other(s) => s,
1047 }
1048 }
1049}
1050
1051impl fmt::Display for ChargePointIdType<'_> {
1052 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1053 f.write_str(self.as_str())
1054 }
1055}
1056
1057#[cfg(test)]
1058mod tests {
1059 use super::*;
1060 use alloc::string::ToString;
1061
1062 #[test]
1063 fn every_meter_state_letter_round_trips() {
1064 for c in "NGTDRMXIOSEF".chars() {
1065 let st = MeterState::parse(&c.to_string());
1066 assert_eq!(st.letter(), c);
1067 assert_eq!(st.is_ok(), c == 'G');
1068 }
1069 }
1070
1071 #[test]
1072 fn an_undefined_meter_state_is_never_ok() {
1073 let st = MeterState::parse("Q");
1074 assert_eq!(st, MeterState::Undefined('Q'));
1075 assert!(!st.is_ok());
1076 assert_eq!(st.letter(), 'Q');
1077 }
1078
1079 #[test]
1080 fn all_five_spellings_of_an_ending_are_endings() {
1081 for c in "ELRAP".chars() {
1082 assert!(TransactionMarker::parse(&c.to_string()).is_end(), "{c}");
1083 }
1084 for c in "BCXST".chars() {
1085 assert!(!TransactionMarker::parse(&c.to_string()).is_end(), "{c}");
1086 }
1087 assert!(TransactionMarker::parse("B").is_begin());
1088 assert!(!TransactionMarker::parse("Q").is_begin());
1089 }
1090
1091 #[test]
1092 fn error_flags_keep_what_they_cannot_interpret() {
1093 let f = ErrorFlags::borrowed("Et");
1094 assert!(f.energy_unusable() && f.time_unusable() && f.any());
1095 let f = ErrorFlags::borrowed("");
1096 assert!(!f.any());
1097 let f = ErrorFlags::borrowed("Q");
1098 assert!(f.any(), "an undefined flag is still a fault");
1099 assert!(!f.energy_unusable());
1100 assert_eq!(f.undefined().collect::<alloc::vec::Vec<_>>(), ['Q']);
1101 }
1102
1103 #[test]
1104 fn an_unknown_unit_is_not_energy() {
1105 assert!(Unit::parse("kWh").is_energy());
1106 assert!(Unit::parse("Wh").is_energy());
1107 assert!(!Unit::parse("mOhm").is_energy());
1108 assert!(!Unit::parse("MWh").is_energy());
1109 assert_eq!(Unit::parse("MWh"), Unit::Other("MWh"));
1110 }
1111
1112 #[test]
1113 fn the_four_error_levels_are_the_ones_the_reference_refuses() {
1114 for s in ["MISMATCH", "INVALID", "OUTDATED", "UNKNOWN"] {
1115 assert!(IdentificationLevel::parse(s).is_error(), "{s}");
1116 }
1117 for s in [
1118 "NONE",
1119 "HEARSAY",
1120 "TRUSTED",
1121 "VERIFIED",
1122 "CERTIFIED",
1123 "SECURE",
1124 ] {
1125 assert!(!IdentificationLevel::parse(s).is_error(), "{s}");
1126 }
1127 }
1128
1129 #[test]
1130 fn identification_flags_are_grouped_by_their_table() {
1131 assert!(matches!(
1132 IdentificationFlag::parse("RFID_PLAIN"),
1133 IdentificationFlag::Rfid(_)
1134 ));
1135 assert!(matches!(
1136 IdentificationFlag::parse("OCPP_RS_TLS"),
1137 IdentificationFlag::Ocpp(_)
1138 ));
1139 assert!(matches!(
1140 IdentificationFlag::parse("ISO15118_PNC"),
1141 IdentificationFlag::Iso15118(_)
1142 ));
1143 assert!(matches!(
1144 IdentificationFlag::parse("PLMN_SMS"),
1145 IdentificationFlag::Plmn(_)
1146 ));
1147 assert!(matches!(
1148 IdentificationFlag::parse("VENDOR_X"),
1149 IdentificationFlag::Other(_)
1150 ));
1151 }
1152
1153 #[test]
1154 fn an_escaped_table_value_still_matches_its_table() {
1155 let raw = crate::json::parse_value(
1159 r#"{"RU":"kWh","IT":"ISO14443","IL":"VERIFIED","CT":"EVSEID","IF":"RFID_PLAIN"}"#,
1160 0,
1161 &crate::Limits::DEFAULT,
1162 &mut alloc::vec::Vec::new(),
1163 )
1164 .unwrap();
1165 let o = raw.as_object().unwrap();
1166 let f = |k: &str| o.get(k).unwrap().as_str().unwrap();
1167
1168 assert_eq!(Unit::parse_decoded(f("RU")), Unit::KWh);
1169 assert!(Unit::parse_decoded(f("RU")).is_energy());
1170 assert_eq!(
1171 IdentificationType::parse_decoded(f("IT")),
1172 IdentificationType::Iso14443
1173 );
1174 assert_eq!(
1175 IdentificationLevel::parse_decoded(f("IL")),
1176 IdentificationLevel::Verified
1177 );
1178 assert_eq!(
1179 ChargePointIdType::parse_decoded(f("CT")),
1180 ChargePointIdType::EvseId
1181 );
1182 assert!(matches!(
1183 IdentificationFlag::parse_decoded(f("IF")),
1184 IdentificationFlag::Rfid(_)
1185 ));
1186
1187 let raw = crate::json::parse_value(
1189 r#"{"RU":"MWh"}"#,
1190 0,
1191 &crate::Limits::DEFAULT,
1192 &mut alloc::vec::Vec::new(),
1193 )
1194 .unwrap();
1195 let u = Unit::parse_decoded(
1196 raw.as_object()
1197 .unwrap()
1198 .get("RU")
1199 .unwrap()
1200 .as_str()
1201 .unwrap(),
1202 );
1203 assert_eq!(u, Unit::Other("MWh"));
1204 }
1205
1206 #[test]
1207 fn all_eighteen_identification_types_round_trip() {
1208 let all = [
1209 "NONE",
1210 "DENIED",
1211 "UNDEFINED",
1212 "ISO14443",
1213 "ISO15693",
1214 "EMAID",
1215 "EVCCID",
1216 "EVCOID",
1217 "ISO7812",
1218 "CARD_TXN_NR",
1219 "CENTRAL",
1220 "CENTRAL_1",
1221 "CENTRAL_2",
1222 "LOCAL",
1223 "LOCAL_1",
1224 "LOCAL_2",
1225 "PHONE_NUMBER",
1226 "KEY_CODE",
1227 ];
1228 assert_eq!(all.len(), 18, "[OCMF Tab. 17] has 18 rows");
1229 for s in all {
1230 assert_eq!(IdentificationType::parse(s).as_str(), s);
1231 assert!(!matches!(
1232 IdentificationType::parse(s),
1233 IdentificationType::Other(_)
1234 ));
1235 }
1236 }
1237}