diff options
| author | 魏曹先生 <1992414357@qq.com> | 2025-11-25 15:10:53 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2025-11-25 15:10:53 +0800 |
| commit | 292227c83753564cde90de7a4f43d9ef4660bd4c (patch) | |
| tree | 5a8f4aaddf85b689985f129fc6d6d4c16e6a478e /crates/system_action/src | |
| parent | f2ecc33f447ba0ccae261720019fa2820aa21875 (diff) | |
Improve documentation for system action and config file crates
Diffstat (limited to 'crates/system_action/src')
| -rw-r--r-- | crates/system_action/src/action.rs | 56 | ||||
| -rw-r--r-- | crates/system_action/src/action_pool.rs | 20 |
2 files changed, 75 insertions, 1 deletions
diff --git a/crates/system_action/src/action.rs b/crates/system_action/src/action.rs index ef1bf11..62425ff 100644 --- a/crates/system_action/src/action.rs +++ b/crates/system_action/src/action.rs @@ -5,6 +5,40 @@ use std::sync::Arc; use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; use tokio::{net::TcpStream, sync::Mutex}; +/// # Trait - Action<Args, Return> +/// +/// A trait used to describe the interaction pattern between client and server +/// +/// ## Generics +/// +/// Args: Represents the parameter type required for this action +/// +/// Return: Represents the return type of this action +/// +/// The above generics must implement serde's Serialize and DeserializeOwned traits, +/// and must be sendable between threads +/// +/// ## Implementation +/// +/// ```ignore +/// pub trait Action<Args, Return> +/// where +/// Args: Serialize + DeserializeOwned + Send, +/// Return: Serialize + DeserializeOwned + Send, +/// { +/// /// Name, used to inform the server which action to execute +/// fn action_name() -> &'static str; +/// +/// /// Whether it's a local Action, used to inform the system if it only runs locally +/// fn is_remote_action() -> bool; +/// +/// /// Action processing logic +/// fn process( +/// context: ActionContext, +/// args: Args, +/// ) -> impl std::future::Future<Output = Result<Return, TcpTargetError>> + Send; +/// } +/// ``` pub trait Action<Args, Return> where Args: Serialize + DeserializeOwned + Send, @@ -20,6 +54,28 @@ where ) -> impl std::future::Future<Output = Result<Return, TcpTargetError>> + Send; } +/// # Struct - ActionContext +/// +/// Used to inform the Action about the current execution environment +/// +/// ## Creation +/// +/// Create ActionContext using the following methods: +/// +/// ```ignore +/// +/// // The instance here is the connection instance passed from external sources for communicating with the server +/// // For specific usage, please refer to the `/crates/utils/tcp_connection` section +/// +/// fn init_local_action_ctx(instance: ConnectionInstance) { +/// // Create context and specify execution on local +/// let mut ctx = ActionContext::local(); +/// } +/// +/// fn init_remote_action_ctx(instance: ConnectionInstance) { +/// // Create context and specify execution on remote +/// let mut ctx = ActionContext::remote(); +/// } #[derive(Default)] pub struct ActionContext { /// Whether the action is executed locally or remotely diff --git a/crates/system_action/src/action_pool.rs b/crates/system_action/src/action_pool.rs index c3ad4a1..019fa6d 100644 --- a/crates/system_action/src/action_pool.rs +++ b/crates/system_action/src/action_pool.rs @@ -15,7 +15,25 @@ type ProcEndCallback = fn() -> ProcEndFuture; type ProcBeginFuture<'a> = Pin<Box<dyn Future<Output = Result<(), TcpTargetError>> + Send + 'a>>; type ProcEndFuture = Pin<Box<dyn Future<Output = Result<(), TcpTargetError>> + Send>>; -/// A pool of registered actions that can be processed by name +/// # Struct - ActionPool +/// +/// This struct is used to register and record all accessible and executable actions +/// +/// It also registers `on_proc_begin` and `on_proc_end` callback functions +/// used for action initialization +/// +/// ## Creating and registering actions +/// ```ignore +/// fn init_action_pool() { +/// let mut pool = Action::new(); +/// +/// // Register action +/// pool.register<YourAction, ActionArgument, ActionReturn>(); +/// +/// // If the action is implemented with `#[action_gen]`, you can also do +/// register_your_action(&mut pool); +/// } +/// ``` pub struct ActionPool { /// HashMap storing action name to action implementation mapping actions: std::collections::HashMap<&'static str, Box<dyn ActionErased>>, |
