rusp_lib/usp_builder/
getinstances.rs

1use crate::usp::mod_Body::OneOfmsg_body::{request, response};
2use crate::usp::mod_GetInstancesResp::{CurrInstance, RequestedPathResult};
3use crate::usp::mod_Request::OneOfreq_type::get_instances;
4use crate::usp::mod_Response::OneOfresp_type::get_instances_resp;
5use crate::usp::{Body, GetInstances, GetInstancesResp, Request, Response};
6
7use crate::usp_errors;
8
9use anyhow::Result;
10
11#[derive(Clone)]
12pub struct GetInstancesBuilder {
13    obj_paths: Vec<String>,
14    first_level_only: bool,
15}
16
17impl GetInstancesBuilder {
18    #[must_use]
19    pub const fn new() -> Self {
20        Self {
21            obj_paths: vec![],
22            first_level_only: false,
23        }
24    }
25
26    #[must_use]
27    pub const fn with_first_level_only(mut self, first_level_only: bool) -> Self {
28        self.first_level_only = first_level_only;
29        self
30    }
31
32    #[must_use]
33    pub fn with_obj_paths(mut self, obj_paths: Vec<String>) -> Self {
34        self.obj_paths = obj_paths;
35        self
36    }
37
38    pub fn build(self) -> Result<Body> {
39        Ok(Body {
40            msg_body: request({
41                Request {
42                    req_type: get_instances({
43                        GetInstances {
44                            obj_paths: self.obj_paths,
45                            first_level_only: self.first_level_only,
46                        }
47                    }),
48                }
49            }),
50        })
51    }
52}
53
54#[derive(Clone)]
55pub struct CurrInstanceBuilder {
56    instantiated_obj_path: String,
57    unique_keys: Vec<(String, String)>,
58}
59
60impl CurrInstanceBuilder {
61    #[must_use]
62    pub const fn new(instantiated_obj_path: String) -> Self {
63        Self {
64            instantiated_obj_path,
65            unique_keys: vec![],
66        }
67    }
68
69    #[must_use]
70    pub fn with_unique_keys(mut self, unique_keys: Vec<(String, String)>) -> Self {
71        self.unique_keys = unique_keys;
72        self
73    }
74
75    pub fn build(self) -> Result<CurrInstance> {
76        let unique_keys = self.unique_keys.into_iter().collect();
77
78        Ok(CurrInstance {
79            instantiated_obj_path: self.instantiated_obj_path,
80            unique_keys,
81        })
82    }
83}
84
85#[derive(Clone)]
86pub struct ReqPathResultBuilder {
87    pub requested_path: String,
88    pub err_code: u32,
89    pub err_msg: Option<String>,
90    pub curr_insts: Vec<CurrInstanceBuilder>,
91}
92
93impl ReqPathResultBuilder {
94    #[must_use]
95    pub const fn new(requested_path: String) -> Self {
96        Self {
97            requested_path,
98            err_code: 0,
99            err_msg: None,
100            curr_insts: vec![],
101        }
102    }
103
104    #[must_use]
105    pub fn set_err(mut self, err_code: u32, err_msg: Option<String>) -> Self {
106        self.err_code = err_code;
107        self.err_msg = err_msg;
108        self
109    }
110
111    #[must_use]
112    pub fn with_curr_insts(mut self, curr_insts: Vec<CurrInstanceBuilder>) -> Self {
113        self.curr_insts = curr_insts;
114        self
115    }
116
117    pub fn build(self) -> Result<RequestedPathResult> {
118        let err_msg = self
119            .err_msg
120            .clone()
121            .unwrap_or_else(|| usp_errors::get_err_msg(self.err_code).to_string());
122
123        let curr_insts = self
124            .curr_insts
125            .into_iter()
126            .map(CurrInstanceBuilder::build)
127            .collect::<Result<Vec<_>>>()?;
128
129        Ok(RequestedPathResult {
130            requested_path: self.requested_path,
131            err_code: self.err_code,
132            err_msg,
133            curr_insts,
134        })
135    }
136}
137
138#[derive(Clone)]
139pub struct GetInstancesRespBuilder {
140    req_path_results: Vec<ReqPathResultBuilder>,
141}
142
143impl GetInstancesRespBuilder {
144    #[must_use]
145    pub const fn new() -> Self {
146        Self {
147            req_path_results: vec![],
148        }
149    }
150
151    #[must_use]
152    pub fn with_req_path_results(mut self, req_path_results: Vec<ReqPathResultBuilder>) -> Self {
153        self.req_path_results = req_path_results;
154        self
155    }
156
157    pub fn build(self) -> Result<Body> {
158        let req_path_results = self
159            .req_path_results
160            .into_iter()
161            .map(ReqPathResultBuilder::build)
162            .collect::<Result<Vec<_>>>()?;
163
164        Ok(Body {
165            msg_body: response({
166                Response {
167                    resp_type: get_instances_resp(GetInstancesResp { req_path_results }),
168                }
169            }),
170        })
171    }
172}