rusp_lib/usp_builder/
get.rs1use crate::usp::mod_Body::OneOfmsg_body::{request, response};
2use crate::usp::mod_GetResp::{RequestedPathResult, ResolvedPathResult};
3use crate::usp::mod_Request::OneOfreq_type::get;
4use crate::usp::mod_Response::OneOfresp_type::get_resp;
5use crate::usp::{Body, Get, GetResp, Request, Response};
6
7use crate::usp_errors;
8
9use anyhow::Result;
10
11#[derive(Clone)]
12pub struct GetBuilder {
13 max_depth: u32,
14 params: Vec<String>,
15}
16
17impl GetBuilder {
18 #[must_use]
19 pub const fn new() -> Self {
20 Self {
21 max_depth: 0,
22 params: vec![],
23 }
24 }
25
26 #[must_use]
27 pub const fn with_max_depth(mut self, max_depth: u32) -> Self {
28 self.max_depth = max_depth;
29 self
30 }
31
32 #[must_use]
33 pub fn with_params(mut self, params: Vec<String>) -> Self {
34 self.params = params;
35 self
36 }
37
38 pub fn build(self) -> Result<Body> {
39 Ok(Body {
40 msg_body: request({
41 Request {
42 req_type: get({
43 Get {
44 max_depth: self.max_depth,
45 param_paths: self.params,
46 }
47 }),
48 }
49 }),
50 })
51 }
52}
53
54#[derive(Clone)]
55pub struct ResolvedPathResultBuilder {
56 pub resolved_path: String,
57 pub result_params: Vec<(String, String)>,
58}
59
60impl ResolvedPathResultBuilder {
61 #[must_use]
62 pub const fn new(resolved_path: String) -> Self {
63 Self {
64 resolved_path,
65 result_params: vec![],
66 }
67 }
68
69 #[must_use]
70 pub fn with_result_params(mut self, result_params: Vec<(String, String)>) -> Self {
71 self.result_params = result_params;
72 self
73 }
74
75 pub fn build(self) -> Result<ResolvedPathResult> {
76 let result_params = self.result_params.into_iter().collect();
77
78 Ok(ResolvedPathResult {
79 resolved_path: self.resolved_path,
80 result_params,
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 resolved_path_results: Vec<ResolvedPathResultBuilder>,
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 resolved_path_results: 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_res_path_results(
113 mut self,
114 resolved_path_results: Vec<ResolvedPathResultBuilder>,
115 ) -> Self {
116 self.resolved_path_results = resolved_path_results;
117 self
118 }
119
120 pub fn build(self) -> Result<RequestedPathResult> {
121 let err_msg = self
122 .err_msg
123 .clone()
124 .unwrap_or_else(|| usp_errors::get_err_msg(self.err_code).to_string());
125
126 let resolved_path_results = self
127 .resolved_path_results
128 .into_iter()
129 .map(ResolvedPathResultBuilder::build)
130 .collect::<Result<Vec<_>>>()?;
131
132 Ok(RequestedPathResult {
133 requested_path: self.requested_path,
134 err_code: self.err_code,
135 err_msg,
136 resolved_path_results,
137 })
138 }
139}
140
141#[derive(Clone)]
142pub struct GetRespBuilder {
143 req_path_results: Vec<ReqPathResultBuilder>,
144}
145
146impl GetRespBuilder {
147 #[must_use]
148 pub const fn new() -> Self {
149 Self {
150 req_path_results: vec![],
151 }
152 }
153
154 #[must_use]
155 pub fn with_req_path_results(mut self, req_path_results: Vec<ReqPathResultBuilder>) -> Self {
156 self.req_path_results = req_path_results;
157 self
158 }
159
160 pub fn build(self) -> Result<Body> {
161 let req_path_results = self
162 .req_path_results
163 .into_iter()
164 .map(ReqPathResultBuilder::build)
165 .collect::<Result<Vec<_>>>()?;
166
167 Ok(Body {
168 msg_body: response({
169 Response {
170 resp_type: get_resp(GetResp { req_path_results }),
171 }
172 }),
173 })
174 }
175}