-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathattribute.rs
172 lines (153 loc) · 5.81 KB
/
attribute.rs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// TODO: nice error messages instead of panics, see: https://stackoverflow.com/a/54394014/2867076
use std::collections::HashMap;
use proc_macro2::{
TokenStream,
TokenTree,
};
use syn::{
AttrStyle,
Attribute,
Meta,
};
use synstructure::BindingInfo;
fn parse_attrs(s: &synstructure::Structure) -> HashMap<String, TokenStream> {
let mut attrs = HashMap::new();
for attr in &s.ast().attrs {
if attr.style != AttrStyle::Outer {
continue
}
if let Meta::List(ml) = &attr.meta {
if ml.path.segments.len() == 1 && ml.path.segments[0].ident == "canonical" {
let mut tt = ml.tokens.clone().into_iter().peekable();
if let Some(key) = tt.next() {
let key = key.to_string();
if let Some(eq_sign) = tt.peek() {
if eq_sign.to_string() == "=" {
let _ = tt.next();
}
} else {
// Single token, no `=`, so it's a boolean flag.
let old = attrs.insert(key.clone(), TokenStream::new());
if old.is_some() {
panic!("duplicate canonical attribute: {}", key);
}
continue
}
// Key-value pair
let value = TokenStream::from_iter(tt);
let old = attrs.insert(key.clone(), value);
if old.is_some() {
panic!("duplicate canonical attribute: {}", key);
}
continue
}
panic!("enum-level canonical attribute must be a `key = value` pair");
}
}
}
attrs
}
/// Pop-level `canonical` attributes for a struct
pub struct StructAttrs {
/// The struct is prefixed with the given word.
/// Useful with`#[canonical(inner_discriminant)]`.
pub prefix: Option<TokenStream>,
}
impl StructAttrs {
pub fn parse(s: &synstructure::Structure) -> Self {
let mut attrs = parse_attrs(s);
let prefix = attrs.remove("prefix");
if !attrs.is_empty() {
panic!("unknown canonical attributes: {:?}", attrs.keys())
}
Self { prefix }
}
}
/// Pop-level `canonical` attributes for an enum
#[allow(non_snake_case)]
pub struct EnumAttrs {
/// Use a custom type as a discriminant. Mapped using `TryFromPrimitive` and `Into`.
pub discriminant: Option<TokenStream>,
/// Same as `discriminant`, but each field is prefixed with the discriminant,
/// so it's not removed when the enum itself is deserialized.
pub inner_discriminant: Option<TokenStream>,
/// Replaces calculation of the serialized static size with a custom function.
pub serialized_size_static_with: Option<TokenStream>,
/// Replaces calculation of the serialized dynamic size with a custom function.
pub serialized_size_dynamic_with: Option<TokenStream>,
/// Replaces serialization with a custom function.
pub serialize_with: Option<TokenStream>,
/// Replaces deserialization with a custom function.
pub deserialize_with: Option<TokenStream>,
/// Determines whether the enum has a dynamic size when `serialize_with` is used.
pub SIZE_NO_DYNAMIC: Option<TokenStream>,
}
impl EnumAttrs {
#[allow(non_snake_case)]
pub fn parse(s: &synstructure::Structure) -> Self {
let mut attrs = parse_attrs(s);
let discriminant = attrs.remove("discriminant");
let inner_discriminant = attrs.remove("inner_discriminant");
let serialized_size_static_with = attrs.remove("serialized_size_static_with");
let serialized_size_dynamic_with = attrs.remove("serialized_size_dynamic_with");
let serialize_with = attrs.remove("serialize_with");
let deserialize_with = attrs.remove("deserialize_with");
let SIZE_NO_DYNAMIC = attrs.remove("SIZE_NO_DYNAMIC");
if !attrs.is_empty() {
panic!("unknown canonical attributes: {:?}", attrs.keys())
}
Self {
discriminant,
inner_discriminant,
serialized_size_static_with,
serialized_size_dynamic_with,
serialize_with,
deserialize_with,
SIZE_NO_DYNAMIC,
}
}
}
/// Parse `#[repr(int)]` attribute for an enum.
pub fn parse_enum_repr(attrs: &[Attribute]) -> Option<String> {
for attr in attrs {
if attr.style != AttrStyle::Outer {
continue
}
if let Meta::List(ml) = &attr.meta {
if ml.path.segments.len() == 1 && ml.path.segments[0].ident == "repr" {
if let Some(TokenTree::Ident(ident)) =
ml.tokens.clone().into_iter().next()
{
return Some(ident.to_string())
}
}
}
}
None
}
/// Parse `#[canonical(skip)]` attribute for a binding field.
pub fn should_skip_field_binding(binding: &BindingInfo<'_>) -> bool {
should_skip_field(&binding.ast().attrs)
}
/// Parse `#[canonical(skip)]` attribute for a struct field.
pub fn should_skip_field(attrs: &[Attribute]) -> bool {
for attr in attrs {
if attr.style != AttrStyle::Outer {
continue
}
if let Meta::List(ml) = &attr.meta {
if ml.path.segments.len() == 1 && ml.path.segments[0].ident == "canonical" {
for token in ml.tokens.clone() {
if let TokenTree::Ident(ident) = &token {
if ident == "skip" {
return true
} else {
panic!("unknown canonical attribute: {}", ident)
}
}
}
}
}
}
false
}