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
57
58
59
60
61
62
use crate::{endpoint::endpoint_ident, util::CollectToResult};
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use std::iter;
use syn::{
parenthesized,
parse::{Parse, ParseStream},
punctuated::Punctuated,
DeriveInput, Result, Token
};
struct MethodList(Punctuated<Ident, Token![,]>);
impl Parse for MethodList {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let content;
let _paren = parenthesized!(content in input);
let list = Punctuated::parse_separated_nonempty(&content)?;
Ok(Self(list))
}
}
pub fn expand_resource(input: DeriveInput) -> Result<TokenStream> {
let ident = input.ident;
let methods = input
.attrs
.into_iter()
.filter(|attr| attr.path.is_ident("resource"))
.map(|attr| syn::parse2(attr.tokens).map(|m: MethodList| m.0.into_iter()))
.flat_map(|list| match list {
Ok(iter) => Box::new(iter.map(|method| {
let ident = endpoint_ident(&method);
Ok(quote!(route.endpoint::<#ident>();))
})) as Box<dyn Iterator<Item = Result<TokenStream>>>,
Err(err) => Box::new(iter::once(Err(err)))
})
.collect_to_result()?;
let non_openapi_impl = quote! {
impl ::gotham_restful::Resource for #ident {
fn setup<D: ::gotham_restful::DrawResourceRoutes>(mut route: D) {
#(#methods)*
}
}
};
let openapi_impl = if !cfg!(feature = "openapi") {
None
} else {
Some(quote! {
impl ::gotham_restful::ResourceWithSchema for #ident {
fn setup<D: ::gotham_restful::DrawResourceRoutesWithSchema>(mut route: D) {
#(#methods)*
}
}
})
};
Ok(quote! {
#non_openapi_impl
#openapi_impl
})
}