From d9a39247012130723b3c023ea9cdbe37d89f6f58 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 6 Oct 2025 17:20:12 +0800 Subject: refactor: optimize procedural macros in action_system - Add register_to_pool and process_at_pool helper methods to generated structs - Improve documentation with usage examples - Enhance macro-generated code structure --- crates/system_action/action_macros/src/lib.rs | 34 ++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/system_action/action_macros/src/lib.rs b/crates/system_action/action_macros/src/lib.rs index 5d274ad..a7de9b6 100644 --- a/crates/system_action/action_macros/src/lib.rs +++ b/crates/system_action/action_macros/src/lib.rs @@ -55,10 +55,38 @@ fn generate_action_struct(input_fn: ItemFn, _is_local: bool) -> proc_macro2::Tok } } + impl #struct_name { + #fn_vis fn register_to_pool(pool: &mut action_system::action_pool::ActionPool) { + pool.register::<#struct_name, #arg_type, #return_type>(); + } + + #fn_vis async fn process_at_pool<'a>( + pool: &'a action_system::action_pool::ActionPool, + ctx: action_system::action::ActionContext, + #arg_param_name: #arg_type + ) -> Result<#return_type, tcp_connection::error::TcpTargetError> { + pool.process::<#arg_type, #return_type>( + Box::leak(string_proc::snake_case!(stringify!(#action_name_ident)).into_boxed_str()), + ctx, + #arg_param_name + ).await + } + } + + #[allow(dead_code)] #[deprecated = "This function is used by #[action_gen] as a template."] - #[doc = " This function is used by #[action_gen] as a template to generate the struct. "] - #[doc = " It is forbidden to call it anywhere."] - #[doc = " and call it using the function name."] + #[doc = "Template function for #[[action_gen]] - do not call directly."] + #[doc = "Use the generated struct instead."] + #[doc = ""] + #[doc = "Register the action to the pool."] + #[doc = "```rust"] + #[doc = "YourActionPascalName::register_to_pool(&mut pool);"] + #[doc = "```"] + #[doc = ""] + #[doc = "Process the action at the pool."] + #[doc = "```rust"] + #[doc = "let result = YourActionPascalName::process_at_pool(&pool, ctx, arg).await?;"] + #[doc = "```"] #fn_vis #fn_sig #fn_block } } -- cgit From a6f73176a51c3aaef5c16b7c9d6f4923c9557d67 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 6 Oct 2025 17:20:19 +0800 Subject: feat: add default implementation and convenience methods to ActionContext - Implement Default trait for ActionContext - Add local() and remote() constructor methods - Make instance field optional for better flexibility --- crates/system_action/src/action.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/system_action/src/action.rs b/crates/system_action/src/action.rs index 14f1148..562a142 100644 --- a/crates/system_action/src/action.rs +++ b/crates/system_action/src/action.rs @@ -11,13 +11,30 @@ pub trait Action { ) -> impl std::future::Future> + Send; } +#[derive(Default)] pub struct ActionContext { // Whether the action is executed locally or remotely local: bool, /// The connection instance in the current context, /// used to interact with the machine on the other end - instance: ConnectionInstance, + instance: Option, +} + +impl ActionContext { + /// Generate local context + pub fn local() -> Self { + let mut ctx = ActionContext::default(); + ctx.local = true; + ctx + } + + /// Generate remote context + pub fn remote() -> Self { + let mut ctx = ActionContext::default(); + ctx.local = false; + ctx + } } impl ActionContext { @@ -32,7 +49,7 @@ impl ActionContext { } /// Get the connection instance in the current context - pub fn instance(&self) -> &ConnectionInstance { + pub fn instance(&self) -> &Option { &self.instance } } -- cgit From dd9e70ad313a7d505370217a411bca41dbb5d36e Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 6 Oct 2025 17:20:25 +0800 Subject: feat: add new error variants to TcpTargetError - Add NotLocal error for actions requiring local execution - Add NotRemote error for actions requiring remote execution --- crates/utils/tcp_connection/src/error.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/utils/tcp_connection/src/error.rs b/crates/utils/tcp_connection/src/error.rs index ffcce6f..691e5ee 100644 --- a/crates/utils/tcp_connection/src/error.rs +++ b/crates/utils/tcp_connection/src/error.rs @@ -35,6 +35,12 @@ pub enum TcpTargetError { #[error("Pool already exists: {0}")] PoolAlreadyExists(String), + + #[error("Not local machine: {0}")] + NotLocal(String), + + #[error("Not remote machine: {0}")] + NotRemote(String), } impl From for TcpTargetError { -- cgit From 58a2c77dace3091fda4ba3903f700b8e928f90c1 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 6 Oct 2025 17:20:31 +0800 Subject: feat: add examples demonstrating action system usage - Create examples workspace member - Add example_action_system binary showcasing macro usage - Demonstrate action registration and processing workflow --- examples/Cargo.toml | 21 +++++++++++++++++++++ examples/src/bin/example_action_system.rs | 18 ++++++++++++++++++ examples/src/lib.rs | 1 + 3 files changed, 40 insertions(+) create mode 100644 examples/Cargo.toml create mode 100644 examples/src/bin/example_action_system.rs create mode 100644 examples/src/lib.rs diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 0000000..3a6cee2 --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "examples" +edition = "2024" +version.workspace = true + +[[bin]] +name = "example_action_system" +path = "src/bin/example_action_system.rs" + +[dependencies] + +# Utils +tcp_connection = { path = "../crates/utils/tcp_connection" } +cfg_file = { path = "../crates/utils/cfg_file", features = ["default"] } +string_proc = { path = "../crates/utils/string_proc" } + +# Core +action_system = { path = "../crates/system_action" } + +# Async & Networking +tokio = { version = "1.46.1", features = ["full"] } diff --git a/examples/src/bin/example_action_system.rs b/examples/src/bin/example_action_system.rs new file mode 100644 index 0000000..a659eb3 --- /dev/null +++ b/examples/src/bin/example_action_system.rs @@ -0,0 +1,18 @@ +use action_system::{action::ActionContext, action_gen, action_pool::ActionPool}; +use tcp_connection::error::TcpTargetError; + +#[tokio::main] +async fn main() { + let mut pool = ActionPool::new(); + PrintNameAction::register_to_pool(&mut pool); + + PrintNameAction::process_at_pool(&pool, ActionContext::local(), "World".to_string()) + .await + .unwrap(); +} + +#[action_gen] +async fn print_name_action(_ctx: ActionContext, name: String) -> Result<(), TcpTargetError> { + println!("Hello, {}!", name); + Ok(()) +} diff --git a/examples/src/lib.rs b/examples/src/lib.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/src/lib.rs @@ -0,0 +1 @@ + -- cgit From e12f167de8e16baa78c86b09eab75201281d3f95 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 6 Oct 2025 17:20:40 +0800 Subject: chore: update workspace configuration and dependencies - Add examples to workspace members - Update vcs_data dependencies structure - Fix library re-export paths in main lib.rs --- Cargo.lock | 11 +++++++++++ Cargo.toml | 2 ++ crates/vcs_data/Cargo.toml | 4 ++++ src/lib.rs | 4 ++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ed2554..bad00f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -412,6 +412,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "examples" +version = "0.1.0" +dependencies = [ + "action_system", + "cfg_file", + "string_proc", + "tcp_connection", + "tokio", +] + [[package]] name = "fiat-crypto" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 72f3270..881fbbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,8 @@ vcs = [] [workspace] members = [ + "examples", + "crates/utils/cfg_file", "crates/utils/cfg_file/cfg_file_derive", "crates/utils/cfg_file/cfg_file_test", diff --git a/crates/vcs_data/Cargo.toml b/crates/vcs_data/Cargo.toml index 07f1a6a..b3bc4a2 100644 --- a/crates/vcs_data/Cargo.toml +++ b/crates/vcs_data/Cargo.toml @@ -4,9 +4,13 @@ edition = "2024" version.workspace = true [dependencies] + +# Utils tcp_connection = { path = "../utils/tcp_connection" } cfg_file = { path = "../utils/cfg_file", features = ["default"] } string_proc = { path = "../utils/string_proc" } + +# Core action_system = { path = "../system_action" } # Identity diff --git a/src/lib.rs b/src/lib.rs index 746f66f..29855e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,8 @@ // Feature `vcs` #[cfg(feature = "vcs")] pub mod vcs { - extern crate vcs; - pub use vcs::*; + extern crate vcs_data; + pub use vcs_data::*; extern crate vcs_actions; pub use vcs_actions::*; -- cgit