use crate::error::AppError;
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
pub struct Priority(i32);
impl Priority {
pub const S: Priority = Priority(1);
pub const A: Priority = Priority(2);
pub const B: Priority = Priority(3);
pub const C: Priority = Priority(4);
pub const NONE: Priority = Priority(5);
pub fn new(inner: i32) -> Self {
if !(1..=4).contains(&inner) {
Priority(5) } else {
Priority(inner)
}
}
pub fn to_char(self) -> &'static str {
match self.0 {
1 => "S",
2 => "A",
3 => "B",
4 => "C",
_ => "",
}
}
pub fn from_char(c: &str) -> Result<Priority, AppError> {
match c.to_uppercase().as_str() {
"S" => Ok(Priority::S),
"A" => Ok(Priority::A),
"B" => Ok(Priority::B),
"C" => Ok(Priority::C),
"" => Ok(Priority::NONE),
_ => Err(AppError::Validation(format!("Invalid priority: {}", c))),
}
}
pub fn next(self) -> Self {
match self.0 {
1 => Priority(5),
2 => Priority(1),
3 => Priority(2),
4 => Priority(3),
5 => Priority(4),
_ => Priority(5),
}
}
}
impl Default for Priority {
fn default() -> Self {
Priority::NONE
}
}
impl From<Priority> for i32 {
fn from(p: Priority) -> Self {
p.0
}
}