http_path_params/path_params/
impl_id.rs1use alloc::string::{String, ToString as _};
2
3use crate::{Id, PathParams};
4
5use super::SetError;
6
7impl PathParams for Id {
8 fn size(&self) -> usize {
9 1
10 }
11 fn get(&self, index: usize) -> (Option<&'static str>, String) {
12 match index {
13 0 => (None, self.0.to_string()),
14 _ => panic!(),
15 }
16 }
17 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
18 match index {
19 0 => {
20 self.0 = value
21 .parse::<usize>()
22 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
23 Ok(())
24 }
25 _ => panic!(),
26 }
27 }
28 fn verify(&self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
29 match index {
30 0 => {
31 let _ = value
32 .parse::<usize>()
33 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
34 Ok(())
35 }
36 _ => panic!(),
37 }
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 use alloc::vec;
46
47 use crate::{PathParam, PathParamsInfo};
48
49 #[test]
50 fn test_get_and_set() {
51 let mut path_params: Id = Default::default();
53 path_params
54 .set_from_info(&PathParamsInfo(vec![(None, "1".to_string())]))
55 .unwrap();
56 assert_eq!(path_params, Id(1));
57 assert_eq!(path_params.info(), vec![(None, "1".to_string())].into());
58 assert!(path_params.verify(0, &(None, "1")).is_ok());
59
60 let mut path_params: PathParam<Id> = Default::default();
62 path_params
63 .set_from_info(&PathParamsInfo(vec![(None, "1".to_string())]))
64 .unwrap();
65 assert_eq!(path_params, PathParam(Id(1)));
66 assert_eq!(path_params.info(), vec![(None, "1".to_string())].into());
67 }
68}