| 1 |
use quote::{format_ident, ToTokens}; |
| 2 |
use syn::{visit_mut::VisitMut, FnArg, Ident, ItemFn, Pat, PatType, Type}; |
| 3 |
|
| 4 |
use crate::{error::ErrorsVec, refident::MaybeType}; |
| 5 |
|
| 6 |
use super::{ |
| 7 |
arguments::FutureArg, |
| 8 |
just_once::{ |
| 9 |
AttrBuilder, JustOnceFnArgAttributeExtractor, JustOnceFnAttributeExtractor, Validator, |
| 10 |
}, |
| 11 |
}; |
| 12 |
|
| 13 |
pub(crate) fn extract_futures(item_fn: &mut ItemFn) -> Result<Vec<(Pat, FutureArg)>, ErrorsVec> { |
| 14 |
let mut extractor = JustOnceFnArgAttributeExtractor::<FutureBuilder>::new("future"); |
| 15 |
|
| 16 |
extractor.visit_item_fn_mut(item_fn); |
| 17 |
extractor.take() |
| 18 |
} |
| 19 |
|
| 20 |
pub(crate) fn extract_global_awt(item_fn: &mut ItemFn) -> Result<bool, ErrorsVec> { |
| 21 |
let mut extractor = JustOnceFnAttributeExtractor::<GlobalAwtBuilder>::new("awt"); |
| 22 |
|
| 23 |
extractor.visit_item_fn_mut(item_fn); |
| 24 |
extractor.take().map(|inner| inner.is_some()) |
| 25 |
} |
| 26 |
|
| 27 |
struct GlobalAwtBuilder; |
| 28 |
|
| 29 |
impl AttrBuilder<ItemFn> for GlobalAwtBuilder { |
| 30 |
type Out = (); |
| 31 |
|
| 32 |
fn build(_attr: syn::Attribute, _ident: &ItemFn) -> syn::Result<Self::Out> { |
| 33 |
Ok(()) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl Validator<ItemFn> for GlobalAwtBuilder {} |
| 38 |
|
| 39 |
struct FutureBuilder; |
| 40 |
|
| 41 |
impl AttrBuilder<Pat> for FutureBuilder { |
| 42 |
type Out = (Pat, FutureArg); |
| 43 |
|
| 44 |
fn build(attr: syn::Attribute, pat: &Pat) -> syn::Result<Self::Out> { |
| 45 |
Self::compute_arguments_kind(&attr).map(|kind| (pat.clone(), kind)) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
impl Validator<FnArg> for FutureBuilder { |
| 50 |
fn validate(arg: &FnArg) -> syn::Result<()> { |
| 51 |
arg.as_future_impl_type().map(|_| ()).ok_or_else(|| { |
| 52 |
syn::Error::new_spanned( |
| 53 |
arg.maybe_type().unwrap().into_token_stream(), |
| 54 |
"This type cannot used to generate impl Future.".to_owned(), |
| 55 |
) |
| 56 |
}) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl FutureBuilder { |
| 61 |
fn compute_arguments_kind(arg: &syn::Attribute) -> syn::Result<FutureArg> { |
| 62 |
if matches!(arg.meta, syn::Meta::Path(_)) { |
| 63 |
Ok(FutureArg::Define) |
| 64 |
} else { |
| 65 |
match arg.parse_args::<Option<Ident>>()? { |
| 66 |
Some(awt) if awt == format_ident!("awt") => Ok(FutureArg::Await), |
| 67 |
None => Ok(FutureArg::Define), |
| 68 |
Some(invalid) => Err(syn::Error::new_spanned( |
| 69 |
arg.parse_args::<Option<Ident>>()?.into_token_stream(), |
| 70 |
format!("Invalid '{invalid}' #[future(...)] arg."), |
| 71 |
)), |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
pub(crate) trait MaybeFutureImplType { |
| 78 |
fn as_future_impl_type(&self) -> Option<&Type>; |
| 79 |
|
| 80 |
fn as_mut_future_impl_type(&mut self) -> Option<&mut Type>; |
| 81 |
} |
| 82 |
|
| 83 |
impl MaybeFutureImplType for FnArg { |
| 84 |
fn as_future_impl_type(&self) -> Option<&Type> { |
| 85 |
match self { |
| 86 |
FnArg::Typed(PatType { ty, .. }) if can_impl_future(ty.as_ref()) => Some(ty.as_ref()), |
| 87 |
_ => None, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
fn as_mut_future_impl_type(&mut self) -> Option<&mut Type> { |
| 92 |
match self { |
| 93 |
FnArg::Typed(PatType { ty, .. }) if can_impl_future(ty.as_ref()) => Some(ty.as_mut()), |
| 94 |
_ => None, |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
fn can_impl_future(ty: &Type) -> bool { |
| 100 |
use Type::*; |
| 101 |
!matches!( |
| 102 |
ty, |
| 103 |
Group(_) |
| 104 |
| ImplTrait(_) |
| 105 |
| Infer(_) |
| 106 |
| Macro(_) |
| 107 |
| Never(_) |
| 108 |
| Slice(_) |
| 109 |
| TraitObject(_) |
| 110 |
| Verbatim(_) |
| 111 |
) |
| 112 |
} |
| 113 |
|
| 114 |
#[cfg(test)] |
| 115 |
mod should { |
| 116 |
use super::*; |
| 117 |
use crate::test::{assert_eq, *}; |
| 118 |
use rstest_test::assert_in; |
| 119 |
|
| 120 |
#[rstest] |
| 121 |
#[case("fn simple(a: u32) {}")] |
| 122 |
#[case("fn more(a: u32, b: &str) {}")] |
| 123 |
#[case("fn gen<S: AsRef<str>>(a: u32, b: S) {}")] |
| 124 |
#[case("fn attr(#[case] a: u32, #[values(1,2)] b: i32) {}")] |
| 125 |
fn not_change_anything_if_no_future_attribute_found(#[case] item_fn: &str) { |
| 126 |
let mut item_fn: ItemFn = item_fn.ast(); |
| 127 |
let orig = item_fn.clone(); |
| 128 |
|
| 129 |
let composed_tuple!(futures, awt) = merge_errors!( |
| 130 |
extract_futures(&mut item_fn), |
| 131 |
extract_global_awt(&mut item_fn) |
| 132 |
) |
| 133 |
.unwrap(); |
| 134 |
|
| 135 |
assert_eq!(orig, item_fn); |
| 136 |
assert!(futures.is_empty()); |
| 137 |
assert!(!awt); |
| 138 |
} |
| 139 |
|
| 140 |
#[rstest] |
| 141 |
#[case::simple("fn f(#[future] a: u32) {}", "fn f(a: u32) {}", &[("a", FutureArg::Define)], false)] |
| 142 |
#[case::global_awt("#[awt] fn f(a: u32) {}", "fn f(a: u32) {}", &[], true)] |
| 143 |
#[case::global_awt_with_inner_function("#[awt] fn f(a: u32) { fn g(){} }", "fn f(a: u32) { fn g(){} }", &[], true)] |
| 144 |
#[case::simple_awaited("fn f(#[future(awt)] a: u32) {}", "fn f(a: u32) {}", &[("a", FutureArg::Await)], false)] |
| 145 |
#[case::simple_awaited_and_global("#[awt] fn f(#[future(awt)] a: u32) {}", "fn f(a: u32) {}", &[("a", FutureArg::Await)], true)] |
| 146 |
#[case::more_than_one( |
| 147 |
"fn f(#[future] a: u32, #[future(awt)] b: String, #[future()] c: std::collection::HashMap<usize, String>) {}", |
| 148 |
r#"fn f(a: u32, |
| 149 |
b: String, |
| 150 |
c: std::collection::HashMap<usize, String>) {}"#, |
| 151 |
&[("a", FutureArg::Define), ("b", FutureArg::Await), ("c", FutureArg::Define)], |
| 152 |
false, |
| 153 |
)] |
| 154 |
#[case::just_one( |
| 155 |
"fn f(a: u32, #[future] b: String) {}", |
| 156 |
r#"fn f(a: u32, b: String) {}"#, |
| 157 |
&[("b", FutureArg::Define)], |
| 158 |
false, |
| 159 |
)] |
| 160 |
#[case::just_one_awaited( |
| 161 |
"fn f(a: u32, #[future(awt)] b: String) {}", |
| 162 |
r#"fn f(a: u32, b: String) {}"#, |
| 163 |
&[("b", FutureArg::Await)], |
| 164 |
false, |
| 165 |
)] |
| 166 |
fn extract( |
| 167 |
#[case] item_fn: &str, |
| 168 |
#[case] expected: &str, |
| 169 |
#[case] expected_futures: &[(&str, FutureArg)], |
| 170 |
#[case] expected_awt: bool, |
| 171 |
) { |
| 172 |
let mut item_fn: ItemFn = item_fn.ast(); |
| 173 |
let expected: ItemFn = expected.ast(); |
| 174 |
|
| 175 |
let composed_tuple!(futures, awt) = merge_errors!( |
| 176 |
extract_futures(&mut item_fn), |
| 177 |
extract_global_awt(&mut item_fn) |
| 178 |
) |
| 179 |
.unwrap(); |
| 180 |
|
| 181 |
assert_eq!(expected, item_fn); |
| 182 |
assert_eq!( |
| 183 |
futures, |
| 184 |
expected_futures |
| 185 |
.into_iter() |
| 186 |
.map(|(id, a)| (pat(id), *a)) |
| 187 |
.collect::<Vec<_>>() |
| 188 |
); |
| 189 |
assert_eq!(expected_awt, awt); |
| 190 |
} |
| 191 |
|
| 192 |
#[rstest] |
| 193 |
#[case::base(r#"#[awt] fn f(a: u32) {}"#, r#"fn f(a: u32) {}"#)] |
| 194 |
#[case::two( |
| 195 |
r#" |
| 196 |
#[awt] |
| 197 |
#[awt] |
| 198 |
fn f(a: u32) {} |
| 199 |
"#, |
| 200 |
r#"fn f(a: u32) {}"# |
| 201 |
)] |
| 202 |
#[case::inner( |
| 203 |
r#" |
| 204 |
#[one] |
| 205 |
#[awt] |
| 206 |
#[two] |
| 207 |
fn f(a: u32) {} |
| 208 |
"#, |
| 209 |
r#" |
| 210 |
#[one] |
| 211 |
#[two] |
| 212 |
fn f(a: u32) {} |
| 213 |
"# |
| 214 |
)] |
| 215 |
fn remove_all_awt_attributes(#[case] item_fn: &str, #[case] expected: &str) { |
| 216 |
let mut item_fn: ItemFn = item_fn.ast(); |
| 217 |
let expected: ItemFn = expected.ast(); |
| 218 |
|
| 219 |
let _ = extract_global_awt(&mut item_fn); |
| 220 |
|
| 221 |
assert_eq!(item_fn, expected); |
| 222 |
} |
| 223 |
|
| 224 |
#[rstest] |
| 225 |
#[case::no_more_than_one("fn f(#[future] #[future] a: u32) {}", "more than once")] |
| 226 |
#[case::no_impl("fn f(#[future] a: impl AsRef<str>) {}", "generate impl Future")] |
| 227 |
#[case::no_slice("fn f(#[future] a: [i32]) {}", "generate impl Future")] |
| 228 |
#[case::invalid_arg("fn f(#[future(other)] a: [i32]) {}", "Invalid 'other'")] |
| 229 |
#[case::no_more_than_one_awt("#[awt] #[awt] fn f(a: u32) {}", "more than once")] |
| 230 |
fn raise_error(#[case] item_fn: &str, #[case] message: &str) { |
| 231 |
let mut item_fn: ItemFn = item_fn.ast(); |
| 232 |
|
| 233 |
let err = merge_errors!( |
| 234 |
extract_futures(&mut item_fn), |
| 235 |
extract_global_awt(&mut item_fn) |
| 236 |
) |
| 237 |
.unwrap_err(); |
| 238 |
|
| 239 |
assert_in!(format!("{:?}", err), message); |
| 240 |
} |
| 241 |
} |