rusp_lib/usp_builder/
getsupporteddm.rs

1use crate::usp::mod_Body::OneOfmsg_body::{request, response};
2use crate::usp::mod_GetSupportedDMResp::SupportedUniqueKeySet;
3use crate::usp::mod_GetSupportedDMResp::{
4    CmdType,
5    ObjAccessType::{self, OBJ_ADD_ONLY},
6    ParamAccessType, ParamValueType, RequestedObjectResult, SupportedCommandResult,
7    SupportedEventResult, SupportedObjectResult, SupportedParamResult, ValueChangeType,
8};
9use crate::usp::mod_Request::OneOfreq_type::get_supported_dm;
10use crate::usp::mod_Response::OneOfresp_type::get_supported_dm_resp;
11use crate::usp::{Body, GetSupportedDM, GetSupportedDMResp, Request, Response};
12use crate::usp_errors;
13
14use anyhow::Result;
15
16#[derive(Clone)]
17pub struct GetSupportedDMBuilder {
18    obj_paths: Vec<String>,
19    first_level_only: bool,
20    return_commands: bool,
21    return_events: bool,
22    return_params: bool,
23    return_unique_key_sets: bool,
24}
25
26impl GetSupportedDMBuilder {
27    #[must_use]
28    pub const fn new() -> Self {
29        Self {
30            obj_paths: vec![],
31            first_level_only: false,
32            return_commands: true,
33            return_events: true,
34            return_params: true,
35            return_unique_key_sets: true,
36        }
37    }
38
39    #[must_use]
40    pub fn with_obj_paths(mut self, obj_paths: Vec<String>) -> Self {
41        self.obj_paths = obj_paths;
42        self
43    }
44
45    #[must_use]
46    pub const fn with_first_level_only(mut self, first_level_only: bool) -> Self {
47        self.first_level_only = first_level_only;
48        self
49    }
50
51    #[must_use]
52    pub const fn with_return_commands(mut self, return_commands: bool) -> Self {
53        self.return_commands = return_commands;
54        self
55    }
56
57    #[must_use]
58    pub const fn with_return_events(mut self, return_events: bool) -> Self {
59        self.return_events = return_events;
60        self
61    }
62
63    #[must_use]
64    pub const fn with_return_params(mut self, return_params: bool) -> Self {
65        self.return_params = return_params;
66        self
67    }
68
69    #[must_use]
70    pub const fn with_return_unique_key_sets(mut self, return_unique_key_sets: bool) -> Self {
71        self.return_unique_key_sets = return_unique_key_sets;
72        self
73    }
74
75    pub fn build(self) -> Result<Body> {
76        Ok(Body {
77            msg_body: request({
78                Request {
79                    req_type: get_supported_dm({
80                        GetSupportedDM {
81                            obj_paths: self.obj_paths,
82                            first_level_only: self.first_level_only,
83                            return_commands: self.return_commands,
84                            return_events: self.return_events,
85                            return_params: self.return_params,
86                            return_unique_key_sets: self.return_unique_key_sets,
87                        }
88                    }),
89                }
90            }),
91        })
92    }
93}
94
95#[derive(Clone)]
96pub struct GSDMCommandResult {
97    command_name: String,
98    input_arg_names: Vec<String>,
99    output_arg_names: Vec<String>,
100    command_type: CmdType,
101}
102
103impl GSDMCommandResult {
104    #[must_use]
105    pub const fn new(command_name: String) -> Self {
106        Self {
107            command_name,
108            input_arg_names: vec![],
109            output_arg_names: vec![],
110            command_type: CmdType::CMD_UNKNOWN,
111        }
112    }
113
114    #[must_use]
115    pub fn with_input_arg_names(mut self, input_arg_names: Vec<String>) -> Self {
116        self.input_arg_names = input_arg_names;
117        self
118    }
119
120    #[must_use]
121    pub fn with_output_arg_names(mut self, output_arg_names: Vec<String>) -> Self {
122        self.output_arg_names = output_arg_names;
123        self
124    }
125
126    #[must_use]
127    pub const fn set_sync(mut self) -> Self {
128        self.command_type = CmdType::CMD_SYNC;
129        self
130    }
131
132    #[must_use]
133    pub const fn set_async(mut self) -> Self {
134        self.command_type = CmdType::CMD_ASYNC;
135        self
136    }
137
138    pub fn build(self) -> Result<SupportedCommandResult> {
139        if matches!(self.command_type, CmdType::CMD_UNKNOWN) {
140            anyhow::bail!(
141                "Cannot build a Supported Command Result without a specified command type"
142            );
143        }
144        Ok(SupportedCommandResult {
145            command_name: self.command_name,
146            input_arg_names: self.input_arg_names,
147            output_arg_names: self.output_arg_names,
148            command_type: self.command_type,
149        })
150    }
151}
152
153#[derive(Clone)]
154pub struct GSDMEventResult {
155    event_name: String,
156    arg_names: Vec<String>,
157}
158
159impl GSDMEventResult {
160    #[must_use]
161    pub const fn new(event_name: String) -> Self {
162        Self {
163            event_name,
164            arg_names: vec![],
165        }
166    }
167
168    #[must_use]
169    pub fn with_arg_names(mut self, arg_names: Vec<String>) -> Self {
170        self.arg_names = arg_names;
171        self
172    }
173
174    pub fn build(self) -> Result<SupportedEventResult> {
175        Ok(SupportedEventResult {
176            event_name: self.event_name,
177            arg_names: self.arg_names,
178        })
179    }
180}
181
182#[derive(Clone)]
183pub struct GSDMParamResult {
184    param_name: String,
185    access: ParamAccessType,
186    value_type: ParamValueType,
187    value_change: ValueChangeType,
188}
189
190impl GSDMParamResult {
191    #[must_use]
192    pub const fn new(param_name: String) -> Self {
193        Self {
194            param_name,
195            access: ParamAccessType::PARAM_READ_ONLY,
196            value_type: ParamValueType::PARAM_UNKNOWN,
197            value_change: ValueChangeType::VALUE_CHANGE_UNKNOWN,
198        }
199    }
200
201    #[must_use]
202    pub const fn set_access_read_only(mut self) -> Self {
203        self.access = ParamAccessType::PARAM_READ_ONLY;
204        self
205    }
206
207    #[must_use]
208    pub const fn set_access_write_only(mut self) -> Self {
209        self.access = ParamAccessType::PARAM_WRITE_ONLY;
210        self
211    }
212
213    #[must_use]
214    pub const fn set_access_read_write(mut self) -> Self {
215        self.access = ParamAccessType::PARAM_READ_WRITE;
216        self
217    }
218
219    #[must_use]
220    pub const fn set_type_int(mut self) -> Self {
221        self.value_type = ParamValueType::PARAM_INT;
222        self
223    }
224
225    #[must_use]
226    pub const fn set_type_unsigned_int(mut self) -> Self {
227        self.value_type = ParamValueType::PARAM_UNSIGNED_INT;
228        self
229    }
230
231    #[must_use]
232    pub const fn set_type_long(mut self) -> Self {
233        self.value_type = ParamValueType::PARAM_LONG;
234        self
235    }
236
237    #[must_use]
238    pub const fn set_type_unsigned_long(mut self) -> Self {
239        self.value_type = ParamValueType::PARAM_UNSIGNED_LONG;
240        self
241    }
242
243    #[must_use]
244    pub const fn set_type_string(mut self) -> Self {
245        self.value_type = ParamValueType::PARAM_STRING;
246        self
247    }
248
249    #[must_use]
250    pub const fn set_type_base64(mut self) -> Self {
251        self.value_type = ParamValueType::PARAM_BASE_64;
252        self
253    }
254
255    #[must_use]
256    pub const fn set_type_hexbinary(mut self) -> Self {
257        self.value_type = ParamValueType::PARAM_HEX_BINARY;
258        self
259    }
260
261    #[must_use]
262    pub const fn set_type_datetime(mut self) -> Self {
263        self.value_type = ParamValueType::PARAM_DATE_TIME;
264        self
265    }
266
267    #[must_use]
268    pub const fn set_type_decimal(mut self) -> Self {
269        self.value_type = ParamValueType::PARAM_DECIMAL;
270        self
271    }
272
273    #[must_use]
274    pub const fn set_type_boolean(mut self) -> Self {
275        self.value_type = ParamValueType::PARAM_BOOLEAN;
276        self
277    }
278
279    #[must_use]
280    pub const fn set_value_change_allowed(mut self) -> Self {
281        self.value_change = ValueChangeType::VALUE_CHANGE_ALLOWED;
282        self
283    }
284
285    #[must_use]
286    pub const fn set_value_change_will_ignore(mut self) -> Self {
287        self.value_change = ValueChangeType::VALUE_CHANGE_WILL_IGNORE;
288        self
289    }
290
291    pub fn build(self) -> Result<SupportedParamResult> {
292        if matches!(self.value_type, ParamValueType::PARAM_UNKNOWN) {
293            anyhow::bail!("Cannot build a Supported Param Result without a specified value type");
294        }
295        Ok(SupportedParamResult {
296            param_name: self.param_name,
297            access: self.access,
298            value_type: self.value_type,
299            value_change: self.value_change,
300        })
301    }
302}
303
304#[derive(Clone)]
305pub struct GSDMSupportedObjectResultBuilder {
306    supported_obj_path: String,
307    access: ObjAccessType,
308    is_multi_instance: bool,
309    supported_commands: Vec<GSDMCommandResult>,
310    supported_events: Vec<GSDMEventResult>,
311    supported_params: Vec<GSDMParamResult>,
312    divergent_paths: Vec<String>,
313    unique_key_sets: Vec<Vec<String>>,
314}
315
316impl GSDMSupportedObjectResultBuilder {
317    #[must_use]
318    pub const fn new(supported_obj_path: String) -> Self {
319        Self {
320            supported_obj_path,
321            access: OBJ_ADD_ONLY,
322            is_multi_instance: false,
323            supported_commands: vec![],
324            supported_events: vec![],
325            supported_params: vec![],
326            divergent_paths: vec![],
327            unique_key_sets: vec![],
328        }
329    }
330
331    #[must_use]
332    pub const fn set_access_add_only(mut self) -> Self {
333        self.access = ObjAccessType::OBJ_ADD_ONLY;
334        self
335    }
336
337    #[must_use]
338    pub const fn set_access_delete_only(mut self) -> Self {
339        self.access = ObjAccessType::OBJ_DELETE_ONLY;
340        self
341    }
342
343    #[must_use]
344    pub const fn set_access_read_only(mut self) -> Self {
345        self.access = ObjAccessType::OBJ_READ_ONLY;
346        self
347    }
348
349    #[must_use]
350    pub const fn set_access_add_delete(mut self) -> Self {
351        self.access = ObjAccessType::OBJ_ADD_DELETE;
352        self
353    }
354
355    #[must_use]
356    pub const fn with_is_multi_instance(mut self, is_multi_instance: bool) -> Self {
357        self.is_multi_instance = is_multi_instance;
358        self
359    }
360
361    #[must_use]
362    pub fn with_supported_commands(mut self, supported_commands: Vec<GSDMCommandResult>) -> Self {
363        self.supported_commands = supported_commands;
364        self
365    }
366
367    #[must_use]
368    pub fn with_supported_events(mut self, supported_events: Vec<GSDMEventResult>) -> Self {
369        self.supported_events = supported_events;
370        self
371    }
372
373    #[must_use]
374    pub fn with_supported_params(mut self, supported_params: Vec<GSDMParamResult>) -> Self {
375        self.supported_params = supported_params;
376        self
377    }
378
379    #[must_use]
380    pub fn with_divergent_paths(mut self, divergent_paths: Vec<String>) -> Self {
381        self.divergent_paths = divergent_paths;
382        self
383    }
384
385    #[must_use]
386    pub fn with_unique_key_sets(mut self, unique_key_sets: Vec<Vec<String>>) -> Self {
387        self.unique_key_sets = unique_key_sets;
388        self
389    }
390
391    pub fn build(self) -> Result<SupportedObjectResult> {
392        let supported_commands = self
393            .supported_commands
394            .into_iter()
395            .map(GSDMCommandResult::build)
396            .collect::<Result<Vec<_>>>()?;
397
398        let supported_events = self
399            .supported_events
400            .into_iter()
401            .map(GSDMEventResult::build)
402            .collect::<Result<Vec<_>>>()?;
403
404        let supported_params = self
405            .supported_params
406            .into_iter()
407            .map(GSDMParamResult::build)
408            .collect::<Result<Vec<_>>>()?;
409
410        let unique_key_sets: Vec<SupportedUniqueKeySet> = self
411            .unique_key_sets
412            .into_iter()
413            .map(|i| SupportedUniqueKeySet { key_names: i })
414            .collect();
415
416        Ok(SupportedObjectResult {
417            supported_obj_path: self.supported_obj_path,
418            access: self.access,
419            is_multi_instance: self.is_multi_instance,
420            supported_commands,
421            supported_events,
422            supported_params,
423            divergent_paths: self.divergent_paths,
424            unique_key_sets,
425        })
426    }
427}
428
429#[derive(Clone)]
430pub struct GSDMReqObjectResultBuilder {
431    req_obj_path: String,
432    err_code: u32,
433    err_msg: Option<String>,
434    data_model_inst_uri: String,
435    supported_objs: Vec<GSDMSupportedObjectResultBuilder>,
436}
437
438impl GSDMReqObjectResultBuilder {
439    #[must_use]
440    pub const fn new(req_obj_path: String) -> Self {
441        Self {
442            req_obj_path,
443            err_code: 0,
444            err_msg: None,
445            data_model_inst_uri: String::new(),
446            supported_objs: vec![],
447        }
448    }
449
450    #[must_use]
451    pub fn set_err(mut self, err_code: u32, err_msg: Option<String>) -> Self {
452        self.err_code = err_code;
453        self.err_msg =
454            Some(err_msg.unwrap_or_else(|| usp_errors::get_err_msg(err_code).to_string()));
455        self
456    }
457
458    #[must_use]
459    pub fn with_data_model_inst_uri(mut self, data_model_inst_uri: String) -> Self {
460        self.data_model_inst_uri = data_model_inst_uri;
461        self
462    }
463
464    #[must_use]
465    pub fn with_supported_objs(
466        mut self,
467        supported_objs: Vec<GSDMSupportedObjectResultBuilder>,
468    ) -> Self {
469        self.supported_objs = supported_objs;
470        self
471    }
472
473    pub fn build(self) -> Result<RequestedObjectResult> {
474        let err_msg = self
475            .err_msg
476            .clone()
477            .unwrap_or_else(|| usp_errors::get_err_msg(self.err_code).to_string());
478
479        let supported_objs = self
480            .supported_objs
481            .into_iter()
482            .map(GSDMSupportedObjectResultBuilder::build)
483            .collect::<Result<Vec<_>>>()?;
484
485        Ok(RequestedObjectResult {
486            req_obj_path: self.req_obj_path,
487            err_code: self.err_code,
488            err_msg,
489            data_model_inst_uri: self.data_model_inst_uri,
490            supported_objs,
491        })
492    }
493}
494
495#[derive(Clone)]
496pub struct GetSupportedDMRespBuilder {
497    req_obj_results: Vec<GSDMReqObjectResultBuilder>,
498}
499
500impl GetSupportedDMRespBuilder {
501    #[must_use]
502    pub const fn new() -> Self {
503        Self {
504            req_obj_results: vec![],
505        }
506    }
507
508    #[must_use]
509    pub fn with_req_obj_results(
510        mut self,
511        req_obj_results: Vec<GSDMReqObjectResultBuilder>,
512    ) -> Self {
513        self.req_obj_results = req_obj_results;
514        self
515    }
516
517    pub fn build(self) -> Result<Body> {
518        let req_obj_results = self
519            .req_obj_results
520            .into_iter()
521            .map(GSDMReqObjectResultBuilder::build)
522            .collect::<Result<Vec<_>>>()?;
523
524        Ok(Body {
525            msg_body: response({
526                Response {
527                    resp_type: get_supported_dm_resp(GetSupportedDMResp { req_obj_results }),
528                }
529            }),
530        })
531    }
532}