summaryrefslogtreecommitdiff
path: root/crates/system_action
diff options
context:
space:
mode:
Diffstat (limited to 'crates/system_action')
-rw-r--r--crates/system_action/action_macros/src/lib.rs14
-rw-r--r--crates/system_action/src/action.rs27
-rw-r--r--crates/system_action/src/action_pool.rs12
-rw-r--r--crates/system_action/src/lib.rs5
4 files changed, 37 insertions, 21 deletions
diff --git a/crates/system_action/action_macros/src/lib.rs b/crates/system_action/action_macros/src/lib.rs
index ce50073..4c03b63 100644
--- a/crates/system_action/action_macros/src/lib.rs
+++ b/crates/system_action/action_macros/src/lib.rs
@@ -64,9 +64,10 @@ fn generate_action_struct(input_fn: ItemFn, _is_local: bool) -> proc_macro2::Tok
#fn_vis async fn #proc_this_action(
pool: &action_system::action_pool::ActionPool,
- ctx: action_system::action::ActionContext,
+ mut ctx: action_system::action::ActionContext,
#arg_param_name: #arg_type
) -> Result<#return_type, tcp_connection::error::TcpTargetError> {
+ ctx.set_is_remote_action(!#_is_local);
let args_json = serde_json::to_string(&#arg_param_name)
.map_err(|e| {
tcp_connection::error::TcpTargetError::Serialization(e.to_string())
@@ -121,11 +122,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()
- && segment.ident != "Result" {
- panic!(
- "Expected Action function to return Result<T, TcpTargetError>, but found different return type"
- );
- }
+ && 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 8a6180a..9eef1db 100644
--- a/crates/system_action/src/action.rs
+++ b/crates/system_action/src/action.rs
@@ -23,7 +23,10 @@ where
#[derive(Default)]
pub struct ActionContext {
/// Whether the action is executed locally or remotely
- local: bool,
+ proc_on_local: bool,
+
+ /// Whether the action being executed in the current context is a remote action
+ is_remote_action: bool,
/// The name of the action being executed
action_name: String,
@@ -42,7 +45,7 @@ impl ActionContext {
/// Generate local context
pub fn local() -> Self {
ActionContext {
- local: true,
+ proc_on_local: true,
..Default::default()
}
}
@@ -50,7 +53,7 @@ impl ActionContext {
/// Generate remote context
pub fn remote() -> Self {
ActionContext {
- local: false,
+ proc_on_local: false,
..Default::default()
}
}
@@ -75,13 +78,23 @@ impl ActionContext {
impl ActionContext {
/// Whether the action is executed locally
- pub fn is_local(&self) -> bool {
- self.local
+ pub fn is_proc_on_local(&self) -> bool {
+ self.proc_on_local
}
/// Whether the action is executed remotely
- pub fn is_remote(&self) -> bool {
- !self.local
+ pub fn is_proc_on_remote(&self) -> bool {
+ !self.proc_on_local
+ }
+
+ /// Whether the action being executed in the current context is a remote action
+ pub fn is_remote_action(&self) -> bool {
+ self.is_remote_action
+ }
+
+ /// Set whether the action being executed in the current context is a remote action
+ pub fn set_is_remote_action(&mut self, is_remote_action: bool) {
+ self.is_remote_action = is_remote_action;
}
/// Get the connection instance in the current context
diff --git a/crates/system_action/src/action_pool.rs b/crates/system_action/src/action_pool.rs
index c28de1e..c3ad4a1 100644
--- a/crates/system_action/src/action_pool.rs
+++ b/crates/system_action/src/action_pool.rs
@@ -7,7 +7,7 @@ use tcp_connection::error::TcpTargetError;
use crate::action::{Action, ActionContext};
type ProcBeginCallback = for<'a> fn(
- &'a ActionContext,
+ &'a mut ActionContext,
args: &'a (dyn std::any::Any + Send + Sync),
) -> ProcBeginFuture<'a>;
type ProcEndCallback = fn() -> ProcEndFuture;
@@ -94,9 +94,9 @@ impl ActionPool {
if let Some(action) = self.actions.get(action_name) {
// Set action name and args in context for callbacks
let context = context.set_action_name(action_name.to_string());
- let context = context.set_action_args(args_json.clone());
+ let mut context = context.set_action_args(args_json.clone());
- self.exec_on_proc_begin(&context, &args_json).await?;
+ self.exec_on_proc_begin(&mut context, &args_json).await?;
let result = action.process_json_erased(context, args_json).await?;
self.exec_on_proc_end().await?;
Ok(result)
@@ -114,7 +114,7 @@ impl ActionPool {
pub async fn process<'a, Args, Return>(
&'a self,
action_name: &'a str,
- context: ActionContext,
+ mut context: ActionContext,
args: Args,
) -> Result<Return, TcpTargetError>
where
@@ -122,7 +122,7 @@ impl ActionPool {
Return: serde::Serialize + Send + 'static,
{
if let Some(action) = self.actions.get(action_name) {
- self.exec_on_proc_begin(&context, &args).await?;
+ self.exec_on_proc_begin(&mut context, &args).await?;
let result = action.process_erased(context, Box::new(args)).await?;
let result = *result
.downcast::<Return>()
@@ -137,7 +137,7 @@ impl ActionPool {
/// Executes the process begin callback if set
async fn exec_on_proc_begin(
&self,
- context: &ActionContext,
+ context: &mut ActionContext,
args: &(dyn std::any::Any + Send + Sync),
) -> Result<(), TcpTargetError> {
if let Some(callback) = &self.on_proc_begin {
diff --git a/crates/system_action/src/lib.rs b/crates/system_action/src/lib.rs
index 07be1bb..12ae999 100644
--- a/crates/system_action/src/lib.rs
+++ b/crates/system_action/src/lib.rs
@@ -1,5 +1,6 @@
-pub use action_system_macros::*;
-pub extern crate action_system_macros as macros;
+pub mod macros {
+ pub use action_system_macros::*;
+}
pub mod action;
pub mod action_pool;