Wrap the quoted output of a validation with a for loop if the field type is a collection.
(
&self,
param: proc_macro2::TokenStream,
tokens: proc_macro2::TokenStream,
)
| 218 | /// Wrap the quoted output of a validation with a for loop if |
| 219 | /// the field type is a collection. |
| 220 | pub fn wrap_validator_if_collection( |
| 221 | &self, |
| 222 | param: proc_macro2::TokenStream, |
| 223 | tokens: proc_macro2::TokenStream, |
| 224 | ) -> proc_macro2::TokenStream { |
| 225 | let field_name = &self.name; |
| 226 | |
| 227 | // When we're using an option, we'll have the field unwrapped, so we should not access it |
| 228 | // through `self`. |
| 229 | let prefix = (!self.is_option()).then(|| quote! { self. }); |
| 230 | |
| 231 | // When iterating over a list, the iterator has Item=T, while a map yields Item=(K, V), and |
| 232 | // we're only interested in V. |
| 233 | if self.is_list() { |
| 234 | quote!( |
| 235 | for (i, item) in #prefix #param.iter().enumerate() { |
| 236 | if let Err(mut errs) = item.validate() { |
| 237 | errs.errors_mut().iter_mut().for_each(|err| err.set_location_idx(i, #field_name)); |
| 238 | errors.merge(errs); |
| 239 | } |
| 240 | } |
| 241 | ) |
| 242 | } else if self.is_map() { |
| 243 | quote!( |
| 244 | for (key, item) in #prefix #param.iter() { |
| 245 | if let Err(mut errs) = item.validate() { |
| 246 | errs.errors_mut().iter_mut().for_each(|err| err.set_location_idx(key, #field_name)); |
| 247 | errors.merge(errs); |
| 248 | } |
| 249 | } |
| 250 | ) |
| 251 | } else { |
| 252 | tokens |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | pub fn wrap_modifier_if_option( |
| 257 | &self, |
no test coverage detected