use core::fmt;
use core::hash::{Hash, Hasher};
use core::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::validate::{Validate, Validator, ViolationCode};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum IdStandard {
Iso,
Din,
Either,
}
impl IdStandard {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Iso => "ISO",
Self::Din => "DIN",
Self::Either => "ISO/DIN",
}
}
}
impl fmt::Display for IdStandard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{value:?} is not a valid {type_name}: {reason}")]
pub struct IdError {
type_name: &'static str,
value: String,
reason: &'static str,
}
impl IdError {
fn new(type_name: &'static str, value: &str, reason: &'static str) -> Self {
Self { type_name, value: value.to_owned(), reason }
}
#[must_use]
pub const fn type_name(&self) -> &'static str {
self.type_name
}
#[must_use]
pub fn value(&self) -> &str {
&self.value
}
}
fn significant_bytes(s: &str) -> impl Iterator<Item = u8> + '_ {
s.strip_prefix('+').unwrap_or(s).bytes().filter(|c| !is_separator(*c)).map(|c| c.to_ascii_uppercase())
}
fn semantic_eq(a: &str, b: &str) -> bool {
significant_bytes(a).eq(significant_bytes(b))
}
fn semantic_cmp(a: &str, b: &str) -> core::cmp::Ordering {
significant_bytes(a).cmp(significant_bytes(b))
}
fn semantic_hash<H: Hasher>(s: &str, state: &mut H) {
for c in significant_bytes(s) {
state.write_u8(c);
}
state.write_u8(0xff);
}
fn case_insensitive_cmp(a: &str, b: &str) -> core::cmp::Ordering {
a.bytes().map(|c| c.to_ascii_uppercase()).cmp(b.bytes().map(|c| c.to_ascii_uppercase()))
}
const fn is_separator(c: u8) -> bool {
matches!(c, b'*' | b'-')
}
macro_rules! oicp_id {
(
$(#[$meta:meta])*
$name:ident, $type_name:literal
) => {
$(#[$meta])*
#[derive(Clone, Debug)]
pub struct $name {
/// Exactly the text that arrived on the wire.
raw: String,
standard: IdStandard,
}
impl $name {
pub fn new(value: impl Into<String>) -> Result<Self, IdError> {
let raw = value.into();
let standard = Self::classify(&raw)?;
Ok(Self { raw, standard })
}
#[must_use]
pub fn new_unchecked(value: impl Into<String>) -> Self {
let raw = value.into();
let standard = Self::classify(&raw).unwrap_or(if raw.starts_with('+') || raw.starts_with(|c: char| c.is_ascii_digit()) {
IdStandard::Din
} else {
IdStandard::Iso
});
Self { raw, standard }
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.raw
}
#[must_use]
pub const fn standard(&self) -> IdStandard {
self.standard
}
#[must_use]
pub fn canonical(&self) -> String {
significant_bytes(&self.raw).map(char::from).collect()
}
#[must_use]
pub fn is_well_formed(&self) -> bool {
Self::classify(&self.raw).is_ok()
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.raw)
}
}
impl PartialEq for $name {
fn eq(&self, other: &Self) -> bool {
semantic_eq(&self.raw, &other.raw)
}
}
impl Eq for $name {}
impl Hash for $name {
fn hash<H: Hasher>(&self, state: &mut H) {
semantic_hash(&self.raw, state);
}
}
impl PartialOrd for $name {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for $name {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
semantic_cmp(&self.raw, &other.raw)
}
}
impl FromStr for $name {
type Err = IdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}
impl Validate for $name {
fn validate_in(&self, v: &mut Validator) {
if let Err(e) = Self::classify(&self.raw) {
v.report(ViolationCode::PatternMismatch, e.to_string());
}
}
}
impl Serialize for $name {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.raw)
}
}
impl<'de> Deserialize<'de> for $name {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Ok(Self::new_unchecked(String::deserialize(d)?))
}
}
#[cfg(feature = "schema")]
impl schemars::JsonSchema for $name {
fn schema_name() -> std::borrow::Cow<'static, str> { $type_name.into() }
fn json_schema(_g: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({ "type": "string" })
}
}
};
}
oicp_id! {
EvseId, "EvseID"
}
impl EvseId {
fn classify(s: &str) -> Result<IdStandard, IdError> {
if let Ok(()) = Self::check_iso(s) {
return Ok(IdStandard::Iso);
}
Self::check_din(s).map(|()| IdStandard::Din)
}
fn check_iso(s: &str) -> Result<(), IdError> {
let b = s.as_bytes();
let mut i = 0;
let err = |reason| Err(IdError::new("EvseID", s, reason));
if b.len() < 2 || !b[0].is_ascii_alphabetic() || !b[1].is_ascii_alphabetic() {
return err("an ISO EvseID starts with a two-letter country code");
}
i += 2;
if b.get(i) == Some(&b'*') {
i += 1;
}
if b.len() < i + 3 || !b[i..i + 3].iter().all(u8::is_ascii_alphanumeric) {
return err("the operator part of an ISO EvseID is three alphanumeric characters");
}
i += 3;
if b.get(i) == Some(&b'*') {
i += 1;
}
if b.get(i).is_none_or(|c| !c.eq_ignore_ascii_case(&b'E')) {
return err("an ISO EvseID has an 'E' after the operator part");
}
i += 1;
let rest = &b[i..];
if rest.is_empty() || rest.len() > 30 {
return err("the instance part of an ISO EvseID is 1 to 30 characters");
}
if !rest.iter().all(|c| c.is_ascii_alphanumeric() || *c == b'*') {
return err("the instance part of an ISO EvseID is alphanumeric with optional '*' separators");
}
Ok(())
}
fn check_din(s: &str) -> Result<(), IdError> {
let err = |reason| Err(IdError::new("EvseID", s, reason));
let body = s.strip_prefix('+').unwrap_or(s);
let mut parts = body.splitn(3, '*');
let (Some(country), Some(operator), Some(instance)) = (parts.next(), parts.next(), parts.next())
else {
return err("a DIN EvseID is country*operator*instance, separated by mandatory '*'");
};
if country.is_empty() || country.len() > 3 || !country.bytes().all(|c| c.is_ascii_digit()) {
return err("the country code of a DIN EvseID is 1 to 3 digits (ITU-T E.164)");
}
if operator.len() != 3 || !operator.bytes().all(|c| c.is_ascii_digit()) {
return err("the operator part of a DIN EvseID is exactly three digits");
}
if instance.is_empty()
|| instance.len() > 32
|| !instance.bytes().all(|c| c.is_ascii_digit() || c == b'*')
{
return err("the instance part of a DIN EvseID is 1 to 32 digits with optional '*' separators");
}
Ok(())
}
#[must_use]
pub fn country(&self) -> &str {
match self.standard() {
IdStandard::Din => {
let body = self.as_str().strip_prefix('+').unwrap_or(self.as_str());
body.split('*').next().unwrap_or("")
}
IdStandard::Iso | IdStandard::Either => &self.as_str()[..2],
}
}
#[must_use]
pub fn operator_id(&self) -> OperatorId {
let raw = match self.standard() {
IdStandard::Iso | IdStandard::Either => {
let b = self.as_str().as_bytes();
let mut end = 2;
if b.get(end) == Some(&b'*') {
end += 1;
}
end += 3;
&self.as_str()[..end]
}
IdStandard::Din => {
let s = self.as_str();
let plus = usize::from(s.starts_with('+'));
let body = &s[plus..];
match body.match_indices('*').nth(1) {
Some((idx, _)) => &s[..plus + idx],
None => s,
}
}
};
OperatorId::new_unchecked(raw)
}
}
oicp_id! {
EvcoId, "EvcoID"
}
impl EvcoId {
fn classify(s: &str) -> Result<IdStandard, IdError> {
if Self::check_iso(s).is_ok() {
return Ok(IdStandard::Iso);
}
Self::check_din(s).map(|()| IdStandard::Din)
}
fn check_iso(s: &str) -> Result<(), IdError> {
let b = s.as_bytes();
let err = |reason| Err(IdError::new("EvcoID", s, reason));
let mut i = 0;
if b.len() < 2 || !b[0].is_ascii_alphabetic() || !b[1].is_ascii_alphabetic() {
return err("an ISO EvcoID starts with a two-letter country code");
}
i += 2;
if b.get(i) == Some(&b'-') {
i += 1;
}
if b.len() < i + 3 || !b[i..i + 3].iter().all(u8::is_ascii_alphanumeric) {
return err("the provider part of an ISO EvcoID is three alphanumeric characters");
}
i += 3;
if b.get(i) == Some(&b'-') {
i += 1;
}
if b.get(i).is_none_or(|c| !c.eq_ignore_ascii_case(&b'C')) {
return err("an ISO EvcoID prefixes its instance part with 'C'");
}
i += 1;
if b.len() < i + 8 || !b[i..i + 8].iter().all(u8::is_ascii_alphanumeric) {
return err("the instance part of an ISO EvcoID is exactly eight alphanumeric characters");
}
i += 8;
if b.get(i) == Some(&b'-') {
i += 1;
}
if b.len() != i + 1 || !b[i].is_ascii_alphanumeric() {
return err("an ISO EvcoID ends with a single check character");
}
Ok(())
}
fn check_din(s: &str) -> Result<(), IdError> {
let b = s.as_bytes();
let err = |reason| Err(IdError::new("EvcoID", s, reason));
let mut i = 0;
if b.len() < 2 || !b[0].is_ascii_alphabetic() || !b[1].is_ascii_alphabetic() {
return err("a DIN EvcoID starts with a two-letter country code");
}
i += 2;
if b.get(i).is_some_and(|c| is_separator(*c)) {
i += 1;
}
if b.len() < i + 3 || !b[i..i + 3].iter().all(u8::is_ascii_alphanumeric) {
return err("the provider part of a DIN EvcoID is three alphanumeric characters");
}
i += 3;
if b.get(i).is_some_and(|c| is_separator(*c)) {
i += 1;
}
if b.len() < i + 6 || !b[i..i + 6].iter().all(u8::is_ascii_alphanumeric) {
return err("the instance part of a DIN EvcoID is exactly six alphanumeric characters");
}
i += 6;
if b.get(i).is_some_and(|c| is_separator(*c)) {
i += 1;
}
if b.len() != i + 1 || !(b[i].is_ascii_digit() || b[i].eq_ignore_ascii_case(&b'X')) {
return err("a DIN EvcoID ends with a check digit or 'X'");
}
Ok(())
}
#[must_use]
pub fn provider_id(&self) -> ProviderId {
let b = self.as_str().as_bytes();
let mut end = 2;
if b.get(end).is_some_and(|c| is_separator(*c)) {
end += 1;
}
end = (end + 3).min(self.as_str().len());
ProviderId::new_unchecked(&self.as_str()[..end])
}
}
oicp_id! {
OperatorId, "OperatorID"
}
impl OperatorId {
fn classify(s: &str) -> Result<IdStandard, IdError> {
let b = s.as_bytes();
let err = |reason| Err(IdError::new("OperatorID", s, reason));
if b.len() >= 2 && b[0].is_ascii_alphabetic() && b[1].is_ascii_alphabetic() {
let rest = if b.get(2) == Some(&b'*') { &b[3..] } else { &b[2..] };
if rest.len() == 3 && rest.iter().all(u8::is_ascii_alphanumeric) {
return Ok(IdStandard::Iso);
}
return err("an ISO OperatorID is a two-letter country code and three alphanumeric characters");
}
let body = s.strip_prefix('+').unwrap_or(s);
let mut parts = body.splitn(2, '*');
let (Some(country), Some(operator)) = (parts.next(), parts.next()) else {
return err("a DIN OperatorID is country*operator with a mandatory '*'");
};
if country.is_empty() || country.len() > 3 || !country.bytes().all(|c| c.is_ascii_digit()) {
return err("the country code of a DIN OperatorID is 1 to 3 digits");
}
if operator.len() != 3 || !operator.bytes().all(|c| c.is_ascii_digit()) {
return err("the operator part of a DIN OperatorID is exactly three digits");
}
Ok(IdStandard::Din)
}
#[must_use]
pub fn country(&self) -> &str {
match self.standard() {
IdStandard::Din => {
let body = self.as_str().strip_prefix('+').unwrap_or(self.as_str());
body.split('*').next().unwrap_or("")
}
IdStandard::Iso | IdStandard::Either => &self.as_str()[..2],
}
}
}
oicp_id! {
ProviderId, "ProviderID"
}
impl ProviderId {
fn classify(s: &str) -> Result<IdStandard, IdError> {
let b = s.as_bytes();
let err = |reason| Err(IdError::new("ProviderID", s, reason));
if b.len() < 2 || !b[0].is_ascii_alphabetic() || !b[1].is_ascii_alphabetic() {
return err("a ProviderID starts with a two-letter country code");
}
let (rest, separated) =
if b.get(2).is_some_and(|c| is_separator(*c)) { (&b[3..], true) } else { (&b[2..], false) };
if rest.len() != 3 || !rest.iter().all(u8::is_ascii_alphanumeric) {
return err("a ProviderID is a two-letter country code and three alphanumeric characters");
}
let _ = separated;
Ok(IdStandard::Either)
}
#[must_use]
pub fn country(&self) -> &str {
&self.as_str()[..2]
}
}
oicp_id! {
ChargingPoolId, "ChargingPoolID"
}
impl ChargingPoolId {
fn classify(s: &str) -> Result<IdStandard, IdError> {
let b = s.as_bytes();
let err = |reason| Err(IdError::new("ChargingPoolID", s, reason));
let mut i = 0;
if b.len() < 2 || !b[0].is_ascii_alphabetic() || !b[1].is_ascii_alphabetic() {
return err("a ChargingPoolID starts with a two-letter country code");
}
i += 2;
if b.get(i) == Some(&b'*') {
i += 1;
}
if b.len() < i + 3 || !b[i..i + 3].iter().all(u8::is_ascii_alphanumeric) {
return err("the operator part of a ChargingPoolID is three alphanumeric characters");
}
i += 3;
if b.get(i) == Some(&b'*') {
i += 1;
}
if b.get(i).is_none_or(|c| !c.eq_ignore_ascii_case(&b'P')) {
return err("a ChargingPoolID has a 'P' after the operator part, marking it as a pool");
}
i += 1;
let rest = &b[i..];
if rest.is_empty() || rest.len() > 30 {
return err("the pool part of a ChargingPoolID is 1 to 30 characters");
}
if !rest.iter().all(|c| c.is_ascii_alphanumeric() || *c == b'*') {
return err("the pool part of a ChargingPoolID is alphanumeric with optional '*' separators");
}
Ok(IdStandard::Iso)
}
}
#[derive(Clone, Debug)]
pub struct SessionId(String);
impl SessionId {
pub fn new(value: impl Into<String>) -> Result<Self, IdError> {
let raw = value.into();
Self::check(&raw)?;
Ok(Self(raw))
}
#[must_use]
pub fn new_unchecked(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn is_well_formed(&self) -> bool {
Self::check(&self.0).is_ok()
}
fn check(s: &str) -> Result<(), IdError> {
let err = |reason| Err(IdError::new("SessionID", s, reason));
let groups: Vec<&str> = s.split('-').collect();
if groups.len() != 5 {
return err("a SessionID has five '-'-separated groups");
}
let expected = [8usize, 4, 4, 4, 12];
for (group, want) in groups.iter().zip(expected) {
if group.len() != want || !group.bytes().all(|c| c.is_ascii_alphanumeric()) {
return err("a SessionID is 8-4-4-4-12 alphanumeric characters");
}
}
Ok(())
}
}
impl fmt::Display for SessionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl PartialEq for SessionId {
fn eq(&self, other: &Self) -> bool {
self.0.eq_ignore_ascii_case(&other.0)
}
}
impl Eq for SessionId {}
impl Hash for SessionId {
fn hash<H: Hasher>(&self, state: &mut H) {
for c in self.0.bytes() {
state.write_u8(c.to_ascii_uppercase());
}
}
}
impl PartialOrd for SessionId {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for SessionId {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
case_insensitive_cmp(&self.0, &other.0)
}
}
impl FromStr for SessionId {
type Err = IdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}
impl Validate for SessionId {
fn validate_in(&self, v: &mut Validator) {
if let Err(e) = Self::check(&self.0) {
v.report(ViolationCode::PatternMismatch, e.to_string());
}
}
}
impl Serialize for SessionId {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.0)
}
}
impl<'de> Deserialize<'de> for SessionId {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Ok(Self::new_unchecked(String::deserialize(d)?))
}
}
#[cfg(feature = "schema")]
impl schemars::JsonSchema for SessionId {
fn schema_name() -> std::borrow::Cow<'static, str> {
"SessionID".into()
}
fn json_schema(_g: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({ "type": "string" })
}
}
#[derive(Clone, Debug)]
pub struct Uid(String);
impl Uid {
pub fn new(value: impl Into<String>) -> Result<Self, IdError> {
let raw = value.into();
Self::check(&raw)?;
Ok(Self(raw))
}
#[must_use]
pub fn new_unchecked(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn canonical(&self) -> String {
self.0.to_ascii_uppercase()
}
#[must_use]
pub fn is_well_formed(&self) -> bool {
Self::check(&self.0).is_ok()
}
fn check(s: &str) -> Result<(), IdError> {
if !matches!(s.len(), 8 | 14 | 20) {
return Err(IdError::new("UID", s, "an RFID UID is 8, 14 or 20 characters"));
}
if !s.bytes().all(|c| c.is_ascii_hexdigit()) {
return Err(IdError::new("UID", s, "an RFID UID is hexadecimal"));
}
if s.bytes().any(|c| c.is_ascii_lowercase()) {
return Err(IdError::new(
"UID",
s,
"the specification writes an RFID UID in uppercase hexadecimal",
));
}
Ok(())
}
}
impl fmt::Display for Uid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl PartialEq for Uid {
fn eq(&self, other: &Self) -> bool {
self.0.eq_ignore_ascii_case(&other.0)
}
}
impl Eq for Uid {}
impl Hash for Uid {
fn hash<H: Hasher>(&self, state: &mut H) {
for c in self.0.bytes() {
state.write_u8(c.to_ascii_uppercase());
}
}
}
impl PartialOrd for Uid {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Uid {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
case_insensitive_cmp(&self.0, &other.0)
}
}
impl FromStr for Uid {
type Err = IdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}
impl Validate for Uid {
fn validate_in(&self, v: &mut Validator) {
if let Err(e) = Self::check(&self.0) {
v.report(ViolationCode::PatternMismatch, e.to_string());
}
}
}
impl Serialize for Uid {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.0)
}
}
impl<'de> Deserialize<'de> for Uid {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Ok(Self::new_unchecked(String::deserialize(d)?))
}
}
#[cfg(feature = "schema")]
impl schemars::JsonSchema for Uid {
fn schema_name() -> std::borrow::Cow<'static, str> {
"UID".into()
}
fn json_schema(_g: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({ "type": "string" })
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ProviderIdOrAll {
One(ProviderId),
All,
}
impl ProviderIdOrAll {
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::One(id) => id.as_str(),
Self::All => "*",
}
}
#[must_use]
pub const fn is_all(&self) -> bool {
matches!(self, Self::All)
}
}
impl fmt::Display for ProviderIdOrAll {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<ProviderId> for ProviderIdOrAll {
fn from(id: ProviderId) -> Self {
Self::One(id)
}
}
impl FromStr for ProviderIdOrAll {
type Err = IdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "*" { Ok(Self::All) } else { ProviderId::new(s).map(Self::One) }
}
}
impl Validate for ProviderIdOrAll {
fn validate_in(&self, v: &mut Validator) {
if let Self::One(id) = self {
id.validate_in(v);
}
}
}
impl Serialize for ProviderIdOrAll {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for ProviderIdOrAll {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = String::deserialize(d)?;
Ok(if s == "*" { Self::All } else { Self::One(ProviderId::new_unchecked(s)) })
}
}
#[cfg(feature = "schema")]
impl schemars::JsonSchema for ProviderIdOrAll {
fn schema_name() -> std::borrow::Cow<'static, str> {
"ProviderIDAsterisk".into()
}
fn json_schema(_g: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({ "type": "string" })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn evse_id_accepts_every_example_the_spec_gives() {
for (text, standard) in [
("DE*AB7*E840*6487", IdStandard::Iso),
("DEAB7E8406487", IdStandard::Iso),
("DE*XYZ*ETEST1", IdStandard::Iso),
("+49*810*000*438", IdStandard::Din),
("49*810*000*438", IdStandard::Din),
] {
let id: EvseId = text.parse().unwrap_or_else(|e| panic!("{text}: {e}"));
assert_eq!(id.standard(), standard, "{text}");
assert_eq!(id.to_string(), text, "{text} was rewritten");
}
}
#[test]
fn evse_ids_that_differ_only_in_separators_and_case_are_equal() {
let a: EvseId = "DE*AB7*E840*6487".parse().unwrap();
let b: EvseId = "DEAB7E8406487".parse().unwrap();
let c: EvseId = "de*ab7*e840*6487".parse().unwrap();
assert_eq!(a, b);
assert_eq!(a, c);
assert_eq!(a.to_string(), "DE*AB7*E840*6487");
assert_eq!(b.to_string(), "DEAB7E8406487");
}
#[test]
fn an_iso_id_never_equals_a_din_one() {
let iso: EvseId = "DE*AB7*E840*6487".parse().unwrap();
let din: EvseId = "+49*810*000*438".parse().unwrap();
assert_ne!(iso, din);
}
#[test]
fn the_optional_din_plus_does_not_make_a_second_charging_spot() {
let with: EvseId = "+49*810*000*438".parse().unwrap();
let without: EvseId = "49*810*000*438".parse().unwrap();
assert_eq!(with, without);
assert_eq!(with.cmp(&without), core::cmp::Ordering::Equal);
assert_eq!(with.canonical(), without.canonical());
assert_eq!(with.to_string(), "+49*810*000*438", "and each keeps its own text");
assert_eq!(with.operator_id(), without.operator_id());
}
#[test]
fn ordering_agrees_with_equality_for_every_identifier() {
use std::hash::{BuildHasher as _, RandomState};
let hasher = RandomState::new();
let spellings = ["DE*AB7*E840*6487", "DEAB7E8406487", "de-ab7-e840-6487", "DE*XYZ*ETEST1"];
for a in spellings {
for b in spellings {
let (x, y) = (EvseId::new_unchecked(a), EvseId::new_unchecked(b));
assert_eq!(
(x.cmp(&y) == core::cmp::Ordering::Equal),
x == y,
"Ord and Eq disagree on {a:?} vs {b:?}"
);
if x == y {
assert_eq!(hasher.hash_one(&x), hasher.hash_one(&y), "{a:?} and {b:?} hash apart");
} else {
assert_ne!(hasher.hash_one(&x), hasher.hash_one(&y), "{a:?} and {b:?} hash alike");
}
}
}
}
#[test]
fn the_operator_is_derived_from_the_evse_id_the_way_hubject_does_it() {
let iso: EvseId = "DE*AB7*E840*6487".parse().unwrap();
assert_eq!(iso.operator_id().to_string(), "DE*AB7");
assert_eq!(iso.country(), "DE");
let packed: EvseId = "DEAB7E8406487".parse().unwrap();
assert_eq!(packed.operator_id().to_string(), "DEAB7");
assert_eq!(iso.operator_id(), packed.operator_id());
let din: EvseId = "+49*810*000*438".parse().unwrap();
assert_eq!(din.operator_id().to_string(), "+49*810");
assert_eq!(din.country(), "49");
}
#[test]
fn evco_id_accepts_every_example_the_spec_gives() {
for (text, standard) in [
("DE-8EO-CAet5e4XY-3", IdStandard::Iso),
("DE8EOCAet5e43X1", IdStandard::Iso),
("DE*8EO*Aet5e4*3", IdStandard::Din),
("DE-8EO-Aet5e4-3", IdStandard::Din),
("DE8EOAet5e43", IdStandard::Din),
("DE-DCB-C12345678-X", IdStandard::Iso),
] {
let id: EvcoId = text.parse().unwrap_or_else(|e| panic!("{text}: {e}"));
assert_eq!(id.standard(), standard, "{text}");
assert_eq!(id.to_string(), text);
}
}
#[test]
fn the_provider_is_derived_from_the_evco_id() {
let iso: EvcoId = "DE-8EO-CAet5e4XY-3".parse().unwrap();
assert_eq!(iso.provider_id().to_string(), "DE-8EO");
let din: EvcoId = "DE*8EO*Aet5e4*3".parse().unwrap();
assert_eq!(din.provider_id().to_string(), "DE*8EO");
assert_eq!(iso.provider_id(), din.provider_id());
}
#[test]
fn operator_and_provider_ids_accept_the_spec_examples() {
for text in ["DE*A36", "DEA36", "+49*536", "49*536"] {
let id: OperatorId = text.parse().unwrap_or_else(|e| panic!("{text}: {e}"));
assert_eq!(id.to_string(), text);
}
for text in ["DE8EO", "DE-8EO", "DE*8EO"] {
let id: ProviderId = text.parse().unwrap_or_else(|e| panic!("{text}: {e}"));
assert_eq!(id.to_string(), text);
assert_eq!(id.country(), "DE");
}
}
#[test]
fn malformed_ids_are_rejected_by_new_but_survive_deserialisation() {
assert!("not-an-evse-id".parse::<EvseId>().is_err());
assert!("DE*AB7*840*6487".parse::<EvseId>().is_err());
let id: EvseId = serde_json::from_str(r#""garbage""#).unwrap();
assert!(!id.is_well_formed());
assert_eq!(serde_json::to_string(&id).unwrap(), r#""garbage""#);
assert_eq!(id.validate().unwrap_err().as_slice()[0].code, ViolationCode::PatternMismatch);
}
#[test]
fn every_prefix_of_a_valid_identifier_is_rejected_without_panicking() {
let valid = [
"DE*AB7*E840*6487",
"DEAB7E8406487",
"+49*810*000*438",
"DE-8EO-CAet5e4XY-3",
"DE*8EO*Aet5e4*3",
"DE*A36",
"+49*536",
"DE-8EO",
"IT*123*P456*AB789",
"b2688855-7f00-0002-6d8e-48d883f6abb6",
"7568290FFF765F",
];
for text in valid {
let extended = format!("{text}9");
for cut in 0..=extended.len() {
let candidate = &extended[..cut];
assert_eq!(
EvseId::new_unchecked(candidate).is_well_formed(),
EvseId::new(candidate).is_ok(),
"EvseID disagreed with itself on {candidate:?}"
);
assert_eq!(
EvcoId::new_unchecked(candidate).is_well_formed(),
EvcoId::new(candidate).is_ok(),
"EvcoID disagreed with itself on {candidate:?}"
);
assert_eq!(
OperatorId::new_unchecked(candidate).is_well_formed(),
OperatorId::new(candidate).is_ok(),
"OperatorID disagreed with itself on {candidate:?}"
);
assert_eq!(
ProviderId::new_unchecked(candidate).is_well_formed(),
ProviderId::new(candidate).is_ok(),
"ProviderID disagreed with itself on {candidate:?}"
);
assert_eq!(
ChargingPoolId::new_unchecked(candidate).is_well_formed(),
ChargingPoolId::new(candidate).is_ok(),
"ChargingPoolID disagreed with itself on {candidate:?}"
);
assert_eq!(
SessionId::new_unchecked(candidate).is_well_formed(),
SessionId::new(candidate).is_ok(),
"SessionID disagreed with itself on {candidate:?}"
);
assert_eq!(
Uid::new_unchecked(candidate).is_well_formed(),
Uid::new(candidate).is_ok(),
"UID disagreed with itself on {candidate:?}"
);
}
}
assert!("DE".parse::<EvseId>().is_err(), "no operator part at all");
assert!("DE*A".parse::<EvseId>().is_err(), "one operator character");
assert!("DE*AB7*E".parse::<EvseId>().is_err(), "no instance after the E marker");
assert!("DE-8EO-C".parse::<EvcoId>().is_err(), "no instance after the C marker");
assert!("IT*123*P".parse::<ChargingPoolId>().is_err(), "no pool part after the P marker");
assert!("DE".parse::<OperatorId>().is_err(), "a country code is not an OperatorID");
assert!("DE".parse::<ProviderId>().is_err(), "nor a ProviderID");
}
#[test]
fn both_characters_of_a_country_code_must_be_letters() {
for bad in [
"1EAB7E840", "E1AB7E840", "D3*AB7*E840*6487", ] {
assert!(bad.parse::<EvseId>().is_err(), "{bad} should be rejected");
}
for bad in ["1E-8EO-CAet5e4XY-3", "D3-8EO-CAet5e4XY-3", "1E*8EO*Aet5e4*3", "D3*8EO*Aet5e4*3"] {
assert!(bad.parse::<EvcoId>().is_err(), "{bad} should be rejected");
}
for bad in ["1E8EO", "D38EO", "1E-8EO"] {
assert!(bad.parse::<ProviderId>().is_err(), "{bad} should be rejected");
}
for bad in ["1T*123*P456", "I3*123*P456"] {
assert!(bad.parse::<ChargingPoolId>().is_err(), "{bad} should be rejected");
}
for bad in ["1E*A36", "D3*A36"] {
assert!(bad.parse::<OperatorId>().is_err(), "{bad} should be rejected");
}
assert!("DE*AB7*E840*6487".parse::<EvseId>().is_ok());
assert!("DE-8EO-CAet5e4XY-3".parse::<EvcoId>().is_ok());
assert!("DE-8EO".parse::<ProviderId>().is_ok());
assert!("IT*123*P456".parse::<ChargingPoolId>().is_ok());
assert!("DE*A36".parse::<OperatorId>().is_ok());
}
#[test]
fn the_din_grammars_reject_each_part_on_its_own() {
for bad in [
"49810000438", "49*810", "*810*000", "4999*810*000", "4a*810*000", "49*81*000", "49*8100*000", "49*8a0*000", "49*810*", "49*810*00a", ] {
assert!(bad.parse::<EvseId>().is_err(), "{bad} should be rejected");
assert!(!EvseId::new_unchecked(bad).is_well_formed(), "{bad}");
}
assert!(format!("49*810*{}", "0".repeat(32)).parse::<EvseId>().is_ok(), "thirty-two digits");
assert!(format!("49*810*{}", "0".repeat(33)).parse::<EvseId>().is_err(), "thirty-three is too many");
for bad in ["49810", "49", "*536", "4999*536", "4a*536", "49*53", "49*5366", "49*5a6"] {
assert!(bad.parse::<OperatorId>().is_err(), "{bad} should be rejected");
}
assert!("+999*536".parse::<OperatorId>().is_ok(), "a three-digit country code");
}
#[test]
fn the_components_a_caller_reads_are_the_ones_in_the_identifier() {
assert_eq!("DE*A36".parse::<OperatorId>().unwrap().country(), "DE");
assert_eq!("DEA36".parse::<OperatorId>().unwrap().country(), "DE");
assert_eq!("+49*536".parse::<OperatorId>().unwrap().country(), "49");
assert_eq!("999*536".parse::<OperatorId>().unwrap().country(), "999");
assert_eq!("DE-8EO".parse::<ProviderId>().unwrap().country(), "DE");
let session: SessionId = "b2688855-7f00-0002-6d8e-48d883f6abb6".parse().unwrap();
assert_eq!(session.as_str(), "b2688855-7f00-0002-6d8e-48d883f6abb6");
assert!(session.is_well_formed());
let broken = SessionId::new_unchecked("nope");
assert_eq!(broken.as_str(), "nope");
assert!(!broken.is_well_formed());
}
#[test]
fn the_pricing_asterisk_survives_every_road_in_and_out() {
let all: ProviderIdOrAll = "*".parse().unwrap();
let one: ProviderIdOrAll = "DE-8EO".parse().unwrap();
assert_eq!(all.to_string(), "*");
assert_eq!(one.to_string(), "DE-8EO");
assert_eq!(all.as_str(), "*");
assert!(all.is_all() && !one.is_all());
assert!(all.validate().is_ok(), "the asterisk is not an identifier to validate");
assert!(one.validate().is_ok());
assert!(ProviderIdOrAll::One(ProviderId::new_unchecked("nope")).validate().is_err());
assert_eq!(serde_json::from_str::<ProviderIdOrAll>(r#""*""#).unwrap(), ProviderIdOrAll::All);
assert_eq!(serde_json::from_str::<ProviderIdOrAll>(r#""DE-8EO""#).unwrap(), one);
assert_eq!(serde_json::to_string(&all).unwrap(), r#""*""#);
}
#[test]
fn a_rejection_says_which_type_rejected_what() {
let err = "not-an-evse-id".parse::<EvseId>().unwrap_err();
assert_eq!(err.type_name(), "EvseID");
assert_eq!(err.value(), "not-an-evse-id");
assert!(err.to_string().contains("EvseID"), "{err}");
assert!(err.to_string().contains("not-an-evse-id"), "{err}");
assert_eq!("DE*A36".parse::<OperatorId>().unwrap().standard().as_str(), "ISO");
assert_eq!("+49*536".parse::<OperatorId>().unwrap().standard().as_str(), "DIN");
assert_eq!("DE8EO".parse::<ProviderId>().unwrap().standard().as_str(), "ISO/DIN");
assert_eq!(IdStandard::Iso.to_string(), "ISO");
assert_eq!(IdStandard::Din.to_string(), "DIN");
assert_eq!(IdStandard::Either.to_string(), "ISO/DIN");
}
#[cfg(feature = "schema")]
#[test]
fn the_published_schema_describes_every_identifier_as_a_string() {
let mut generator = schemars::SchemaGenerator::default();
for (name, schema) in [
("EvseID", <EvseId as schemars::JsonSchema>::json_schema(&mut generator)),
("EvcoID", <EvcoId as schemars::JsonSchema>::json_schema(&mut generator)),
("OperatorID", <OperatorId as schemars::JsonSchema>::json_schema(&mut generator)),
("ProviderID", <ProviderId as schemars::JsonSchema>::json_schema(&mut generator)),
("ChargingPoolID", <ChargingPoolId as schemars::JsonSchema>::json_schema(&mut generator)),
("SessionID", <SessionId as schemars::JsonSchema>::json_schema(&mut generator)),
("UID", <Uid as schemars::JsonSchema>::json_schema(&mut generator)),
("ProviderIDAsterisk", <ProviderIdOrAll as schemars::JsonSchema>::json_schema(&mut generator)),
] {
let json = serde_json::to_value(&schema).unwrap();
assert_eq!(json["type"], "string", "{name} published an empty schema: {json}");
}
assert_eq!(<EvseId as schemars::JsonSchema>::schema_name(), "EvseID");
assert_eq!(<Uid as schemars::JsonSchema>::schema_name(), "UID");
assert_eq!(<ProviderIdOrAll as schemars::JsonSchema>::schema_name(), "ProviderIDAsterisk");
}
#[test]
fn session_ids_compare_hash_and_order_as_one_session() {
use std::hash::{BuildHasher as _, RandomState};
let hasher = RandomState::new();
let lower: SessionId = "b2688855-7f00-0002-6d8e-48d883f6abb6".parse().unwrap();
let upper: SessionId = "B2688855-7F00-0002-6D8E-48D883F6ABB6".parse().unwrap();
let other: SessionId = "c2688855-7f00-0002-6d8e-48d883f6abb6".parse().unwrap();
assert_eq!(lower, upper, "one session, written two ways");
assert_eq!(hasher.hash_one(&lower), hasher.hash_one(&upper));
assert_eq!(lower.cmp(&upper), core::cmp::Ordering::Equal);
assert_eq!(lower.to_string(), "b2688855-7f00-0002-6d8e-48d883f6abb6", "and each keeps its text");
assert_ne!(lower, other, "two sessions are two sessions");
assert_ne!(hasher.hash_one(&lower), hasher.hash_one(&other));
assert!(lower < other);
assert!(other > upper);
assert_eq!(lower.partial_cmp(&other), Some(core::cmp::Ordering::Less));
}
#[test]
fn rfid_uids_compare_hash_and_order_as_one_card() {
use std::hash::{BuildHasher as _, RandomState};
let hasher = RandomState::new();
let upper: Uid = "7568290FFF765F".parse().unwrap();
let lower = Uid::new_unchecked("7568290fff765f");
let other: Uid = "AABBCCDDEEFF11".parse().unwrap();
assert_eq!(upper, lower);
assert_eq!(hasher.hash_one(&upper), hasher.hash_one(&lower));
assert_eq!(upper.cmp(&lower), core::cmp::Ordering::Equal);
assert_ne!(upper, other);
assert_ne!(hasher.hash_one(&upper), hasher.hash_one(&other));
assert!(other > upper, "A sorts after 7");
assert_eq!(upper.partial_cmp(&other), Some(core::cmp::Ordering::Less));
}
#[test]
fn every_grammar_boundary_is_checked_at_the_boundary() {
assert!("DE*AB7*E1".parse::<EvseId>().is_ok(), "one instance character is enough");
assert!(format!("DE*AB7*E{}", "1".repeat(30)).parse::<EvseId>().is_ok(), "thirty is the cap");
assert!(format!("DE*AB7*E{}", "1".repeat(31)).parse::<EvseId>().is_err(), "thirty-one is not");
assert!("DE*AB7*E".parse::<EvseId>().is_err(), "an empty instance part is not an EvseID");
assert!("DE*AB*E1".parse::<EvseId>().is_err(), "two operator characters is not three");
assert!("DE*AB7".parse::<EvseId>().is_err(), "and the E marker is not optional");
assert!("DE-8EO-CAet5e4XY-3".parse::<EvcoId>().is_ok());
assert!("DE-8EO-CAet5e4X-3".parse::<EvcoId>().is_err(), "seven is not eight");
assert!("DE-8EO-CAet5e4XYZ-3".parse::<EvcoId>().is_err(), "nor is nine");
assert!("DE-8EO-CAet5e4XY-".parse::<EvcoId>().is_err(), "the check character is not optional");
assert!("DE-8EO-CAet5e4XY-34".parse::<EvcoId>().is_err(), "and there is exactly one of it");
assert!("DE-8E-CAet5e4XY-3".parse::<EvcoId>().is_err(), "two provider characters is not three");
assert!("DE*8EO*Aet5e4*3".parse::<EvcoId>().is_ok());
assert!("DE*8EO*Aet5e*3".parse::<EvcoId>().is_err(), "five is not six");
assert!("DE*8EO*Aet5e44*3".parse::<EvcoId>().is_err(), "nor is seven");
assert!("DE*8EO*Aet5e4*".parse::<EvcoId>().is_err(), "the check digit is not optional");
assert!("DE*8EO*Aet5e4*33".parse::<EvcoId>().is_err(), "and there is exactly one of it");
assert!("DE*8EO*Aet5e4*X".parse::<EvcoId>().is_ok(), "X is a check digit");
assert!("DE*8EO*Aet5e4*Y".parse::<EvcoId>().is_err(), "Y is not");
assert!("IT*123*P4".parse::<ChargingPoolId>().is_ok());
assert!(format!("IT*123*P{}", "4".repeat(30)).parse::<ChargingPoolId>().is_ok());
assert!(format!("IT*123*P{}", "4".repeat(31)).parse::<ChargingPoolId>().is_err());
assert!("IT*123*P".parse::<ChargingPoolId>().is_err(), "an empty pool part is not a pool");
assert!("IT*12*P456".parse::<ChargingPoolId>().is_err(), "two operator characters is not three");
assert!("IT*123*456".parse::<ChargingPoolId>().is_err(), "and the P marker is not optional");
assert!("b2688855-7f00-0002-6d8e-48d883f6abb6".parse::<SessionId>().is_ok());
for bad in [
"b268885-7f00-0002-6d8e-48d883f6abb6", "b26888556-7f00-0002-6d8e-48d883f6abb6", "b2688855-7f0-0002-6d8e-48d883f6abb6", "b2688855-7f00-0002-6d8e-48d883f6abb", "b2688855-7f00-0002-6d8e-48d883f6abb6a", "b2688855-7f00-0002-6d8e-48d883f6ab_6", ] {
assert!(bad.parse::<SessionId>().is_err(), "{bad} should be rejected");
}
}
#[test]
fn session_ids_follow_the_guid_shape() {
let id: SessionId = "b2688855-7f00-0002-6d8e-48d883f6abb6".parse().unwrap();
assert_eq!(id.to_string(), "b2688855-7f00-0002-6d8e-48d883f6abb6");
assert!("b2688855-7f00-0002-6d8e".parse::<SessionId>().is_err());
assert!("zzzzzzzz-aaaa-bbbb-cccc-dddddddddddd".parse::<SessionId>().is_ok());
}
#[test]
fn rfid_uids_match_case_insensitively_but_only_uppercase_is_emitted() {
let upper: Uid = "7568290FFF765F".parse().unwrap();
let lower = Uid::new_unchecked("7568290fff765f");
assert_eq!(upper, lower, "the same card");
assert_eq!(lower.to_string(), "7568290fff765f", "and its text survives");
assert_eq!(lower.canonical(), "7568290FFF765F", "with an uppercase key for lookups");
assert!("7568290fff765f".parse::<Uid>().is_err());
assert!(lower.validate().is_err());
assert!(!lower.is_well_formed());
assert!(upper.validate().is_ok());
assert!("123".parse::<Uid>().is_err());
}
#[test]
fn is_well_formed_agrees_with_validate_for_every_identifier() {
for text in ["7568290FFF765F", "7568290fff765f", "123", "", "DE*ABC*E1", "not an id"] {
let uid = Uid::new_unchecked(text);
assert_eq!(uid.is_well_formed(), uid.validate().is_ok(), "Uid disagreed on {text:?}");
let evse = EvseId::new_unchecked(text);
assert_eq!(evse.is_well_formed(), evse.validate().is_ok(), "EvseId disagreed on {text:?}");
let session = SessionId::new_unchecked(text);
assert_eq!(
session.is_well_formed(),
session.validate().is_ok(),
"SessionId disagreed on {text:?}"
);
}
}
#[test]
fn charging_pool_ids_use_the_p_marker() {
let id: ChargingPoolId = "IT*123*P456*AB789".parse().unwrap();
assert_eq!(id.to_string(), "IT*123*P456*AB789");
assert!("IT*123*E456*AB789".parse::<ChargingPoolId>().is_err());
}
#[test]
fn the_pricing_asterisk_is_its_own_variant() {
let all: ProviderIdOrAll = "*".parse().unwrap();
assert!(all.is_all());
assert_eq!(serde_json::to_string(&all).unwrap(), r#""*""#);
let one: ProviderIdOrAll = "DE-8EO".parse().unwrap();
assert!(!one.is_all());
}
}