use std::fmt;
use strum::EnumMessage;
#[derive(
Clone, Copy, Debug, PartialEq, Eq, Hash, strum_macros::EnumMessage, strum_macros::EnumString,
)]
pub enum Originator {
#[strum(serialize = "", detailed_message = "Unknown Originator")]
Unknown,
#[strum(serialize = "PEP", detailed_message = "Primary Entry Point System")]
PrimaryEntryPoint,
#[strum(serialize = "CIV", detailed_message = "Civil authorities")]
CivilAuthority,
#[strum(serialize = "WXR", detailed_message = "National Weather Service")]
NationalWeatherService,
#[strum(message = "WXR", detailed_message = "Environment Canada")]
EnvironmentCanada,
#[strum(
serialize = "EAS",
detailed_message = "Broadcast station or cable system"
)]
BroadcastStation,
}
impl Originator {
pub fn from_org_and_call<S1, S2>(org: S1, call: S2) -> Self
where
S1: AsRef<str>,
S2: AsRef<str>,
{
let decode = str::parse(org.as_ref()).unwrap_or_default();
if decode == Self::NationalWeatherService && call.as_ref().starts_with("EC/") {
Self::EnvironmentCanada
} else {
decode
}
}
pub fn as_display_str(&self) -> &'static str {
self.get_detailed_message().expect("missing definition")
}
pub fn as_code_str(&self) -> &'static str {
self.get_message()
.unwrap_or_else(|| self.get_serializations()[0])
}
}
impl std::default::Default for Originator {
fn default() -> Self {
Self::Unknown
}
}
impl AsRef<str> for Originator {
fn as_ref(&self) -> &'static str {
self.as_code_str()
}
}
impl fmt::Display for Originator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
self.as_code_str().fmt(f)
} else {
self.as_display_str().fmt(f)
}
}
}