#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AgencyCode {
Bdew,
Gs1,
Entso,
Dvgw,
}
impl AgencyCode {
pub const DEFAULT: Self = Self::Bdew;
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Bdew => "293",
Self::Gs1 => "9",
Self::Entso => "305",
Self::Dvgw => "332",
}
}
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
match s {
"293" => Some(Self::Bdew),
"9" => Some(Self::Gs1),
"305" => Some(Self::Entso),
"332" => Some(Self::Dvgw),
_ => None,
}
}
}
impl Default for AgencyCode {
fn default() -> Self {
Self::DEFAULT
}
}
impl std::fmt::Display for AgencyCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bdew_is_default() {
assert_eq!(AgencyCode::default(), AgencyCode::Bdew);
assert_eq!(AgencyCode::DEFAULT.as_str(), "293");
}
#[test]
fn round_trip_from_str() {
for (code, variant) in [
("293", AgencyCode::Bdew),
("9", AgencyCode::Gs1),
("305", AgencyCode::Entso),
("332", AgencyCode::Dvgw),
] {
assert_eq!(AgencyCode::parse(code), Some(variant));
assert_eq!(variant.as_str(), code);
}
assert_eq!(AgencyCode::parse("999"), None);
}
}