summaryrefslogtreecommitdiff
path: root/src/tasks
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
parent3fa4e33421e3fa94ca63ceb99856f97f9aac741a (diff)
finish ctx
Diffstat (limited to 'src/tasks')
-rw-r--r--src/tasks/client.rs21
-rw-r--r--src/tasks/server.rs22
2 files changed, 34 insertions, 9 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
diff --git a/src/tasks/server.rs b/src/tasks/server.rs
index 0896dca..b1cfc15 100644
--- a/src/tasks/server.rs
+++ b/src/tasks/server.rs
@@ -2,8 +2,13 @@ use std::fmt::Display;
use crate::tasks::{IntoTaskErr, Task as BaseTask, TaskOutput};
use serde::Deserialize;
+use tokio::net::TcpStream;
-pub struct Task<T = ()>(pub BaseTask<Name, T>);
+pub struct ServerCtx {
+ stream: TcpStream,
+}
+
+pub struct Task(pub BaseTask<Name, ServerCtx>);
#[derive(Debug, Deserialize, Clone, Copy)]
pub enum Name {
@@ -16,9 +21,14 @@ impl Display for Name {
}
}
-pub const TASKS: &[Task] = &[Task::<i32>(BaseTask { name: Name::GetIp, exec: get_ip })];
+pub const TASKS: &[Task] = &[Task(BaseTask { name: Name::GetIp, exec: get_ip })];
-fn get_ip(cmds: &[&str], _: i32) -> TaskOutput {}
+fn get_ip(_: &[&str], ctx: &ServerCtx) -> TaskOutput {
+ match ctx.stream.peer_addr() {
+ Ok(addr) => Ok(addr.ip().to_string()),
+ Err(e) => Err(e.to_string()),
+ }
+}
impl TryFrom<&str> for &'static Task {
type Error = IntoTaskErr;
@@ -33,14 +43,14 @@ impl TryFrom<&str> for &'static Task {
}
}
-impl From<Task> for BaseTask<Name> {
+impl From<Task> for BaseTask<Name, ServerCtx> {
fn from(value: Task) -> Self {
value.0
}
}
-impl AsRef<BaseTask<Name>> for Task {
- fn as_ref(&self) -> &BaseTask<Name> {
+impl AsRef<BaseTask<Name, ServerCtx>> for Task {
+ fn as_ref(&self) -> &BaseTask<Name, ServerCtx> {
&self.0
}
}