From 368aee800658e9c1a9a30871c45fc6034cceda48 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Tue, 16 Sep 2025 20:48:43 +0800 Subject: refactor: use std TryFrom instead of custom IntoTask --- src/client/connector.rs | 8 ++++---- src/client/executor.rs | 5 ++--- src/tasks.rs | 34 +++++----------------------------- 3 files changed, 11 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/client/connector.rs b/src/client/connector.rs index a595c8e..6c002c9 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -3,12 +3,12 @@ pub mod httpconnector; use std::net::IpAddr; use crate::{client::Client, executor::Executor}; -use remote_control_rs::tasks::{IntoTask, NETWORK_T}; +use remote_control_rs::tasks::NETWORK_T; -pub type Result = std::result::Result; +pub type Result = std::result::Result; pub trait Connection { - type Ok: std::fmt::Display + IntoTask + AsRef = String; + type Ok: std::fmt::Display + AsRef = String; type Err: std::fmt::Debug; // should use channel to notify update @@ -62,7 +62,7 @@ pub trait Connection { None => return, }; // FIXME: handle the errors... peak prod code - self.ntfy(Executor::new(task, it.map(str::to_string)).unwrap().run().unwrap().clone().unwrap()).unwrap(); + self.ntfy(Executor::new(task.try_into().unwrap(), it.map(str::to_string)).unwrap().run().unwrap().clone().unwrap()).unwrap(); } Result::Err(e) => { #[cfg(debug_assertions)] diff --git a/src/client/executor.rs b/src/client/executor.rs index 32a0fdc..379c832 100644 --- a/src/client/executor.rs +++ b/src/client/executor.rs @@ -1,4 +1,4 @@ -use remote_control_rs::tasks::{IntoTask, IntoTaskErr, Task}; +use remote_control_rs::tasks::{IntoTaskErr, Task}; pub struct Executor { task: &'static Task, @@ -7,8 +7,7 @@ pub struct Executor { } impl Executor { - pub fn new(task: impl IntoTask, args: impl IntoIterator) -> Result { - let task = task.into_task()?; + pub fn new(task: &'static Task, args: impl IntoIterator) -> Result { Ok(Self { task: task, args: args.into_iter().collect(), output: None }) } 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 { 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())) } } -- cgit v1.2.3