| 1 |
use std::marker::PhantomData; |
| 2 |
|
| 3 |
use quote::ToTokens; |
| 4 |
use syn::{visit_mut::VisitMut, Attribute, FnArg, ItemFn, Pat}; |
| 5 |
|
| 6 |
use crate::{error::ErrorsVec, refident::MaybePat, utils::attr_is}; |
| 7 |
|
| 8 |
pub trait AttrBuilder<E> { |
| 9 |
type Out; |
| 10 |
|
| 11 |
fn build(attr: Attribute, extra: &E) -> syn::Result<Self::Out>; |
| 12 |
} |
| 13 |
|
| 14 |
pub trait Validator<T> { |
| 15 |
fn validate(_arg: &T) -> syn::Result<()> { |
| 16 |
Ok(()) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
impl AttrBuilder<Pat> for () { |
| 21 |
type Out = Pat; |
| 22 |
|
| 23 |
fn build(_attr: Attribute, pat: &Pat) -> syn::Result<Self::Out> { |
| 24 |
Ok(pat.clone()) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
impl AttrBuilder<ItemFn> for () { |
| 29 |
type Out = Attribute; |
| 30 |
|
| 31 |
fn build(attr: Attribute, _item_fn: &ItemFn) -> syn::Result<Self::Out> { |
| 32 |
Ok(attr.clone()) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl<T> Validator<T> for () {} |
| 37 |
|
| 38 |
/// Simple struct used to visit function argument attributes and extract attributes that match |
| 39 |
/// the `name`: Only one attribute is allowed for arguments. |
| 40 |
pub struct JustOnceFnArgAttributeExtractor<'a, B = ()> |
| 41 |
where |
| 42 |
B: AttrBuilder<Pat>, |
| 43 |
{ |
| 44 |
name: &'a str, |
| 45 |
elements: Vec<B::Out>, |
| 46 |
errors: Vec<syn::Error>, |
| 47 |
_phantom: PhantomData<B>, |
| 48 |
} |
| 49 |
|
| 50 |
impl<'a> From<&'a str> for JustOnceFnArgAttributeExtractor<'a, ()> { |
| 51 |
fn from(value: &'a str) -> Self { |
| 52 |
Self::new(value) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl<'a, B> JustOnceFnArgAttributeExtractor<'a, B> |
| 57 |
where |
| 58 |
B: AttrBuilder<Pat>, |
| 59 |
{ |
| 60 |
pub fn new(name: &'a str) -> Self { |
| 61 |
Self { |
| 62 |
name, |
| 63 |
elements: Default::default(), |
| 64 |
errors: Default::default(), |
| 65 |
_phantom: PhantomData, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
pub fn take(self) -> Result<Vec<B::Out>, ErrorsVec> { |
| 70 |
if self.errors.is_empty() { |
| 71 |
Ok(self.elements) |
| 72 |
} else { |
| 73 |
Err(self.errors.into()) |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl<B> VisitMut for JustOnceFnArgAttributeExtractor<'_, B> |
| 79 |
where |
| 80 |
B: AttrBuilder<Pat>, |
| 81 |
B: Validator<FnArg>, |
| 82 |
{ |
| 83 |
fn visit_fn_arg_mut(&mut self, node: &mut FnArg) { |
| 84 |
let pat = match node.maybe_pat() { |
| 85 |
Some(pat) => pat.clone(), |
| 86 |
None => return, |
| 87 |
}; |
| 88 |
if let FnArg::Typed(ref mut arg) = node { |
| 89 |
// Extract interesting attributes |
| 90 |
let attrs = std::mem::take(&mut arg.attrs); |
| 91 |
let (extracted, remain): (Vec<_>, Vec<_>) = |
| 92 |
attrs.into_iter().partition(|a| attr_is(a, self.name)); |
| 93 |
|
| 94 |
arg.attrs = remain; |
| 95 |
|
| 96 |
let parsed = extracted |
| 97 |
.into_iter() |
| 98 |
.map(|attr| B::build(attr.clone(), &pat).map(|t| (attr, t))) |
| 99 |
.collect::<Result<Vec<_>, _>>(); |
| 100 |
|
| 101 |
match parsed { |
| 102 |
Ok(data) => match data.len() { |
| 103 |
1 => match B::validate(node) { |
| 104 |
Ok(_) => self.elements.extend(data.into_iter().map(|(_attr, t)| t)), |
| 105 |
Err(e) => { |
| 106 |
self.errors.push(e); |
| 107 |
} |
| 108 |
}, |
| 109 |
|
| 110 |
0 => {} |
| 111 |
_ => { |
| 112 |
self.errors |
| 113 |
.extend(data.into_iter().skip(1).map(|(attr, _t)| { |
| 114 |
syn::Error::new_spanned( |
| 115 |
attr.into_token_stream(), |
| 116 |
format!("Cannot use #[{}] more than once.", self.name), |
| 117 |
) |
| 118 |
})); |
| 119 |
} |
| 120 |
}, |
| 121 |
Err(e) => { |
| 122 |
self.errors.push(e); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/// Simple struct used to visit function attributes and extract attributes that match |
| 130 |
/// the `name`: Only one attribute is allowed for arguments. |
| 131 |
pub struct JustOnceFnAttributeExtractor<'a, B = ()> |
| 132 |
where |
| 133 |
B: AttrBuilder<ItemFn>, |
| 134 |
{ |
| 135 |
name: &'a str, |
| 136 |
inner: Result<Option<B::Out>, Vec<syn::Error>>, |
| 137 |
_phantom: PhantomData<B>, |
| 138 |
} |
| 139 |
|
| 140 |
impl<'a> From<&'a str> for JustOnceFnAttributeExtractor<'a, ()> { |
| 141 |
fn from(value: &'a str) -> Self { |
| 142 |
Self::new(value) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl<'a, B> JustOnceFnAttributeExtractor<'a, B> |
| 147 |
where |
| 148 |
B: AttrBuilder<ItemFn>, |
| 149 |
{ |
| 150 |
pub fn new(name: &'a str) -> Self { |
| 151 |
Self { |
| 152 |
name, |
| 153 |
inner: Ok(Default::default()), |
| 154 |
_phantom: PhantomData, |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
pub fn take(self) -> Result<Option<B::Out>, ErrorsVec> { |
| 159 |
self.inner.map_err(Into::into) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
impl<B> VisitMut for JustOnceFnAttributeExtractor<'_, B> |
| 164 |
where |
| 165 |
B: AttrBuilder<ItemFn>, |
| 166 |
B: Validator<ItemFn>, |
| 167 |
{ |
| 168 |
fn visit_item_fn_mut(&mut self, item_fn: &mut ItemFn) { |
| 169 |
// Extract interesting attributes |
| 170 |
let attrs = std::mem::take(&mut item_fn.attrs); |
| 171 |
let (extracted, remain): (Vec<_>, Vec<_>) = |
| 172 |
attrs.into_iter().partition(|a| attr_is(a, self.name)); |
| 173 |
|
| 174 |
item_fn.attrs = remain; |
| 175 |
|
| 176 |
let parsed = extracted |
| 177 |
.into_iter() |
| 178 |
.map(|attr| B::build(attr.clone(), item_fn).map(|t| (attr, t))) |
| 179 |
.collect::<Result<Vec<_>, _>>(); |
| 180 |
let mut errors = Vec::default(); |
| 181 |
let mut out = None; |
| 182 |
|
| 183 |
match parsed { |
| 184 |
Ok(data) => match data.len() { |
| 185 |
1 => match B::validate(item_fn) { |
| 186 |
Ok(_) => { |
| 187 |
out = data.into_iter().next().map(|(_attr, t)| t); |
| 188 |
} |
| 189 |
Err(e) => { |
| 190 |
errors.push(e); |
| 191 |
} |
| 192 |
}, |
| 193 |
|
| 194 |
0 => {} |
| 195 |
_ => { |
| 196 |
errors.extend(data.into_iter().skip(1).map(|(attr, _t)| { |
| 197 |
syn::Error::new_spanned( |
| 198 |
attr.into_token_stream(), |
| 199 |
format!("Cannot use #[{}] more than once.", self.name), |
| 200 |
) |
| 201 |
})); |
| 202 |
} |
| 203 |
}, |
| 204 |
Err(e) => { |
| 205 |
errors.push(e); |
| 206 |
} |
| 207 |
}; |
| 208 |
if errors.is_empty() { |
| 209 |
self.inner = Ok(out); |
| 210 |
} else { |
| 211 |
self.inner = Err(errors); |
| 212 |
} |
| 213 |
} |
| 214 |
} |