From bebc63d7bcaaf30f9ffe0a58548250c8cc7189d4 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 14 Sep 2025 18:55:27 +0800 Subject: chore: move tasks to lib indicate this will be shared with server as well, type safety? --- src/client/connector.rs | 3 +- src/client/connector/connectors/httpconnector.rs | 3 +- src/client/executor.rs | 4 +- src/client/executor/tasks.rs | 171 ----------------------- src/client/todo.md | 3 + 5 files changed, 8 insertions(+), 176 deletions(-) delete mode 100644 src/client/executor/tasks.rs create mode 100644 src/client/todo.md (limited to 'src/client') diff --git a/src/client/connector.rs b/src/client/connector.rs index 869703a..b111fc5 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -3,7 +3,8 @@ pub mod manager; use std::{thread::sleep, time::Duration}; -use crate::executor::{Executor, tasks::IntoTask}; +use crate::executor::Executor; +use remote_control_rs::tasks::IntoTask; #[derive(Debug)] pub enum Result { diff --git a/src/client/connector/connectors/httpconnector.rs b/src/client/connector/connectors/httpconnector.rs index 0dd198b..0b9d590 100644 --- a/src/client/connector/connectors/httpconnector.rs +++ b/src/client/connector/connectors/httpconnector.rs @@ -3,7 +3,8 @@ use reqwest::{ blocking::{Client, Response}, }; -use crate::{connector::*, executor::tasks::TASKS}; +use crate::connector::*; +use remote_control_rs::tasks::TASKS; use std::{thread::sleep, time::Duration}; pub struct HTTPConnector { diff --git a/src/client/executor.rs b/src/client/executor.rs index 8ef98b5..ad419f0 100644 --- a/src/client/executor.rs +++ b/src/client/executor.rs @@ -1,6 +1,4 @@ -use crate::executor::tasks::{IntoTask, IntoTaskErr, Task}; - -pub mod tasks; +use remote_control_rs::tasks::{IntoTask, IntoTaskErr, Task}; pub struct Executor { task: &'static Task, diff --git a/src/client/executor/tasks.rs b/src/client/executor/tasks.rs deleted file mode 100644 index 3d9d940..0000000 --- a/src/client/executor/tasks.rs +++ /dev/null @@ -1,171 +0,0 @@ -pub struct Task { - pub name: &'static str, - pub exec: fn(&[String]) -> Result, -} - -impl PartialEq for &Task { - fn eq(&self, other: &Self) -> bool { - self.name == other.name - } -} - -pub const TASKS: &[Task] = &[ - Task { - name: "nop", - exec: no_op, - }, - Task { - name: "exec", - exec: exec, - }, - Task { - name: "network", - exec: network, - }, -]; - -fn no_op(_: &[String]) -> Result { - Ok("".to_string()) -} - -fn exec(cmds: &[String]) -> Result { - use std::process::Command; - // TODO: fmt and add exit code, and add support for both stdout and stderr, and support for no output - match Command::new(cmds[0].as_str()).args(&cmds[1..]).output() { - Ok(o) => { - if o.status.success() { - Ok(String::from_utf8_lossy(&o.stdout).to_string()) - } else { - Err(String::from_utf8_lossy(&o.stderr).to_string()) - } - } - Err(e) => Err(e.to_string()), - } -} - -fn network(switch: &[String]) -> Result { - use std::process::{Command, Stdio}; - let switch: bool = match switch[0].parse() { - Ok(s) => s, - Err(e) => return Err(e.to_string()), - }; - - // TODO: windows and linux - - // todo let .. = Command::new('networksetup') - #[cfg(target_os = "macos")] - { - use std::io::Read; - match Command::new("networksetup") - .arg("-listallhardwareports") - .stdout(Stdio::piped()) - .spawn() - { - Ok(devs) => { - match Command::new("awk") - .arg(r#"/Wi-Fi|AirPort/{getline; print $2}"#) - .stdin(Stdio::from(match devs.stdout { - Some(s) => s, - None => match devs.stderr { - Some(mut e) => { - let mut reason = String::new(); - match e.read_to_string(&mut reason) { - Ok(_) => {} - Err(e) => return Err(format!("networksetup failed to list hardware: {e}")) - } - return Err(format!( - "networksetup failed to list hardware: {reason}" - )); - } - None => Err("networksetup failed to list hardware".to_string())?, - }, - })) - .stdout(Stdio::piped()) - .spawn() - { - Ok(dev) => match Command::new("networksetup").arg("-setairportpower").arg( - match dev.stdout { - Some(mut o) => { - let mut output = String::new(); - match o.read_to_string(&mut output) { - Ok(_) => (), - Err(e) => return Err(format!("awk failed to get device from networksetup list hardware: {e}")) - }; - output - } - None => match dev.stderr { - Some(mut e) => { - let mut reason = String::new(); - match e.read_to_string(&mut reason) { - Ok(_) => {} - Err(e) => return Err(format!("awk failed to get device from networksetup list hardware: {e}")) - } - return Err(format!("awk failed to get device from networksetup list hardware: {reason}")); - } - None => Err("awk failed to get device from networksetup list hardware".to_string())?, - }, - }, - ).arg(if switch { "on" } else { "off" }).output() { - Ok(o) => { - if o.status.success() { - Ok(String::from_utf8_lossy(&o.stdout) - .to_string()) - } else { - Err(String::from_utf8_lossy(&o.stderr) - .to_string()) - } - } - Err(e) => Err(e.to_string()), - }, - Err(e) => Err(e.to_string()), - } - } - Err(e) => Err(e.to_string()), - } - } - - #[cfg(not(target_os = "macos"))] - Err("not implemented".to_string()) -} - -#[derive(Debug)] -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 IntoTask for &Task { - fn into_task<'a>(self) -> Result<&'a Task, IntoTaskErr> { - for task in TASKS { - if task == self { - return Ok(task); - } - } - Err(IntoTaskErr::NotFound(self.name.to_string())) - } -} diff --git a/src/client/todo.md b/src/client/todo.md new file mode 100644 index 0000000..11eecf5 --- /dev/null +++ b/src/client/todo.md @@ -0,0 +1,3 @@ +- [ ] multithreading(tokio) +- [ ] http json option +- [ ] collect basic stuff -- cgit v1.2.3