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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#![recursion_limit = "128"]
extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
use proc_macro::TokenStream;
use syn::{DeriveInput, Path, parse::{Parse, ParseStream, Result}};
mod impl_saveload;
#[proc_macro_derive(Component, attributes(storage))]
pub fn component(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
let gen = impl_component(&ast);
gen.into()
}
struct StorageAttribute {
storage: Path,
}
impl Parse for StorageAttribute {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let _parenthesized_token = parenthesized!(content in input);
Ok(StorageAttribute {
storage: content.parse()?
})
}
}
fn impl_component(ast: &DeriveInput) -> proc_macro2::TokenStream {
let name = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let storage = ast
.attrs
.iter()
.find(|attr| attr.path.segments[0].ident == "storage")
.map(|attr| {
syn::parse2::<StorageAttribute>(attr.tts.clone())
.unwrap()
.storage
})
.unwrap_or_else(|| parse_quote!(DenseVecStorage));
quote! {
impl #impl_generics Component for #name #ty_generics #where_clause {
type Storage = #storage<Self>;
}
}
}
#[proc_macro_derive(ConvertSaveload)]
pub fn saveload(input: TokenStream) -> TokenStream {
use impl_saveload::impl_saveload;
let mut ast = syn::parse(input).unwrap();
let gen = impl_saveload(&mut ast);
gen.into()
}