use serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Effort {
Low,
#[default]
Medium,
High,
}
impl Effort {
pub const ALL: [Self; 3] = [Self::Low, Self::Medium, Self::High];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Low => "low",
Self::Medium => "medium",
Self::High => "high",
}
}
}
impl std::fmt::Display for Effort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("`{0}` is not an effort level: low, medium or high")]
pub struct UnknownEffort(pub String);
impl std::str::FromStr for Effort {
type Err = UnknownEffort;
fn from_str(label: &str) -> Result<Self, Self::Err> {
Self::ALL
.into_iter()
.find(|effort| effort.as_str() == label)
.ok_or_else(|| UnknownEffort(label.to_owned()))
}
}