1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::refident::MaybeIdent;
use syn::{Attribute, FnArg, Ident, ItemFn};
pub(crate) fn fn_args_idents(test: &ItemFn) -> impl Iterator<Item = &Ident> {
fn_args(&test).filter_map(MaybeIdent::maybe_ident)
}
pub(crate) fn fn_args_has_ident(fn_decl: &ItemFn, ident: &Ident) -> bool {
fn_args_idents(fn_decl).find(|&id| id == ident).is_some()
}
pub(crate) fn fn_args(item_fn: &ItemFn) -> impl Iterator<Item = &FnArg> {
item_fn.sig.inputs.iter()
}
pub(crate) fn attr_ends_with(attr: &Attribute, segment: &syn::PathSegment) -> bool {
&attr.path.segments.iter().last() == &Some(segment)
}
#[cfg(test)]
mod test {
use syn::parse_quote;
use super::*;
use crate::test::{assert_eq, *};
#[test]
fn fn_args_idents_should() {
let item_fn = parse_quote! {
fn the_functon(first: u32, second: u32) {}
};
let mut args = fn_args_idents(&item_fn);
assert_eq!("first", args.next().unwrap().to_string());
assert_eq!("second", args.next().unwrap().to_string());
}
#[test]
fn fn_args_has_ident_should() {
let item_fn = parse_quote! {
fn the_functon(first: u32, second: u32) {}
};
assert!(fn_args_has_ident(&item_fn, &ident("first")));
assert!(!fn_args_has_ident(&item_fn, &ident("third")));
}
}