Mountain/RPC/CocoonService/Command/
ExecuteContributedCommand.rs1#![allow(non_snake_case)]
2
3use CommonLibrary::Command::CommandExecutor::CommandExecutor;
7use serde_json::json;
8use tonic::{Response, Status};
9
10use crate::{
11 RPC::CocoonService::CocoonServiceImpl,
12 Vine::Generated::{ExecuteCommandRequest, ExecuteCommandResponse, RpcError, argument, execute_command_response},
13 dev_log,
14};
15
16pub async fn Fn(
17 Service:&CocoonServiceImpl,
18
19 Request:ExecuteCommandRequest,
20) -> Result<Response<ExecuteCommandResponse>, Status> {
21 dev_log!(
22 "cocoon",
23 "[CocoonService] Executing command '{}' with {} arguments",
24 Request.command_id,
25 Request.arguments.len()
26 );
27
28 for (Index, Argument) in Request.arguments.iter().enumerate() {
29 dev_log!("cocoon", "[CocoonService] Argument {}: {:?}", Index, Argument);
30 }
31
32 let Arg:serde_json::Value = Request
33 .arguments
34 .first()
35 .and_then(|A| A.value.as_ref())
36 .map(|V| {
37 match V {
38 argument::Value::StringValue(S) => json!(S),
39 argument::Value::IntValue(I) => json!(I),
40 argument::Value::BoolValue(B) => json!(B),
41 argument::Value::BytesValue(Bytes) => serde_json::from_slice(Bytes).unwrap_or(serde_json::Value::Null),
42 }
43 })
44 .unwrap_or(serde_json::Value::Null);
45
46 match Service.environment.ExecuteCommand(Request.command_id, Arg).await {
47 Ok(Value) => {
48 let Bytes = serde_json::to_vec(&Value).unwrap_or_default();
49
50 Ok(Response::new(ExecuteCommandResponse {
51 result:Some(execute_command_response::Result::Value(Bytes)),
52 }))
53 },
54
55 Err(Error) => {
56 let Bytes = serde_json::to_vec(&Error.to_string()).unwrap_or_default();
57
58 Ok(Response::new(ExecuteCommandResponse {
59 result:Some(execute_command_response::Result::Error(RpcError {
60 code:-32000,
61 message:Error.to_string(),
62 data:Bytes,
63 })),
64 }))
65 },
66 }
67}