rusp_lib/usp_builder/
operate.rs

1use crate::usp_errors;
2use std::collections::HashMap;
3
4use crate::usp::mod_Body::OneOfmsg_body::{request, response};
5use crate::usp::mod_OperateResp::mod_OperationResult::OutputArgs;
6use crate::usp::mod_OperateResp::{
7    mod_OperationResult::{CommandFailure, OneOfoperation_resp},
8    OperationResult,
9};
10use crate::usp::mod_Request::OneOfreq_type::operate;
11use crate::usp::mod_Response::OneOfresp_type::operate_resp;
12use crate::usp::{Body, Operate, OperateResp, Request, Response};
13
14use anyhow::Result;
15
16#[derive(Clone)]
17pub struct OperateBuilder {
18    command: String,
19    command_key: String,
20    send_resp: bool,
21    input_args: Vec<(String, String)>,
22}
23
24impl OperateBuilder {
25    #[must_use]
26    pub const fn new(command: String) -> Self {
27        Self {
28            command,
29            command_key: String::new(),
30            send_resp: false,
31            input_args: vec![],
32        }
33    }
34
35    #[must_use]
36    pub fn with_command_key(mut self, command_key: String) -> Self {
37        self.command_key = command_key;
38        self
39    }
40
41    #[must_use]
42    pub const fn with_send_resp(mut self, send_resp: bool) -> Self {
43        self.send_resp = send_resp;
44        self
45    }
46
47    #[must_use]
48    pub fn with_input_args(mut self, input_args: Vec<(String, String)>) -> Self {
49        self.input_args = input_args;
50        self
51    }
52
53    pub fn build(self) -> Result<Body> {
54        Ok(Body {
55            msg_body: request({
56                Request {
57                    req_type: operate({
58                        Operate {
59                            command: self.command,
60                            command_key: self.command_key,
61                            send_resp: self.send_resp,
62                            input_args: self.input_args.into_iter().collect(),
63                        }
64                    }),
65                }
66            }),
67        })
68    }
69}
70
71#[derive(Clone)]
72pub enum OperateRespOperationResult {
73    Failure {
74        err_code: u32,
75        err_msg: String,
76    },
77    Path {
78        req_obj_path: String,
79    },
80    OutputArgs {
81        output_args: HashMap<String, String>,
82    },
83    None,
84}
85
86#[derive(Clone)]
87pub struct OperateRespResultBuilder {
88    executed_command: String,
89    operation_result: OperateRespOperationResult,
90}
91
92impl OperateRespResultBuilder {
93    #[must_use]
94    pub const fn new(executed_command: String) -> Self {
95        Self {
96            operation_result: OperateRespOperationResult::None,
97            executed_command,
98        }
99    }
100
101    #[must_use]
102    pub fn set_failure(mut self, err_code: u32, err_msg: Option<String>) -> Self {
103        self.operation_result = OperateRespOperationResult::Failure {
104            err_code,
105            err_msg: err_msg.unwrap_or_else(|| usp_errors::get_err_msg(err_code).to_string()),
106        };
107        self
108    }
109
110    #[must_use]
111    pub fn set_path(mut self, req_obj_path: String) -> Self {
112        self.operation_result = OperateRespOperationResult::Path { req_obj_path };
113        self
114    }
115
116    #[must_use]
117    pub fn set_output_args(mut self, output_args: Vec<(String, String)>) -> Self {
118        self.operation_result = OperateRespOperationResult::OutputArgs {
119            output_args: output_args.into_iter().collect(),
120        };
121        self
122    }
123
124    pub fn build(self) -> Result<OperationResult> {
125        match self.operation_result {
126            OperateRespOperationResult::OutputArgs { output_args } => Ok(OperationResult {
127                operation_resp: OneOfoperation_resp::req_output_args(OutputArgs {
128                    output_args: output_args.into_iter().collect::<HashMap<_, _>>(),
129                }),
130                executed_command: self.executed_command,
131            }),
132            OperateRespOperationResult::Failure { err_code, err_msg } => Ok(OperationResult {
133                operation_resp: OneOfoperation_resp::cmd_failure(CommandFailure {
134                    err_code,
135                    err_msg,
136                }),
137                executed_command: self.executed_command,
138            }),
139            OperateRespOperationResult::None => Err(anyhow::anyhow!(
140                "Need to have either OutputArgs or Path or Failure"
141            )),
142            OperateRespOperationResult::Path { req_obj_path } => Ok(OperationResult {
143                operation_resp: OneOfoperation_resp::req_obj_path(req_obj_path),
144                executed_command: self.executed_command,
145            }),
146        }
147    }
148}
149
150#[derive(Clone)]
151pub struct OperateRespBuilder {
152    operation_results: Vec<OperateRespResultBuilder>,
153}
154
155impl OperateRespBuilder {
156    #[must_use]
157    pub const fn new() -> Self {
158        Self {
159            operation_results: vec![],
160        }
161    }
162
163    #[must_use]
164    pub fn with_operation_results(
165        mut self,
166        operation_results: Vec<OperateRespResultBuilder>,
167    ) -> Self {
168        self.operation_results = operation_results;
169        self
170    }
171
172    pub fn build(self) -> Result<Body> {
173        Ok(Body {
174            msg_body: response({
175                Response {
176                    resp_type: operate_resp(OperateResp {
177                        operation_results: self
178                            .operation_results
179                            .into_iter()
180                            .map(OperateRespResultBuilder::build)
181                            .collect::<Result<Vec<_>>>()?,
182                    }),
183                }
184            }),
185        })
186    }
187}