http_path_params/path_params/
impl_username.rs1use alloc::string::{String, ToString as _};
2
3use crate::{PathParams, Username};
4
5use super::SetError;
6
7impl PathParams for Username {
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.to_string();
21 Ok(())
22 }
23 _ => panic!(),
24 }
25 }
26 fn verify(&self, index: usize, (_, _value): &(Option<&str>, &str)) -> Result<(), SetError> {
27 match index {
28 0 => Ok(()),
29 _ => panic!(),
30 }
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 use alloc::vec;
39
40 use crate::{PathParam, PathParamsInfo};
41
42 #[test]
43 fn test_get_and_set() {
44 let mut path_params: Username = Default::default();
46 path_params
47 .set_from_info(&PathParamsInfo(vec![(None, "foo".to_string())]))
48 .unwrap();
49 assert_eq!(path_params, Username("foo".to_string()));
50 assert_eq!(path_params.info(), vec![(None, "foo".to_string())].into());
51 assert!(path_params.verify(0, &(None, "foo")).is_ok());
52
53 let mut path_params: PathParam<Username> = Default::default();
55 path_params
56 .set_from_info(&PathParamsInfo(vec![(None, "foo".to_string())]))
57 .unwrap();
58 assert_eq!(path_params, PathParam(Username("foo".to_string())));
59 assert_eq!(path_params.info(), vec![(None, "foo".to_string())].into());
60 }
61}