use std::num::NonZeroU32;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TimeUnit {
Second,
Minute,
}
impl std::fmt::Display for TimeUnit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
TimeUnit::Second => "s",
TimeUnit::Minute => "m",
}
)
}
}
impl std::str::FromStr for TimeUnit {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"s" => Ok(TimeUnit::Second),
"m" => Ok(TimeUnit::Minute),
_ => Err(format!("Invalid time unit: {}", s)),
}
}
}
impl TimeUnit {
pub const fn multiplier_in_seconds(&self) -> NonZeroU32 {
match self {
TimeUnit::Second => NonZeroU32::new(1).expect("Is always non-zero"),
TimeUnit::Minute => NonZeroU32::new(60).expect("Is always non-zero"),
}
}
}