diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-09-16 20:48:43 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-09-17 21:28:52 +0800 |
| commit | 368aee800658e9c1a9a30871c45fc6034cceda48 (patch) | |
| tree | 546952a7fdc4ac86b6de9278267f41ecf4bdeb19 /src/tasks.rs | |
| parent | 73b908cb44cf52a768647c0f5cd450ab48cf831f (diff) | |
refactor: use std TryFrom instead of custom IntoTask
Diffstat (limited to 'src/tasks.rs')
| -rw-r--r-- | src/tasks.rs | 34 |
1 files changed, 5 insertions, 29 deletions
diff --git a/src/tasks.rs b/src/tasks.rs index bfa24e8..ad41a35 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -115,39 +115,15 @@ pub enum IntoTaskErr { NotFound(String), } -pub trait IntoTask { - fn into_task<'a>(self) -> Result<&'a Task, IntoTaskErr>; -} - -impl IntoTask for String { - fn into_task<'a>(self) -> Result<&'a Task, IntoTaskErr> { - for task in TASKS { - if task.name == self { - return Ok(task); - } - } - Err(IntoTaskErr::NotFound(self)) - } -} - -impl IntoTask for &str { - fn into_task<'a>(self) -> Result<&'a Task, IntoTaskErr> { - for task in TASKS { - if task.name == self { - return Ok(task); - } - } - Err(IntoTaskErr::NotFound(self.to_string())) - } -} +impl TryFrom<&str> for &Task { + type Error = IntoTaskErr; -impl IntoTask for &Task { - fn into_task<'a>(self) -> Result<&'a Task, IntoTaskErr> { + fn try_from(value: &str) -> Result<Self, Self::Error> { for task in TASKS { - if task == self { + if task.name == value { return Ok(task); } } - Err(IntoTaskErr::NotFound(self.name.to_string())) + Err(IntoTaskErr::NotFound(value.to_string())) } } |
