1use alloc::string::{String, ToString as _};
2
3use crate::PathParams;
4
5use super::SetError;
6
7impl PathParams for () {
9 fn size(&self) -> usize {
10 0
11 }
12 fn get(&self, _index: usize) -> (Option<&'static str>, String) {
13 panic!()
14 }
15 fn set(&mut self, _index: usize, (_, _value): &(Option<&str>, &str)) -> Result<(), SetError> {
16 panic!()
17 }
18 fn verify(&self, _index: usize, (_, _value): &(Option<&str>, &str)) -> Result<(), SetError> {
19 panic!()
20 }
21}
22
23impl PathParams for usize {
25 fn size(&self) -> usize {
26 1
27 }
28 fn get(&self, index: usize) -> (Option<&'static str>, String) {
29 match index {
30 0 => (None, self.to_string()),
31 _ => panic!(),
32 }
33 }
34 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
35 match index {
36 0 => {
37 *self = value
38 .parse::<usize>()
39 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
40 Ok(())
41 }
42 _ => panic!(),
43 }
44 }
45 fn verify(&self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
46 match index {
47 0 => {
48 let _ = value
49 .parse::<usize>()
50 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
51 Ok(())
52 }
53 _ => panic!(),
54 }
55 }
56}
57
58impl PathParams for String {
59 fn size(&self) -> usize {
60 1
61 }
62 fn get(&self, index: usize) -> (Option<&'static str>, String) {
63 match index {
64 0 => (None, self.to_string()),
65 _ => panic!(),
66 }
67 }
68 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
69 match index {
70 0 => {
71 *self = value.to_string();
72 Ok(())
73 }
74 _ => panic!(),
75 }
76 }
77 fn verify(&self, index: usize, (_, _value): &(Option<&str>, &str)) -> Result<(), SetError> {
78 match index {
79 0 => Ok(()),
80 _ => panic!(),
81 }
82 }
83}
84
85impl PathParams for (usize, usize) {
87 fn size(&self) -> usize {
88 2
89 }
90 fn get(&self, index: usize) -> (Option<&'static str>, String) {
91 match index {
92 0 => (None, self.0.to_string()),
93 1 => (None, self.1.to_string()),
94 _ => panic!(),
95 }
96 }
97 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
98 match index {
99 0 => {
100 self.0 = value
101 .parse::<usize>()
102 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
103 Ok(())
104 }
105 1 => {
106 self.1 = value
107 .parse::<usize>()
108 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
109 Ok(())
110 }
111 _ => panic!(),
112 }
113 }
114 fn verify(&self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
115 match index {
116 0 => {
117 let _ = value
118 .parse::<usize>()
119 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
120 Ok(())
121 }
122 1 => {
123 let _ = value
124 .parse::<usize>()
125 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
126 Ok(())
127 }
128 _ => panic!(),
129 }
130 }
131}
132
133impl PathParams for (String, String) {
134 fn size(&self) -> usize {
135 2
136 }
137 fn get(&self, index: usize) -> (Option<&'static str>, String) {
138 match index {
139 0 => (None, self.0.to_string()),
140 1 => (None, self.1.to_string()),
141 _ => panic!(),
142 }
143 }
144 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
145 match index {
146 0 => {
147 self.0 = value.to_string();
148 Ok(())
149 }
150 1 => {
151 self.1 = value.to_string();
152 Ok(())
153 }
154 _ => panic!(),
155 }
156 }
157 fn verify(&self, index: usize, (_, _value): &(Option<&str>, &str)) -> Result<(), SetError> {
158 match index {
159 0 => Ok(()),
160 1 => Ok(()),
161 _ => panic!(),
162 }
163 }
164}
165
166impl PathParams for (usize, String) {
167 fn size(&self) -> usize {
168 2
169 }
170 fn get(&self, index: usize) -> (Option<&'static str>, String) {
171 match index {
172 0 => (None, self.0.to_string()),
173 1 => (None, self.1.to_string()),
174 _ => panic!(),
175 }
176 }
177 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
178 match index {
179 0 => {
180 self.0 = value
181 .parse::<usize>()
182 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
183 Ok(())
184 }
185 1 => {
186 self.1 = value.to_string();
187 Ok(())
188 }
189 _ => panic!(),
190 }
191 }
192 fn verify(&self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
193 match index {
194 0 => {
195 let _ = value
196 .parse::<usize>()
197 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
198 Ok(())
199 }
200 1 => Ok(()),
201 _ => panic!(),
202 }
203 }
204}
205
206impl PathParams for (String, usize) {
207 fn size(&self) -> usize {
208 2
209 }
210 fn get(&self, index: usize) -> (Option<&'static str>, String) {
211 match index {
212 0 => (None, self.0.to_string()),
213 1 => (None, self.1.to_string()),
214 _ => panic!(),
215 }
216 }
217 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
218 match index {
219 0 => {
220 self.0 = value.to_string();
221 Ok(())
222 }
223 1 => {
224 self.1 = value
225 .parse::<usize>()
226 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
227 Ok(())
228 }
229 _ => panic!(),
230 }
231 }
232 fn verify(&self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
233 match index {
234 0 => Ok(()),
235 1 => {
236 let _ = value
237 .parse::<usize>()
238 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
239 Ok(())
240 }
241 _ => panic!(),
242 }
243 }
244}
245
246#[cfg(test)]
247mod tests {
248 use super::*;
249
250 use alloc::vec;
251
252 use crate::PathParamsInfo;
253
254 #[test]
255 fn test_get_and_set() {
256 let mut path_params: usize = Default::default();
258 path_params
259 .set_from_info(&PathParamsInfo(vec![(None, "1".to_string())]))
260 .unwrap();
261 assert_eq!(path_params, 1);
262 assert_eq!(path_params.info(), vec![(None, "1".to_string())].into());
263 assert!(path_params.verify(0, &(None, "1")).is_ok());
264
265 let mut path_params: String = Default::default();
266 path_params
267 .set_from_info(&PathParamsInfo(vec![(None, "foo".to_string())]))
268 .unwrap();
269 assert_eq!(path_params, "foo");
270 assert_eq!(path_params.info(), vec![(None, "foo".to_string())].into());
271 assert!(path_params.verify(0, &(None, "foo")).is_ok());
272
273 let mut path_params: (usize, usize) = Default::default();
275 path_params
276 .set_from_info(&PathParamsInfo(vec![
277 (None, "1".to_string()),
278 (None, "2".to_string()),
279 ]))
280 .unwrap();
281 assert_eq!(path_params, (1, 2));
282 assert_eq!(
283 path_params.info(),
284 vec![(None, "1".to_string()), (None, "2".to_string())].into()
285 );
286 assert!(path_params.verify(0, &(None, "1")).is_ok());
287 assert!(path_params.verify(1, &(None, "2")).is_ok());
288
289 let mut path_params: (String, String) = Default::default();
291 path_params
292 .set_from_info(&PathParamsInfo(vec![
293 (None, "foo".to_string()),
294 (None, "bar".to_string()),
295 ]))
296 .unwrap();
297 assert_eq!(path_params, ("foo".to_string(), "bar".to_string()));
298 assert_eq!(
299 path_params.info(),
300 vec![(None, "foo".to_string()), (None, "bar".to_string())].into()
301 );
302 assert!(path_params.verify(0, &(None, "foo")).is_ok());
303 assert!(path_params.verify(1, &(None, "bar")).is_ok());
304
305 let mut path_params: (usize, String) = Default::default();
307 path_params
308 .set_from_info(&PathParamsInfo(vec![
309 (None, "1".to_string()),
310 (None, "foo".to_string()),
311 ]))
312 .unwrap();
313 assert_eq!(path_params, (1, "foo".to_string()));
314 assert_eq!(
315 path_params.info(),
316 vec![(None, "1".to_string()), (None, "foo".to_string())].into()
317 );
318 assert!(path_params.verify(0, &(None, "1")).is_ok());
319 assert!(path_params.verify(1, &(None, "foo")).is_ok());
320
321 let mut path_params: (String, usize) = Default::default();
323 path_params
324 .set_from_info(&PathParamsInfo(vec![
325 (None, "foo".to_string()),
326 (None, "1".to_string()),
327 ]))
328 .unwrap();
329 assert_eq!(path_params, ("foo".to_string(), 1));
330 assert_eq!(
331 path_params.info(),
332 vec![(None, "foo".to_string()), (None, "1".to_string())].into()
333 );
334 assert!(path_params.verify(0, &(None, "foo")).is_ok());
335 assert!(path_params.verify(1, &(None, "1")).is_ok());
336 }
337}