rusp_lib/usp_builder/
set.rs

1use std::collections::HashMap;
2
3use crate::usp::mod_Set::{UpdateObject, UpdateParamSetting};
4use crate::usp::mod_SetResp::mod_UpdatedObjectResult::mod_OperationStatus::{
5    OneOfoper_status::{oper_failure, oper_success},
6    OperationFailure, OperationSuccess,
7};
8use crate::usp::mod_SetResp::ParameterError;
9use crate::usp::mod_SetResp::{mod_UpdatedObjectResult::OperationStatus, UpdatedObjectResult};
10use crate::usp::mod_SetResp::{UpdatedInstanceFailure, UpdatedInstanceResult};
11use crate::usp::{Body, Request, Response, Set, SetResp};
12use crate::usp_errors;
13
14use anyhow::Result;
15
16#[derive(Clone)]
17pub struct UpdateObjectBuilder {
18    obj_path: String,
19    param_settings: Vec<(String, String, bool)>,
20}
21
22impl UpdateObjectBuilder {
23    #[must_use]
24    pub const fn new(obj_path: String) -> Self {
25        Self {
26            obj_path,
27            param_settings: vec![],
28        }
29    }
30
31    #[must_use]
32    pub fn with_param_settings(mut self, param_settings: Vec<(String, String, bool)>) -> Self {
33        self.param_settings = param_settings;
34        self
35    }
36
37    pub fn build(self) -> Result<UpdateObject> {
38        let param_settings = self
39            .param_settings
40            .into_iter()
41            .map(|(n, v, r)| UpdateParamSetting {
42                param: n,
43                value: v,
44                required: r,
45            })
46            .collect();
47
48        Ok(UpdateObject {
49            obj_path: self.obj_path,
50            param_settings,
51        })
52    }
53}
54
55#[derive(Clone)]
56pub struct SetBuilder {
57    allow_partial: bool,
58    update_objs: Vec<UpdateObjectBuilder>,
59}
60
61impl SetBuilder {
62    #[must_use]
63    pub const fn new() -> Self {
64        Self {
65            allow_partial: false,
66            update_objs: vec![],
67        }
68    }
69
70    #[must_use]
71    pub const fn with_allow_partial(mut self, allow_partial: bool) -> Self {
72        self.allow_partial = allow_partial;
73        self
74    }
75
76    #[must_use]
77    pub fn with_update_objs(mut self, update_objs: Vec<UpdateObjectBuilder>) -> Self {
78        self.update_objs = update_objs;
79        self
80    }
81
82    pub fn build(self) -> Result<Body> {
83        use crate::usp::mod_Body::OneOfmsg_body::request;
84        use crate::usp::mod_Request::OneOfreq_type::set;
85
86        let update_objs = self
87            .update_objs
88            .into_iter()
89            .map(UpdateObjectBuilder::build)
90            .collect::<Result<Vec<_>>>()?;
91
92        Ok(Body {
93            msg_body: request({
94                Request {
95                    req_type: set({
96                        Set {
97                            allow_partial: self.allow_partial,
98                            update_objs,
99                        }
100                    }),
101                }
102            }),
103        })
104    }
105}
106
107#[derive(Clone)]
108pub struct SetRespParameterError {
109    param: String,
110    err_code: u32,
111    err_msg: String,
112}
113
114impl SetRespParameterError {
115    #[must_use]
116    pub fn new(param: String, err_code: u32, err_msg: Option<String>) -> Self {
117        Self {
118            param,
119            err_code,
120            err_msg: err_msg.unwrap_or_else(|| usp_errors::get_err_msg(err_code).to_string()),
121        }
122    }
123}
124
125#[derive(Clone)]
126pub struct SetOperationSuccessBuilder {
127    affected_path: String,
128    param_errs: Vec<SetRespParameterError>,
129    updated_params: HashMap<String, String>,
130}
131
132impl SetOperationSuccessBuilder {
133    #[must_use]
134    pub fn new(affected_path: String) -> Self {
135        Self {
136            affected_path,
137            param_errs: vec![],
138            updated_params: HashMap::new(),
139        }
140    }
141
142    #[must_use]
143    pub fn with_param_errs(mut self, param_errs: Vec<SetRespParameterError>) -> Self {
144        self.param_errs = param_errs;
145        self
146    }
147
148    #[must_use]
149    pub fn with_updated_params(mut self, updated_params: HashMap<String, String>) -> Self {
150        self.updated_params = updated_params;
151        self
152    }
153}
154
155#[derive(Clone)]
156pub struct UpdatedInstanceFailureBuilder {
157    affected_path: String,
158    param_errs: Vec<SetRespParameterError>,
159}
160
161impl UpdatedInstanceFailureBuilder {
162    #[must_use]
163    pub const fn new(affected_path: String) -> Self {
164        Self {
165            affected_path,
166            param_errs: vec![],
167        }
168    }
169
170    #[must_use]
171    pub fn with_param_errs(mut self, param_errs: Vec<SetRespParameterError>) -> Self {
172        self.param_errs = param_errs;
173        self
174    }
175}
176
177#[derive(Clone)]
178pub struct SetOperationFailureBuilder {
179    err_code: u32,
180    err_msg: String,
181    updated_inst_failures: Vec<UpdatedInstanceFailureBuilder>,
182}
183
184#[derive(Clone)]
185pub enum SetOperationStatus {
186    Failure(SetOperationFailureBuilder),
187    Success(Vec<SetOperationSuccessBuilder>),
188    None,
189}
190
191impl SetOperationStatus {
192    #[must_use]
193    pub const fn new() -> Self {
194        Self::None
195    }
196
197    #[must_use]
198    pub fn set_failure(
199        self,
200        err_code: u32,
201        err_msg: Option<String>,
202        updated_inst_failures: Vec<UpdatedInstanceFailureBuilder>,
203    ) -> Self {
204        Self::Failure(SetOperationFailureBuilder {
205            err_code,
206            err_msg: err_msg.unwrap_or_else(|| usp_errors::get_err_msg(err_code).to_string()),
207            updated_inst_failures,
208        })
209    }
210
211    #[must_use]
212    pub fn set_success(self, updated_inst_results: Vec<SetOperationSuccessBuilder>) -> Self {
213        Self::Success(updated_inst_results)
214    }
215
216    pub fn build(self) -> Result<OperationStatus> {
217        match self {
218            Self::Failure(f) => Ok(OperationStatus {
219                oper_status: oper_failure(OperationFailure {
220                    err_code: f.err_code,
221                    err_msg: f.err_msg,
222                    updated_inst_failures: f
223                        .updated_inst_failures
224                        .into_iter()
225                        .map(|e| UpdatedInstanceFailure {
226                            affected_path: e.affected_path,
227                            param_errs: e
228                                .param_errs
229                                .into_iter()
230                                .map(|p| ParameterError {
231                                    param: p.param,
232                                    err_code: p.err_code,
233                                    err_msg: if p.err_msg.is_empty() {
234                                        usp_errors::get_err_msg(p.err_code).into()
235                                    } else {
236                                        p.err_msg
237                                    },
238                                })
239                                .collect(),
240                        })
241                        .collect(),
242                }),
243            }),
244            Self::Success(s) => Ok(OperationStatus {
245                oper_status: oper_success(OperationSuccess {
246                    updated_inst_results: s
247                        .into_iter()
248                        .map(|s| UpdatedInstanceResult {
249                            affected_path: s.affected_path,
250                            param_errs: s
251                                .param_errs
252                                .into_iter()
253                                .map(|e| ParameterError {
254                                    param: e.param,
255                                    err_code: e.err_code,
256                                    err_msg: if e.err_msg.is_empty() {
257                                        usp_errors::get_err_msg(e.err_code).into()
258                                    } else {
259                                        e.err_msg
260                                    },
261                                })
262                                .collect(),
263                            updated_params: s.updated_params.into_iter().collect(),
264                        })
265                        .collect(),
266                }),
267            }),
268            Self::None => Err(anyhow::anyhow!("")),
269        }
270    }
271}
272
273#[derive(Clone)]
274pub struct UpdatedObjectResultsBuilder {
275    requested_path: String,
276    oper_status: SetOperationStatus,
277}
278
279impl UpdatedObjectResultsBuilder {
280    #[must_use]
281    pub const fn new(requested_path: String, oper_status: SetOperationStatus) -> Self {
282        Self {
283            requested_path,
284            oper_status,
285        }
286    }
287
288    pub fn build(self) -> Result<UpdatedObjectResult> {
289        Ok(UpdatedObjectResult {
290            requested_path: self.requested_path,
291            oper_status: Some(self.oper_status.build()?),
292        })
293    }
294}
295
296#[derive(Clone)]
297pub struct SetRespBuilder {
298    updated_obj_results: Vec<UpdatedObjectResultsBuilder>,
299}
300
301impl SetRespBuilder {
302    #[must_use]
303    pub const fn new() -> Self {
304        Self {
305            updated_obj_results: vec![],
306        }
307    }
308
309    #[must_use]
310    pub fn with_updated_obj_results(
311        mut self,
312        updated_obj_results: Vec<UpdatedObjectResultsBuilder>,
313    ) -> Self {
314        self.updated_obj_results = updated_obj_results;
315        self
316    }
317
318    pub fn build(self) -> Result<Body> {
319        use crate::usp::mod_Body::OneOfmsg_body::response;
320        use crate::usp::mod_Response::OneOfresp_type::set_resp;
321
322        let updated_obj_results = self
323            .updated_obj_results
324            .into_iter()
325            .map(UpdatedObjectResultsBuilder::build)
326            .collect::<Result<Vec<_>>>()?;
327
328        Ok(Body {
329            msg_body: response({
330                Response {
331                    resp_type: set_resp({
332                        SetResp {
333                            updated_obj_results,
334                        }
335                    }),
336                }
337            }),
338        })
339    }
340}