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
use syn::{Field, DeriveInput, Data, Lit, Meta, Fields, FieldsNamed};
pub fn symbol_name(field: &Field) -> String {
match find_str_attr_val(field, "dlopen_name") {
Some(val) => val,
None =>
match field.ident {
Some(ref val) => val.to_string(),
None => panic!("All structure fields need to be identifiable")
}
}
}
pub fn find_str_attr_val<'a>(field: &'a Field, attr_name: &str) -> Option<String> {
for attr in field.attrs.iter() {
match attr.parse_meta() {
Ok(Meta::NameValue(ref meta)) => {
if meta.ident == attr_name {
return match &meta.lit {
&Lit::Str(ref val, ..) => {
Some(val.value())
},
_ => panic!("{} attribute must be a string", attr_name)
}
}
},
_ => continue
}
}
None
}
pub fn has_marker_attr(field :&Field, attr_name: &str) -> bool {
for attr in field.attrs.iter() {
match attr.parse_meta() {
Ok(Meta::Word(ref val)) => if val == attr_name{
return true;
},
_ => continue
}
}
false
}
pub fn get_fields<'a>(ast: &'a DeriveInput, trait_name: &str) -> &'a FieldsNamed {
let vd = match ast.data {
Data::Enum(_) | Data::Union(_) => panic ! ("{} can be only implemented for structures", trait_name),
Data::Struct( ref val) => val
};
match &vd.fields {
& Fields::Named( ref f) => f,
& Fields::Unnamed(_) | &Fields::Unit => panic ! ("{} can be only implemented for structures", trait_name)
}
}