Skip to main content

xtask_todo_lib/
priority.rs

1//! Priority for a todo item.
2
3use std::fmt;
4use std::str::FromStr;
5
6/// Priority for a todo item.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum Priority {
9    Low,
10    Medium,
11    High,
12}
13
14impl Priority {
15    #[must_use]
16    pub const fn as_u8(self) -> u8 {
17        match self {
18            Self::Low => 1,
19            Self::Medium => 2,
20            Self::High => 3,
21        }
22    }
23}
24
25impl FromStr for Priority {
26    type Err = ();
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        match s.trim().to_lowercase().as_str() {
30            "low" | "1" => Ok(Self::Low),
31            "medium" | "2" => Ok(Self::Medium),
32            "high" | "3" => Ok(Self::High),
33            _ => Err(()),
34        }
35    }
36}
37
38impl fmt::Display for Priority {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::Low => f.write_str("low"),
42            Self::Medium => f.write_str("medium"),
43            Self::High => f.write_str("high"),
44        }
45    }
46}