Common/Command/ExecuteCommand.rs
1//! # ExecuteCommand Effect
2//!
3//! Defines the `ActionEffect` for executing a registered command.
4
5use std::sync::Arc;
6
7use serde_json::Value;
8
9use super::CommandExecutor::CommandExecutor;
10use crate::{Effect::ActionEffect::ActionEffect, Error::CommonError::CommonError};
11
12/// Creates an effect that, when executed, will run a command by its unique
13/// identifier.
14///
15/// It uses the `CommandExecutor` capability from the environment to dispatch
16/// the command to the appropriate handler, whether that handler is a native
17/// Rust function or a proxied function in an external sidecar process.
18///
19/// # Parameters
20///
21/// * `CommandIdentifier`: The unique ID of the command to execute (e.g.,
22
23/// "FileSystem.ReadFile").
24/// * `Argument`: A `serde_json::Value` containing the arguments for the
25/// command.
26///
27/// # Returns
28///
29/// An `ActionEffect` that resolves with the command's return value as a
30/// `serde_json::Value`, or a `CommonError` if the command is not found or fails
31/// during execution.
32pub fn ExecuteCommand(
33 CommandIdentifier:String,
34
35 Argument:Value,
36) -> ActionEffect<Arc<dyn CommandExecutor>, CommonError, Value> {
37 ActionEffect::New(Arc::new(move |Executor:Arc<dyn CommandExecutor>| {
38 let CommandIdentifierClone = CommandIdentifier.clone();
39
40 let ArgumentClone = Argument.clone();
41
42 Box::pin(async move { Executor.ExecuteCommand(CommandIdentifierClone, ArgumentClone).await })
43 }))
44}