|
| 1 | +use syn::{GenericArgument, Path, PathArguments, Type, TypeParamBound}; |
| 2 | + |
| 3 | +const PRIMITIVE_AND_BUILT_IN_TYPES: [&str; 18] = [ |
| 4 | + "bool", "i8", "i16", "i32", "i64", "i128", "isize", "u8", "u16", "u32", "u64", "u128", "usize", |
| 5 | + "f32", "f64", "char", "str", "String", |
| 6 | +]; |
| 7 | + |
| 8 | +const CONTAINER_TYPES: [&str; 20] = [ |
| 9 | + "Arc", |
| 10 | + "BTreeMap", |
| 11 | + "BTreeSet", |
| 12 | + "HashMap", |
| 13 | + "HashSet", |
| 14 | + "LinkedList", |
| 15 | + "Option", |
| 16 | + "Rc", |
| 17 | + "Vec", |
| 18 | + "VecDeque", |
| 19 | + "std::collections::BTreeMap", |
| 20 | + "std::collections::BTreeSet", |
| 21 | + "std::collections::HashMap", |
| 22 | + "std::collections::HashSet", |
| 23 | + "std::collections::LinkedList", |
| 24 | + "std::collections::VecDeque", |
| 25 | + "std::option::Option", |
| 26 | + "std::rc::Rc", |
| 27 | + "std::sync::Arc", |
| 28 | + "std::vec::Vec", |
| 29 | +]; |
| 30 | + |
| 31 | +fn path_to_string(path: &Path) -> String { |
| 32 | + // TODO: This is probably slow, replace with comparisons. |
| 33 | + path.segments |
| 34 | + .iter() |
| 35 | + .map(|segment| segment.ident.to_string()) |
| 36 | + .collect::<Vec<_>>() |
| 37 | + .join("::") |
| 38 | +} |
| 39 | + |
| 40 | +fn is_validate_path(path: &Path) -> bool { |
| 41 | + let path_string = path_to_string(path); |
| 42 | + path_string == "Validate" || path_string == "fortifier::Validate" |
| 43 | +} |
| 44 | + |
| 45 | +fn should_validate_generic_argument(arg: &GenericArgument) -> bool { |
| 46 | + match arg { |
| 47 | + GenericArgument::Lifetime(_) => true, |
| 48 | + GenericArgument::Type(r#type) => should_validate_type(r#type), |
| 49 | + GenericArgument::Const(_expr) => todo!(), |
| 50 | + GenericArgument::AssocType(_assoc_type) => todo!(), |
| 51 | + GenericArgument::AssocConst(_assoc_const) => todo!(), |
| 52 | + GenericArgument::Constraint(_constraint) => todo!(), |
| 53 | + _ => true, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn should_validate_path(path: &Path) -> bool { |
| 58 | + if let Some(ident) = path.get_ident() { |
| 59 | + return !PRIMITIVE_AND_BUILT_IN_TYPES.contains(&ident.to_string().as_str()); |
| 60 | + } |
| 61 | + let path_string = path_to_string(path); |
| 62 | + |
| 63 | + if CONTAINER_TYPES.contains(&path_string.as_str()) |
| 64 | + && let Some(segment) = path.segments.last() |
| 65 | + && let PathArguments::AngleBracketed(arguments) = &segment.arguments |
| 66 | + && !arguments.args.iter().all(should_validate_generic_argument) |
| 67 | + { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + true |
| 72 | +} |
| 73 | + |
| 74 | +pub fn should_validate_type(r#type: &Type) -> bool { |
| 75 | + match r#type { |
| 76 | + Type::Array(r#type) => should_validate_type(&r#type.elem), |
| 77 | + Type::BareFn(_) => false, |
| 78 | + Type::Group(r#type) => should_validate_type(&r#type.elem), |
| 79 | + Type::ImplTrait(r#type) => r#type.bounds.iter().any( |
| 80 | + |bound| matches!(bound, TypeParamBound::Trait(bound) if is_validate_path(&bound.path)), |
| 81 | + ), |
| 82 | + Type::Infer(_) => true, |
| 83 | + Type::Macro(_) => true, |
| 84 | + Type::Never(_) => false, |
| 85 | + Type::Paren(r#type) => should_validate_type(&r#type.elem), |
| 86 | + Type::Path(r#type) => should_validate_path(&r#type.path), |
| 87 | + Type::Ptr(r#type) => should_validate_type(&r#type.elem), |
| 88 | + Type::Reference(r#type) => should_validate_type(&r#type.elem), |
| 89 | + Type::Slice(r#type) => should_validate_type(&r#type.elem), |
| 90 | + Type::TraitObject(r#type) => r#type.bounds.iter().any( |
| 91 | + |bound| matches!(bound, TypeParamBound::Trait(bound) if is_validate_path(&bound.path)), |
| 92 | + ), |
| 93 | + Type::Tuple(r#type) => { |
| 94 | + !r#type.elems.is_empty() && r#type.elems.iter().all(should_validate_type) |
| 95 | + } |
| 96 | + Type::Verbatim(_) => false, |
| 97 | + _ => false, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use proc_macro2::TokenStream; |
| 104 | + use quote::quote; |
| 105 | + |
| 106 | + use super::should_validate_type; |
| 107 | + |
| 108 | + fn validate(tokens: TokenStream) -> bool { |
| 109 | + should_validate_type(&syn::parse2(tokens).expect("Type should be valid.")) |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn should_validate() { |
| 114 | + assert!(validate(quote!(&T))); |
| 115 | + assert!(validate(quote!(T))); |
| 116 | + |
| 117 | + assert!(validate(quote!((T, T)))); |
| 118 | + assert!(validate(quote!((A, B, C)))); |
| 119 | + |
| 120 | + assert!(validate(quote!([T]))); |
| 121 | + assert!(validate(quote!([T; 3]))); |
| 122 | + assert!(validate(quote!([&T]))); |
| 123 | + assert!(validate(quote!([&T; 3]))); |
| 124 | + assert!(validate(quote!(&[T]))); |
| 125 | + assert!(validate(quote!(&[T; 3]))); |
| 126 | + |
| 127 | + assert!(validate(quote!(Arc<T>))); |
| 128 | + assert!(validate(quote!(BTreeSet<T>))); |
| 129 | + assert!(validate(quote!(BTreeMap<K, V>))); |
| 130 | + assert!(validate(quote!(HashSet<T>))); |
| 131 | + assert!(validate(quote!(HashMap<K, V>))); |
| 132 | + assert!(validate(quote!(LinkedList<T>))); |
| 133 | + assert!(validate(quote!(Option<T>))); |
| 134 | + assert!(validate(quote!(Option<Option<T>>))); |
| 135 | + assert!(validate(quote!(Rc<T>))); |
| 136 | + assert!(validate(quote!(Vec<T>))); |
| 137 | + assert!(validate(quote!(VecDeque<T>))); |
| 138 | + |
| 139 | + assert!(validate(quote!(impl Validate))); |
| 140 | + assert!(validate(quote!(impl fortifier::Validate))); |
| 141 | + assert!(validate(quote!(dyn Validate))); |
| 142 | + assert!(validate(quote!(dyn ::fortifier::Validate))); |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn should_not_validate() { |
| 147 | + assert!(!validate(quote!(bool))); |
| 148 | + assert!(!validate(quote!(i8))); |
| 149 | + assert!(!validate(quote!(i16))); |
| 150 | + assert!(!validate(quote!(i32))); |
| 151 | + assert!(!validate(quote!(i64))); |
| 152 | + assert!(!validate(quote!(i128))); |
| 153 | + assert!(!validate(quote!(isize))); |
| 154 | + assert!(!validate(quote!(u8))); |
| 155 | + assert!(!validate(quote!(u16))); |
| 156 | + assert!(!validate(quote!(u32))); |
| 157 | + assert!(!validate(quote!(u64))); |
| 158 | + assert!(!validate(quote!(u128))); |
| 159 | + assert!(!validate(quote!(usize))); |
| 160 | + assert!(!validate(quote!(f32))); |
| 161 | + assert!(!validate(quote!(f64))); |
| 162 | + assert!(!validate(quote!(char))); |
| 163 | + assert!(!validate(quote!(&str))); |
| 164 | + assert!(!validate(quote!(String))); |
| 165 | + |
| 166 | + assert!(!validate(quote!(()))); |
| 167 | + assert!(!validate(quote!((bool, bool)))); |
| 168 | + assert!(!validate(quote!((usize, usize, usize)))); |
| 169 | + assert!(!validate(quote!((usize, &str)))); |
| 170 | + |
| 171 | + assert!(!validate(quote!([isize]))); |
| 172 | + assert!(!validate(quote!([&str; 3]))); |
| 173 | + assert!(!validate(quote!(&[isize]))); |
| 174 | + assert!(!validate(quote!(&[&str; 3]))); |
| 175 | + |
| 176 | + assert!(!validate(quote!(Arc<&str>))); |
| 177 | + assert!(!validate(quote!(BTreeSet<usize>))); |
| 178 | + assert!(!validate(quote!(BTreeMap<usize, &str>))); |
| 179 | + assert!(!validate(quote!(HashSet<&str>))); |
| 180 | + assert!(!validate(quote!(HashMap<&str, &str>))); |
| 181 | + assert!(!validate(quote!(LinkedList<char>))); |
| 182 | + assert!(!validate(quote!(Option<char>))); |
| 183 | + assert!(!validate(quote!(Option<Option<String>>))); |
| 184 | + assert!(!validate(quote!(Rc<&str>))); |
| 185 | + assert!(!validate(quote!(Vec<usize>))); |
| 186 | + assert!(!validate(quote!(VecDeque<String>))); |
| 187 | + |
| 188 | + assert!(!validate(quote!(impl Serialize))); |
| 189 | + assert!(!validate(quote!(dyn Serialize))); |
| 190 | + } |
| 191 | +} |
0 commit comments