rusp_lib/usp_builder/
add.rs

1use std::collections::HashMap;
2
3use crate::usp::mod_Add::{CreateObject, CreateParamSetting};
4use crate::usp::mod_AddResp::{mod_CreatedObjectResult::OperationStatus, CreatedObjectResult};
5use crate::usp::{Add, AddResp, Body, Request, Response};
6use crate::usp_errors;
7
8use anyhow::Result;
9
10#[derive(Clone)]
11pub struct CreateObjectBuilder {
12    obj_path: String,
13    param_settings: Vec<(String, String, bool)>,
14}
15
16impl CreateObjectBuilder {
17    #[must_use]
18    pub const fn new(obj_path: String) -> Self {
19        Self {
20            obj_path,
21            param_settings: vec![],
22        }
23    }
24
25    #[must_use]
26    pub fn with_param_settings(mut self, param_settings: Vec<(String, String, bool)>) -> Self {
27        self.param_settings = param_settings;
28        self
29    }
30
31    pub fn build(self) -> Result<CreateObject> {
32        let param_settings = self
33            .param_settings
34            .into_iter()
35            .map(|(n, v, r)| CreateParamSetting {
36                param: n,
37                value: v,
38                required: r,
39            })
40            .collect();
41
42        Ok(CreateObject {
43            obj_path: self.obj_path,
44            param_settings,
45        })
46    }
47}
48
49#[derive(Clone)]
50pub struct AddBuilder {
51    allow_partial: bool,
52    create_objs: Vec<CreateObjectBuilder>,
53}
54
55impl AddBuilder {
56    #[must_use]
57    pub const fn new() -> Self {
58        Self {
59            allow_partial: false,
60            create_objs: vec![],
61        }
62    }
63
64    #[must_use]
65    pub const fn with_allow_partial(mut self, allow_partial: bool) -> Self {
66        self.allow_partial = allow_partial;
67        self
68    }
69
70    #[must_use]
71    pub fn with_create_objs(mut self, create_objs: Vec<CreateObjectBuilder>) -> Self {
72        self.create_objs = create_objs;
73        self
74    }
75
76    pub fn build(self) -> Result<Body> {
77        use crate::usp::mod_Body::OneOfmsg_body::request;
78        use crate::usp::mod_Request::OneOfreq_type::add;
79
80        let create_objs = self
81            .create_objs
82            .into_iter()
83            .map(CreateObjectBuilder::build)
84            .collect::<Result<Vec<_>>>()?;
85
86        Ok(Body {
87            msg_body: request({
88                Request {
89                    req_type: add({
90                        Add {
91                            allow_partial: self.allow_partial,
92                            create_objs,
93                        }
94                    }),
95                }
96            }),
97        })
98    }
99}
100
101#[derive(Clone)]
102pub struct AddRespParameterError {
103    pub param: String,
104    pub err_code: u32,
105    pub err_msg: String,
106}
107
108#[derive(Clone)]
109pub struct AddOperationSuccessBuilder {
110    pub instantiated_path: String,
111    pub param_errs: Vec<AddRespParameterError>,
112    pub unique_keys: HashMap<String, String>,
113}
114
115#[derive(Clone)]
116pub struct AddOperationFailureBuilder {
117    pub err_code: u32,
118    pub err_msg: String,
119}
120
121#[derive(Clone)]
122pub enum AddOperationStatus {
123    Failure(AddOperationFailureBuilder),
124    Success(AddOperationSuccessBuilder),
125    None,
126}
127
128impl AddOperationStatus {
129    #[must_use]
130    pub const fn new() -> Self {
131        Self::None
132    }
133
134    #[must_use]
135    pub fn set_failure(self, err_code: u32, err_msg: Option<String>) -> Self {
136        Self::Failure(AddOperationFailureBuilder {
137            err_code,
138            err_msg: err_msg.unwrap_or_else(|| usp_errors::get_err_msg(err_code).to_string()),
139        })
140    }
141
142    #[must_use]
143    pub fn set_success(
144        self,
145        instantiated_path: String,
146        param_errs: Vec<AddRespParameterError>,
147        unique_keys: HashMap<String, String>,
148    ) -> Self {
149        Self::Success(AddOperationSuccessBuilder {
150            instantiated_path,
151            param_errs,
152            unique_keys,
153        })
154    }
155
156    pub fn build(self) -> Result<OperationStatus> {
157        use crate::usp::mod_AddResp::mod_CreatedObjectResult::mod_OperationStatus::{
158            OneOfoper_status::{oper_failure, oper_success},
159            OperationFailure, OperationSuccess,
160        };
161        use crate::usp::mod_AddResp::ParameterError;
162        match self {
163            Self::Failure(f) => Ok(OperationStatus {
164                oper_status: oper_failure(OperationFailure {
165                    err_code: f.err_code,
166                    err_msg: f.err_msg,
167                }),
168            }),
169            Self::Success(s) => Ok(OperationStatus {
170                oper_status: oper_success(OperationSuccess {
171                    instantiated_path: s.instantiated_path,
172                    param_errs: s
173                        .param_errs
174                        .into_iter()
175                        .map(|e| ParameterError {
176                            param: e.param,
177                            err_code: e.err_code,
178                            err_msg: if e.err_msg.is_empty() {
179                                usp_errors::get_err_msg(e.err_code).into()
180                            } else {
181                                e.err_msg
182                            },
183                        })
184                        .collect(),
185                    unique_keys: s.unique_keys.into_iter().collect(),
186                }),
187            }),
188            Self::None => Err(anyhow::anyhow!("")),
189        }
190    }
191}
192
193#[derive(Clone)]
194pub struct CreatedObjectResultsBuilder {
195    requested_path: String,
196    oper_status: AddOperationStatus,
197}
198
199impl CreatedObjectResultsBuilder {
200    #[must_use]
201    pub const fn new(requested_path: String, oper_status: AddOperationStatus) -> Self {
202        Self {
203            requested_path,
204            oper_status,
205        }
206    }
207
208    pub fn build(self) -> Result<CreatedObjectResult> {
209        Ok(CreatedObjectResult {
210            requested_path: self.requested_path,
211            oper_status: Some(self.oper_status.build()?),
212        })
213    }
214}
215
216#[derive(Clone)]
217pub struct AddRespBuilder {
218    created_obj_results: Vec<CreatedObjectResultsBuilder>,
219}
220
221impl AddRespBuilder {
222    #[must_use]
223    pub const fn new() -> Self {
224        Self {
225            created_obj_results: vec![],
226        }
227    }
228
229    #[must_use]
230    pub fn with_created_obj_results(
231        mut self,
232        created_obj_results: Vec<CreatedObjectResultsBuilder>,
233    ) -> Self {
234        self.created_obj_results = created_obj_results;
235        self
236    }
237
238    pub fn build(self) -> Result<Body> {
239        use crate::usp::mod_Body::OneOfmsg_body::response;
240        use crate::usp::mod_Response::OneOfresp_type::add_resp;
241
242        let created_obj_results = self
243            .created_obj_results
244            .into_iter()
245            .map(CreatedObjectResultsBuilder::build)
246            .collect::<Result<Vec<_>>>()?;
247
248        Ok(Body {
249            msg_body: response({
250                Response {
251                    resp_type: add_resp({
252                        AddResp {
253                            created_obj_results,
254                        }
255                    }),
256                }
257            }),
258        })
259    }
260}