|
1 | | -use std::{path::Path, str::FromStr}; |
| 1 | +use std::{fmt, path::Path, str::FromStr}; |
2 | 2 |
|
3 | 3 | use colored::Colorize; |
4 | 4 | use eyre::Result; |
@@ -51,12 +51,26 @@ pub struct DailyChallengeQuestion { |
51 | 51 | pub titleSlug: String, |
52 | 52 | } |
53 | 53 |
|
| 54 | +#[derive(Debug, Clone, Copy)] |
54 | 55 | pub enum Difficulty { |
55 | 56 | Easy, |
56 | 57 | Medium, |
57 | 58 | Hard, |
58 | 59 | } |
59 | 60 |
|
| 61 | +impl FromStr for Difficulty { |
| 62 | + type Err = eyre::ErrReport; |
| 63 | + |
| 64 | + fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { |
| 65 | + match s.to_lowercase().as_ref() { |
| 66 | + "easy" => Ok(Difficulty::Easy), |
| 67 | + "medium" => Ok(Difficulty::Medium), |
| 68 | + "hard" => Ok(Difficulty::Hard), |
| 69 | + _ => Err(eyre::eyre!("Unknown difficulty")), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
60 | 74 | #[derive(Debug, Deserialize)] |
61 | 75 | #[allow(non_snake_case)] |
62 | 76 | pub struct DailyChallenge { |
@@ -99,37 +113,26 @@ impl BoilerPlateCode { |
99 | 113 | } |
100 | 114 | } |
101 | 115 |
|
102 | | -impl std::fmt::Display for DailyChallenge { |
103 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 116 | +impl fmt::Display for DailyChallenge { |
| 117 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
104 | 118 | write!( |
105 | 119 | f, |
106 | 120 | "Title : {}\nDifficulty : {}\nDate : {}\nStatus : {}\nAC Rate : {:.2}%", |
107 | 121 | self.question.title.bright_cyan(), |
108 | | - Difficulty::from_str(&self.question.difficulty), |
| 122 | + Difficulty::from_str(&self.question.difficulty).map_err(|_| fmt::Error)?, |
109 | 123 | self.date, |
110 | 124 | self.userStatus, |
111 | 125 | self.question.acRate |
112 | 126 | ) |
113 | 127 | } |
114 | 128 | } |
115 | 129 |
|
116 | | -impl std::fmt::Display for Difficulty { |
117 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 130 | +impl fmt::Display for Difficulty { |
| 131 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
118 | 132 | match self { |
119 | 133 | Difficulty::Easy => write!(f, "{}", "Easy".bright_green()), |
120 | 134 | Difficulty::Medium => write!(f, "{}", "Medium".bright_yellow()), |
121 | 135 | Difficulty::Hard => write!(f, "{}", "Hard".bright_red()), |
122 | 136 | } |
123 | 137 | } |
124 | 138 | } |
125 | | - |
126 | | -impl Difficulty { |
127 | | - pub fn from_str(difficulty: &str) -> Difficulty { |
128 | | - match difficulty { |
129 | | - "Easy" => Difficulty::Easy, |
130 | | - "Medium" => Difficulty::Medium, |
131 | | - "Hard" => Difficulty::Hard, |
132 | | - _ => panic!("Invalid difficulty"), |
133 | | - } |
134 | | - } |
135 | | -} |
|
0 commit comments