summaryrefslogtreecommitdiff
path: root/src/tasks/client.rs
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-21 15:03:21 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-21 15:03:21 +0800
commitf155278db783770b4fc44931e9ac0cb14131f981 (patch)
tree4f6acf6e9b0944713ded17398168bcdee91859dd /src/tasks/client.rs
parent3fa4e33421e3fa94ca63ceb99856f97f9aac741a (diff)
finish ctx
Diffstat (limited to 'src/tasks/client.rs')
-rw-r--r--src/tasks/client.rs21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/tasks/client.rs b/src/tasks/client.rs
index ea7525a..e4510aa 100644
--- a/src/tasks/client.rs
+++ b/src/tasks/client.rs
@@ -3,6 +3,7 @@ use std::fmt::Display;
use crate::tasks::{IntoTaskErr, Task as BaseTask};
use serde::Deserialize;
+#[derive(Clone, Copy)]
pub struct Task(pub BaseTask<Name>);
#[derive(Debug, Deserialize, Clone, Copy)]
@@ -21,11 +22,11 @@ impl Display for Name {
pub const TASKS: &[Task] = &[Task(BaseTask { name: Name::Nop, exec: no_op }), Task(BaseTask { name: Name::Exec, exec: exec }), NETWORK_T];
pub const NETWORK_T: Task = Task(BaseTask { name: Name::Network, exec: network });
-fn no_op(_: &[&str]) -> Result<String, String> {
+fn no_op(_: &[&str], _: &()) -> Result<String, String> {
Ok("".to_string())
}
-fn exec(cmds: &[&str]) -> Result<String, String> {
+fn exec(cmds: &[&str], _: &()) -> Result<String, String> {
use std::process::Command;
// TODO: fmt and add exit code, and add support for both stdout and stderr, and
// support for no output
@@ -41,7 +42,7 @@ fn exec(cmds: &[&str]) -> Result<String, String> {
}
}
-fn network(switch: &[&str]) -> Result<String, String> {
+fn network(switch: &[&str], _: &()) -> Result<String, String> {
use std::process::{Command, Stdio};
let switch: bool = switch[0].parse::<bool>().map_err(|e| e.to_string())?;
@@ -128,6 +129,20 @@ impl TryFrom<&str> for &'static Task {
}
}
+// ts uses Copy trait, try avoid that
+impl TryFrom<&str> for Task {
+ type Error = IntoTaskErr;
+
+ fn try_from(value: &str) -> Result<Self, Self::Error> {
+ for task in TASKS {
+ if task.0.name.to_string() == value {
+ return Ok(*task);
+ }
+ }
+ Err(IntoTaskErr::NotFound(value.to_string()))
+ }
+}
+
impl From<Task> for BaseTask<Name> {
fn from(value: Task) -> Self {
value.0