-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathfield.rs
More file actions
65 lines (57 loc) · 1.86 KB
/
field.rs
File metadata and controls
65 lines (57 loc) · 1.86 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
use super::attribute::Attribute;
use crate::registrant::attribute;
use quote::ToTokens;
// do not derive debug since this needs "extra-traits"
// feature for crate `syn`, which slows compile time
// too much, and is not needed as this struct is not
// public.
pub struct Field {
ident: syn::Ident,
name: syn::LitStr,
attr: Attribute,
}
impl Field {
pub(super) fn ident(&self) -> &syn::Ident {
&self.ident
}
pub(super) fn name(&self) -> &syn::LitStr {
match &self.attr.rename {
Some(rename) => rename,
None => &self.name,
}
}
pub(super) fn help(&self) -> syn::LitStr {
self.attr
.help
.clone()
.unwrap_or_else(|| syn::LitStr::new("", self.ident.span()))
}
pub(super) fn unit(&self) -> Option<&syn::LitStr> {
self.attr.unit.as_ref()
}
pub(super) fn skip(&self) -> bool {
self.attr.skip
}
}
impl TryFrom<syn::Field> for Field {
type Error = syn::Error;
fn try_from(field: syn::Field) -> Result<Self, Self::Error> {
let ident = field
.ident
.clone()
.expect("Fields::Named should have an identifier");
let name = syn::LitStr::new(&ident.to_string(), ident.span());
let attr = field
.attrs
.into_iter()
// ignore unknown attributes, which might be defined by another derive macros.
.filter(|attr| attr.path().is_ident("doc") || attr.path().is_ident("registrant"))
.try_fold(vec![], |mut acc, attr| {
acc.push(syn::parse2::<Attribute>(attr.meta.into_token_stream())?);
Ok::<Vec<attribute::Attribute>, syn::Error>(acc)
})?
.into_iter()
.try_fold(Attribute::default(), |acc, attr| acc.merge(attr))?;
Ok(Field { ident, name, attr })
}
}