-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.rs
More file actions
308 lines (258 loc) · 10.2 KB
/
lib.rs
File metadata and controls
308 lines (258 loc) · 10.2 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use proc_macro2::TokenStream;
use syn::{Attribute, AttrStyle, Data, DataStruct, DeriveInput, Field, Fields, Ident, Meta, Token};
use syn::punctuated::Punctuated;
use quote::quote;
use std::collections::HashMap;
const INSCRIBE_LENGTH: usize = 64;
const INSCRIBE_HANDLING_IDENT: &str = "inscribe";
const INSCRIBE_ADDL_IDENT: &str = "inscribe_addl";
const INSCRIBE_MARK_IDENT: &str = "inscribe_mark";
const INSCRIBE_NAME_IDENT: &str = "inscribe_name";
const SKIP_IDENT: &str = "skip";
const SERIALIZE_IDENT: &str = "serialize";
const RECURSE_IDENT: &str = "recurse";
// The three derive options for each struct member: inscribe it, serialize it, or skip it.
enum Handling {
Recurse,
Serialize,
Skip
}
struct MemberInfo {
handling: Handling,
name_ident: Ident,
sort_ident: Ident,
}
fn parse_contained_ident(attr: &Attribute) -> Option<Ident> {
// Get the nested attribute data
let nested = match attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated) {
Ok(parse_result) => parse_result,
Err(_) => { return None; },
};
// This was originally a for loop, but clippy noted that it never actually loops, so it
// has been replaced with an if-let construction. This may be something to watch if the
// metadata API changes.
if let Some(meta) = nested.iter().next() {
match meta {
Meta::Path(path) => { return Some(path.get_ident().unwrap().clone()); },
_ => { },
}
};
None
}
fn get_member_info(field: &Field) -> MemberInfo {
// By default: handling is recursive, and the name is the field name
let mut member_handling = Handling::Recurse;
let mut found_handling: bool = false;
let mut found_name: bool = false;
let mut sort_name = match field.ident.clone() {
Some(k) => k,
None => { panic!("Couldn't get field name"); }
};
// Run over all the attributes
for attr in field.clone().attrs {
// Skip inner attributes
if let AttrStyle::Inner(_) = attr.style { continue; }
// Don't process attributes we don't care about
if !attr.path().is_ident(INSCRIBE_HANDLING_IDENT) &&
!attr.path().is_ident(INSCRIBE_NAME_IDENT) {
continue;
}
// Parse out whatever is inside the attribute
let inside = match parse_contained_ident(&attr) {
Some(ident) => ident,
None => { panic!("Failed to parse member attribute for Inscribe trait"); }
};
// Get handling specifications
if attr.path().is_ident(INSCRIBE_HANDLING_IDENT) {
// Don't process the same handling twice
if found_handling {
panic!("Inscribe handling attribute defined more than once");
}
if inside.to_string() == String::from(SKIP_IDENT) {
member_handling = Handling::Skip;
} else if inside.to_string() == String::from(SERIALIZE_IDENT) {
member_handling = Handling::Serialize;
} else if inside.to_string() == String::from(RECURSE_IDENT) {
member_handling = Handling::Recurse;
} else {
panic!("Invalid handling specification");
}
found_handling = true;
continue;
}
// Get sorting name
if attr.path().is_ident(INSCRIBE_NAME_IDENT) {
// Don't process the name twice
if found_name {
panic!("Inscribe name attribute defined more than once");
}
sort_name = inside.clone();
found_name = true;
continue;
}
}
MemberInfo {
name_ident: field.ident.clone().unwrap(),
sort_ident: sort_name,
handling: member_handling
}
}
fn implement_get_inscription(dstruct: &DataStruct) -> TokenStream {
let members = match dstruct.fields.clone() {
Fields::Named(a) => a,
_ => { panic!("Invalid struct type"); }
};
// Build hash table to match each of the struct member names to an associated MemberInfo
// struct
let mut member_table: HashMap<String, MemberInfo> = HashMap::new();
let mut member_vec: Vec<String> = Vec::new();
for field in members.named.iter() {
let member_info = get_member_info(&field);
let sort_name_str = member_info.sort_ident.to_string();
member_table.insert(sort_name_str.clone(), member_info);
member_vec.push(sort_name_str);
}
// Now run through the elements in sorted order
let mut center = quote!{};
member_vec.sort();
for sort_name in member_vec.iter() {
let current_member = member_table.get(sort_name).unwrap(); // Guaranteed to work
let member_ident = current_member.name_ident.clone();
let elt = match current_member.handling {
Handling::Recurse => quote!{
let sub_inscription = self.#member_ident.get_inscription()?;
hasher.update(sub_inscription.as_slice());
},
Handling::Serialize => quote!{
serial_out = match bcs::to_bytes(&self.#member_ident) {
Ok(bvec) => bvec,
_ => { return Err(decree::error::Error::new_general("Could not serialize Value")); },
};
hasher.update(serial_out.as_slice());
},
Handling::Skip => quote!{}, // Add nothing to the process
};
// Integrate the hash update string into the overall routine
center = quote!{
#center
#elt
}
}
// Now that we have all the relevant hash update lines in #center, we slap in in the middle
// of a routine that sets up the various temporary values and performs the final hash
// computation.
quote! {
fn get_inscription(&self) -> Result<Vec<u8>, decree::error::Error> {
use tiny_keccak::TupleHash;
use tiny_keccak::Hasher;
use bcs;
use serde::Serialize;
use decree::inscribe::InscribeBuffer;
use decree::decree::FSInput;
let mut serial_out: Vec<u8> = Vec::new();
let mut hasher = TupleHash::v256(Self::MARK.as_bytes());
// Add the struct members into the TupleHash
#center
// Add the final additional data
let additional = self.get_additional()?;
hasher.update(additional.as_slice());
let mut hash_buf: InscribeBuffer = [0u8; #INSCRIBE_LENGTH];
hasher.finalize(&mut hash_buf);
Ok(hash_buf.to_vec())
}
}
}
fn implement_default_mark(ast: &DeriveInput) -> TokenStream {
// By default, the mark/identifier for a struct will be its name
let ident = &ast.ident;
let ident_str = ident.to_string();
let mark = quote!{
const MARK: &'static str = #ident_str;
};
mark
}
fn implement_get_addl(ast: &DeriveInput) -> TokenStream {
// In the absence of an outer attribute, we use the default implementation
let mut addl_implementation: TokenStream = quote!{};
// Check the outer attributes for something like `#[inscribe_addl(addl_function)]`
for attr in &ast.attrs {
// We only look for "inscribe" attributes
if !attr.path().is_ident(INSCRIBE_ADDL_IDENT) { continue; }
let nested = match attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated) {
Ok(parse_result) => {
parse_result
},
Err(_) => { panic!("Failed to parse inscribe_addl field attribute"); }
};
if let Some(meta) = nested.iter().next() {
match meta {
Meta::Path(path) => { addl_implementation = quote!{
fn get_additional(&self) -> Result<Vec<u8>, decree::error::Error> {
self.#path()
}
}},
_ => { panic!("Invalid metadata for field attribute"); },
}
}
break;
}
addl_implementation
}
fn implement_get_mark(ast: &DeriveInput) -> TokenStream {
let mut found_mark: bool = false;
let mut mark_implementation: TokenStream = quote!{};
// Check the outer attributes for something like `#[inscribe_mark(mark_function)]`
for attr in &ast.attrs {
// We only look for "inscribe" attributes
if !attr.path().is_ident(INSCRIBE_MARK_IDENT) { continue; }
let nested = match attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated) {
Ok(parse_result) => {
parse_result
},
Err(_) => { panic!("Failed to parse inscribe_mark field attribute"); }
};
if let Some(meta) = nested.iter().next() {
match meta {
Meta::Path(path) => { mark_implementation = quote!{
const MARK: &'static str = #path;
}},
_ => { panic!("Invalid metadata for field attribute"); },
}
}
found_mark = true;
break;
}
if found_mark {
mark_implementation
} else {
implement_default_mark(ast)
}
}
fn implement_inscribe_trait(ast: DeriveInput, dstruct: &DataStruct) -> TokenStream {
let get_mark: TokenStream = implement_get_mark(&ast);
let get_inscr: TokenStream = implement_get_inscription(dstruct);
let get_addl: TokenStream = implement_get_addl(&ast);
let ident = ast.ident;
let generics = ast.generics;
quote! {
impl #generics Inscribe for #ident #generics {
#get_mark
#get_inscr
#get_addl
}
}
}
#[proc_macro_derive(Inscribe, attributes(inscribe, inscribe_addl, inscribe_mark, inscribe_name))]
pub fn inscribe_derive(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast: DeriveInput = syn::parse(item.clone()).unwrap();
// We don't support for derive for anything but structs
let dstruct = match ast.clone().data {
Data::Struct(d) => d,
_ => { panic!("Invalid type for derive(Inscribe)")},
};
// We don't support unnamed structs
if !matches!(dstruct.fields, Fields::Named(_)) {
panic!("Unnamed structs not supported for derive(Inscribe)");
}
implement_inscribe_trait(ast, &dstruct).into()
}