http_path_params/path_params/
impl_path_param.rs1use alloc::string::{String, ToString as _};
2use core::{fmt::Display, str::FromStr};
3
4use crate::{PathParam, PathParams};
5
6use super::SetError;
7
8impl<T> PathParams for PathParam<T>
9where
10 T: FromStr + Display + Clone + Default + Send + Sync + 'static,
11 <T as FromStr>::Err: Display,
12{
13 fn size(&self) -> usize {
14 1
15 }
16 fn get(&self, index: usize) -> (Option<&'static str>, String) {
17 match index {
18 0 => (None, self.0.to_string()),
19 _ => panic!(),
20 }
21 }
22 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
23 match index {
24 0 => {
25 self.0 = value
26 .parse::<T>()
27 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
28 Ok(())
29 }
30 _ => panic!(),
31 }
32 }
33 fn verify(&self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
34 match index {
35 0 => {
36 let _ = value
37 .parse::<T>()
38 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
39 Ok(())
40 }
41 _ => panic!(),
42 }
43 }
44}
45
46impl<T1, T2> PathParams for (PathParam<T1>, PathParam<T2>)
47where
48 T1: FromStr + Display + Clone + Default + Send + Sync + 'static,
49 <T1 as FromStr>::Err: Display,
50 T2: FromStr + Display + Clone + Default + Send + Sync + 'static,
51 <T2 as FromStr>::Err: Display,
52{
53 fn size(&self) -> usize {
54 2
55 }
56 fn get(&self, index: usize) -> (Option<&'static str>, String) {
57 match index {
58 0 => (None, self.0 .0.to_string()),
59 1 => (None, self.1 .0.to_string()),
60 _ => panic!(),
61 }
62 }
63 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
64 match index {
65 0 => {
66 self.0 .0 = value
67 .parse::<T1>()
68 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
69 Ok(())
70 }
71 1 => {
72 self.1 .0 = value
73 .parse::<T2>()
74 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
75 Ok(())
76 }
77 _ => panic!(),
78 }
79 }
80 fn verify(&self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
81 match index {
82 0 => {
83 let _ = value
84 .parse::<T1>()
85 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
86 Ok(())
87 }
88 1 => {
89 let _ = value
90 .parse::<T2>()
91 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
92 Ok(())
93 }
94 _ => panic!(),
95 }
96 }
97}
98
99impl<T1, T2, T3> PathParams for (PathParam<T1>, PathParam<T2>, PathParam<T3>)
100where
101 T1: FromStr + Display + Clone + Default + Send + Sync + 'static,
102 <T1 as FromStr>::Err: Display,
103 T2: FromStr + Display + Clone + Default + Send + Sync + 'static,
104 <T2 as FromStr>::Err: Display,
105 T3: FromStr + Display + Clone + Default + Send + Sync + 'static,
106 <T3 as FromStr>::Err: Display,
107{
108 fn size(&self) -> usize {
109 3
110 }
111 fn get(&self, index: usize) -> (Option<&'static str>, String) {
112 match index {
113 0 => (None, self.0 .0.to_string()),
114 1 => (None, self.1 .0.to_string()),
115 2 => (None, self.2 .0.to_string()),
116 _ => panic!(),
117 }
118 }
119 fn set(&mut self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
120 match index {
121 0 => {
122 self.0 .0 = value
123 .parse::<T1>()
124 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
125 Ok(())
126 }
127 1 => {
128 self.1 .0 = value
129 .parse::<T2>()
130 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
131 Ok(())
132 }
133 2 => {
134 self.2 .0 = value
135 .parse::<T3>()
136 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
137 Ok(())
138 }
139 _ => panic!(),
140 }
141 }
142 fn verify(&self, index: usize, (_, value): &(Option<&str>, &str)) -> Result<(), SetError> {
143 match index {
144 0 => {
145 let _ = value
146 .parse::<T1>()
147 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
148 Ok(())
149 }
150 1 => {
151 let _ = value
152 .parse::<T2>()
153 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
154 Ok(())
155 }
156 2 => {
157 let _ = value
158 .parse::<T3>()
159 .map_err(|err| SetError::ValueParseFailed(err.to_string()))?;
160 Ok(())
161 }
162 _ => panic!(),
163 }
164 }
165}
166
167#[cfg(test)]
168mod tests {
169 use super::*;
170
171 use alloc::vec;
172
173 use crate::PathParamsInfo;
174
175 #[test]
176 fn test_get_and_set() {
177 let mut path_params: PathParam<usize> = Default::default();
179 path_params
180 .set_from_info(&PathParamsInfo(vec![(None, "1".to_string())]))
181 .unwrap();
182 assert_eq!(path_params, PathParam(1));
183 assert_eq!(path_params.info(), vec![(None, "1".to_string())].into());
184 assert!(path_params.verify(0, &(None, "1")).is_ok());
185
186 let mut path_params: (PathParam<usize>, PathParam<String>) = Default::default();
188 path_params
189 .set_from_info(&PathParamsInfo(vec![
190 (None, "1".to_string()),
191 (None, "foo".to_string()),
192 ]))
193 .unwrap();
194 assert_eq!(path_params, (PathParam(1), PathParam("foo".to_string())));
195 assert_eq!(
196 path_params.info(),
197 vec![(None, "1".to_string()), (None, "foo".to_string())].into()
198 );
199 assert!(path_params.verify(0, &(None, "1")).is_ok());
200 assert!(path_params.verify(1, &(None, "foo")).is_ok());
201
202 let mut path_params: (PathParam<usize>, PathParam<String>, PathParam<bool>) =
204 Default::default();
205 path_params
206 .set_from_info(&PathParamsInfo(vec![
207 (None, "1".to_string()),
208 (None, "foo".to_string()),
209 (None, "true".to_string()),
210 ]))
211 .unwrap();
212 assert_eq!(
213 path_params,
214 (PathParam(1), PathParam("foo".to_string()), PathParam(true))
215 );
216 assert_eq!(
217 path_params.info(),
218 vec![
219 (None, "1".to_string()),
220 (None, "foo".to_string()),
221 (None, "true".to_string())
222 ]
223 .into()
224 );
225 assert!(path_params.verify(0, &(None, "1")).is_ok());
226 assert!(path_params.verify(1, &(None, "foo")).is_ok());
227 assert!(path_params.verify(2, &(None, "true")).is_ok());
228 }
229}