From 3fa4e33421e3fa94ca63ceb99856f97f9aac741a Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 20 Sep 2025 23:40:49 +0800 Subject: wip ctx --- src/tasks.rs | 4 ++-- src/tasks/server.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/tasks.rs b/src/tasks.rs index 3e76ce4..069f9c2 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -4,9 +4,9 @@ pub type TaskOutput = Result; pub mod client; pub mod server; -pub struct Task { +pub struct Task { pub name: N, - pub exec: fn(&[&str]) -> TaskOutput, + pub exec: fn(&[&str], T) -> TaskOutput, } #[derive(Deserialize)] diff --git a/src/tasks/server.rs b/src/tasks/server.rs index e69de29..0896dca 100644 --- a/src/tasks/server.rs +++ b/src/tasks/server.rs @@ -0,0 +1,46 @@ +use std::fmt::Display; + +use crate::tasks::{IntoTaskErr, Task as BaseTask, TaskOutput}; +use serde::Deserialize; + +pub struct Task(pub BaseTask); + +#[derive(Debug, Deserialize, Clone, Copy)] +pub enum Name { + GetIp, +} + +impl Display for Name { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{:?}", self) + } +} + +pub const TASKS: &[Task] = &[Task::(BaseTask { name: Name::GetIp, exec: get_ip })]; + +fn get_ip(cmds: &[&str], _: i32) -> TaskOutput {} + +impl TryFrom<&str> for &'static Task { + type Error = IntoTaskErr; + + fn try_from(value: &str) -> Result { + for task in TASKS { + if task.0.name.to_string() == value { + return Ok(task); + } + } + Err(IntoTaskErr::NotFound(value.to_string())) + } +} + +impl From for BaseTask { + fn from(value: Task) -> Self { + value.0 + } +} + +impl AsRef> for Task { + fn as_ref(&self) -> &BaseTask { + &self.0 + } +} -- cgit v1.2.3