wasmer_bus_macros/
method_inputs.rs

1use syn::parse::{Parse, ParseStream};
2use syn::punctuated::Punctuated;
3use syn::FnArg;
4use syn::*;
5
6pub struct MethodInputs {
7    pub inputs: Punctuated<MethodInput, Token![,]>,
8    pub has_self: bool,
9}
10
11impl Parse for MethodInputs {
12    fn parse(input: ParseStream) -> Result<Self> {
13        let args: Punctuated<FnArg, Token![,]> = Punctuated::parse_terminated(input)?;
14
15        let mut inputs: Punctuated<MethodInput, Token![,]> = Punctuated::new();
16        let mut has_self = false;
17
18        for arg in args {
19            match arg {
20                FnArg::Receiver(a) => {
21                    if let Some((_, None)) = a.reference {
22                    } else {
23                        return Err(Error::new(
24                            input.span(),
25                            "all bus methods must have an immutable self reference",
26                        ));
27                    }
28                    if a.mutability.is_some() {
29                        return Err(Error::new(input.span(), "bus methods can not be mutable"));
30                    }
31                    has_self = true;
32                }
33                FnArg::Typed(typed_arg) => match typed_arg.pat.as_ref() {
34                    Pat::Ident(arg_name) => {
35                        inputs.push(MethodInput::new(typed_arg.clone(), arg_name.clone()));
36                    }
37                    _ => {
38                        return Err(Error::new(
39                            input.span(),
40                            "only named arguments are supported",
41                        ));
42                    }
43                },
44            }
45        }
46
47        Ok(MethodInputs { inputs, has_self })
48    }
49}
50
51pub struct MethodInput {
52    pub attrs: Vec<Attribute>,
53    pub ident: Ident,
54    pub pat_ident: PatIdent,
55    pub pat_type: PatType,
56    pub ty_attrs: Vec<Attribute>,
57    pub ty: Box<Type>,
58}
59
60impl MethodInput {
61    fn new(pat_type: PatType, pat_ident: PatIdent) -> Self {
62        MethodInput {
63            attrs: pat_ident.attrs.clone(),
64            ident: pat_ident.ident.clone(),
65            pat_ident,
66            ty_attrs: pat_type.attrs.clone(),
67            ty: pat_type.ty.clone(),
68            pat_type: pat_type,
69        }
70    }
71}