summaryrefslogtreecommitdiff
path: root/crates/system_action
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-10-13 14:27:01 +0800
committer魏曹先生 <1992414357@qq.com>2025-10-13 14:27:01 +0800
commit4810f56e6a49b60923eb850d5944457650c81c75 (patch)
treeef02095c73635b5ace574c26dfcb999017e34897 /crates/system_action
parentacf0804b5f9bdc2796d847919a8ae20103be600a (diff)
Fix Clippy warnings and optimize code
- Fix let_underscore_future warning by properly awaiting async functions - Make accept_import function async to match add_mapping usage - Propagate errors properly with ? operator instead of ignoring them - Replace manual Default implementation with derive attribute - Replace vec! with array literal to avoid useless_vec warning - All tests pass and code is now Clippy clean
Diffstat (limited to 'crates/system_action')
-rw-r--r--crates/system_action/action_macros/src/lib.rs7
-rw-r--r--crates/system_action/src/action.rs14
-rw-r--r--crates/system_action/src/action_pool.rs48
3 files changed, 41 insertions, 28 deletions
diff --git a/crates/system_action/action_macros/src/lib.rs b/crates/system_action/action_macros/src/lib.rs
index d1a47ee..ce50073 100644
--- a/crates/system_action/action_macros/src/lib.rs
+++ b/crates/system_action/action_macros/src/lib.rs
@@ -101,7 +101,7 @@ fn generate_action_struct(input_fn: ItemFn, _is_local: bool) -> proc_macro2::Tok
}
fn validate_function_signature(fn_sig: &syn::Signature) {
- if !fn_sig.asyncness.is_some() {
+ if fn_sig.asyncness.is_none() {
panic!("Expected async function for Action, but found synchronous function");
}
@@ -120,13 +120,12 @@ fn validate_function_signature(fn_sig: &syn::Signature) {
};
if let syn::Type::Path(type_path) = return_type.as_ref() {
- if let Some(segment) = type_path.path.segments.last() {
- if segment.ident != "Result" {
+ if let Some(segment) = type_path.path.segments.last()
+ && segment.ident != "Result" {
panic!(
"Expected Action function to return Result<T, TcpTargetError>, but found different return type"
);
}
- }
} else {
panic!(
"Expected Action function to return Result<T, TcpTargetError>, but found no return type"
diff --git a/crates/system_action/src/action.rs b/crates/system_action/src/action.rs
index 3ae5711..8a6180a 100644
--- a/crates/system_action/src/action.rs
+++ b/crates/system_action/src/action.rs
@@ -41,16 +41,18 @@ pub struct ActionContext {
impl ActionContext {
/// Generate local context
pub fn local() -> Self {
- let mut ctx = ActionContext::default();
- ctx.local = true;
- ctx
+ ActionContext {
+ local: true,
+ ..Default::default()
+ }
}
/// Generate remote context
pub fn remote() -> Self {
- let mut ctx = ActionContext::default();
- ctx.local = false;
- ctx
+ ActionContext {
+ local: false,
+ ..Default::default()
+ }
}
/// Build connection instance from TcpStream
diff --git a/crates/system_action/src/action_pool.rs b/crates/system_action/src/action_pool.rs
index f3e178a..c28de1e 100644
--- a/crates/system_action/src/action_pool.rs
+++ b/crates/system_action/src/action_pool.rs
@@ -6,12 +6,14 @@ use tcp_connection::error::TcpTargetError;
use crate::action::{Action, ActionContext};
-type ProcBeginCallback =
- for<'a> fn(
- &'a ActionContext,
- args: &'a (dyn std::any::Any + Send + Sync),
- ) -> Pin<Box<dyn Future<Output = Result<(), TcpTargetError>> + Send + 'a>>;
-type ProcEndCallback = fn() -> Pin<Box<dyn Future<Output = Result<(), TcpTargetError>> + Send>>;
+type ProcBeginCallback = for<'a> fn(
+ &'a ActionContext,
+ args: &'a (dyn std::any::Any + Send + Sync),
+) -> ProcBeginFuture<'a>;
+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
pub struct ActionPool {
@@ -25,6 +27,12 @@ pub struct ActionPool {
on_proc_end: Option<ProcEndCallback>,
}
+impl Default for ActionPool {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
impl ActionPool {
/// Creates a new empty ActionPool
pub fn new() -> Self {
@@ -88,9 +96,9 @@ impl ActionPool {
let context = context.set_action_name(action_name.to_string());
let context = context.set_action_args(args_json.clone());
- let _ = self.exec_on_proc_begin(&context, &args_json).await?;
+ self.exec_on_proc_begin(&context, &args_json).await?;
let result = action.process_json_erased(context, args_json).await?;
- let _ = self.exec_on_proc_end().await?;
+ self.exec_on_proc_end().await?;
Ok(result)
} else {
Err(TcpTargetError::Unsupported("InvalidAction".to_string()))
@@ -106,7 +114,7 @@ impl ActionPool {
pub async fn process<'a, Args, Return>(
&'a self,
action_name: &'a str,
- mut context: ActionContext,
+ context: ActionContext,
args: Args,
) -> Result<Return, TcpTargetError>
where
@@ -114,12 +122,12 @@ impl ActionPool {
Return: serde::Serialize + Send + 'static,
{
if let Some(action) = self.actions.get(action_name) {
- let _ = self.exec_on_proc_begin(&context, &args).await?;
+ self.exec_on_proc_begin(&context, &args).await?;
let result = action.process_erased(context, Box::new(args)).await?;
let result = *result
.downcast::<Return>()
.map_err(|_| TcpTargetError::Unsupported("InvalidArguments".to_string()))?;
- let _ = self.exec_on_proc_end().await?;
+ self.exec_on_proc_end().await?;
Ok(result)
} else {
Err(TcpTargetError::Unsupported("InvalidAction".to_string()))
@@ -150,25 +158,29 @@ impl ActionPool {
}
/// Trait for type-erased actions that can be stored in ActionPool
+type ProcessErasedFuture = std::pin::Pin<
+ Box<
+ dyn std::future::Future<Output = Result<Box<dyn std::any::Any + Send>, TcpTargetError>>
+ + Send,
+ >,
+>;
+type ProcessJsonErasedFuture =
+ std::pin::Pin<Box<dyn std::future::Future<Output = Result<String, TcpTargetError>> + Send>>;
+
trait ActionErased: Send + Sync {
/// Processes the action with type-erased arguments and returns type-erased result
fn process_erased(
&self,
context: ActionContext,
args: Box<dyn std::any::Any + Send>,
- ) -> std::pin::Pin<
- Box<
- dyn std::future::Future<Output = Result<Box<dyn std::any::Any + Send>, TcpTargetError>>
- + Send,
- >,
- >;
+ ) -> ProcessErasedFuture;
/// Processes the action with JSON-serialized arguments and returns JSON-serialized result
fn process_json_erased(
&self,
context: ActionContext,
args_json: String,
- ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<String, TcpTargetError>> + Send>>;
+ ) -> ProcessJsonErasedFuture;
}
/// Wrapper struct that implements ActionErased for concrete Action types