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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#![recursion_limit = "256"]
extern crate proc_macro;
mod pb_convert;
mod tx_set;
use proc_macro::TokenStream;
use quote::quote;
use syn::{Attribute, Lit, Meta, MetaList, MetaNameValue, NestedMeta, Path};
const CRATE_PATH_ATTRIBUTE: &str = "crate";
const PB_CONVERT_ATTRIBUTE: &str = "pb";
const SERDE_PB_CONVERT_ATTRIBUTE: &str = "serde_pb_convert";
#[proc_macro_derive(ProtobufConvert, attributes(exonum))]
pub fn generate_protobuf_convert(input: TokenStream) -> TokenStream {
pb_convert::implement_protobuf_convert(input)
}
#[proc_macro_derive(TransactionSet, attributes(exonum))]
pub fn transaction_set_derive(input: TokenStream) -> TokenStream {
tx_set::implement_transaction_set(input)
}
fn get_exonum_types_prefix(attrs: &[Attribute]) -> impl quote::ToTokens {
let map_attrs = get_exonum_name_value_attributes(attrs);
let crate_path = map_attrs.into_iter().find_map(|nv| match &nv {
MetaNameValue {
lit: Lit::Str(path),
ident,
..
} if ident == CRATE_PATH_ATTRIBUTE => Some(
path.parse::<Path>()
.expect("failed to parse crate path in the attribute"),
),
_ => None,
});
if let Some(path) = crate_path {
quote!(#path)
} else {
quote!(exonum)
}
}
fn get_exonum_attributes(attrs: &[Attribute]) -> Vec<Meta> {
let exonum_meta = attrs
.iter()
.find_map(|attr| attr.parse_meta().ok().filter(|m| m.name() == "exonum"));
match exonum_meta {
Some(Meta::List(MetaList { nested: list, .. })) => list
.into_iter()
.filter_map(|n| match n {
NestedMeta::Meta(meta) => Some(meta),
_ => None,
})
.collect(),
Some(_) => panic!("`exonum` attribute should contain list of name value pairs"),
None => vec![],
}
}
fn get_exonum_name_value_attributes(attrs: &[Attribute]) -> Vec<MetaNameValue> {
get_exonum_attributes(attrs)
.into_iter()
.filter_map(|meta| match meta {
Meta::NameValue(name_value) => Some(name_value),
_ => None,
})
.collect()
}
fn find_exonum_word_attribute(attrs: &[Attribute], ident_name: &str) -> bool {
get_exonum_attributes(attrs).iter().any(|meta| match meta {
Meta::Word(ident) if ident == ident_name => true,
_ => false,
})
}