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
use syn::{self, Field, Ident, Meta};
use options::{Core, DefaultExpression, ForwardAttrs, ParseAttribute, ParseData};
use util::PathList;
use {FromMeta, Result};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OuterFrom {
pub ident: Option<Ident>,
pub attrs: Option<Ident>,
pub container: Core,
pub attr_names: PathList,
pub forward_attrs: Option<ForwardAttrs>,
pub from_ident: bool,
}
impl OuterFrom {
pub fn start(di: &syn::DeriveInput) -> Self {
OuterFrom {
container: Core::start(di),
attrs: Default::default(),
ident: Default::default(),
attr_names: Default::default(),
forward_attrs: Default::default(),
from_ident: Default::default(),
}
}
}
impl ParseAttribute for OuterFrom {
fn parse_nested(&mut self, mi: &Meta) -> Result<()> {
let path = mi.path();
if path.is_ident("attributes") {
self.attr_names = FromMeta::from_meta(mi)?;
} else if path.is_ident("forward_attrs") {
self.forward_attrs = FromMeta::from_meta(mi)?;
} else if path.is_ident("from_ident") {
self.container.default = Some(DefaultExpression::Trait);
self.from_ident = true;
} else {
return self.container.parse_nested(mi)
}
Ok(())
}
}
impl ParseData for OuterFrom {
fn parse_field(&mut self, field: &Field) -> Result<()> {
match field
.ident
.as_ref()
.map(|v| v.to_string())
.as_ref()
.map(|v| v.as_str())
{
Some("ident") => {
self.ident = field.ident.clone();
Ok(())
}
Some("attrs") => {
self.attrs = field.ident.clone();
Ok(())
}
_ => self.container.parse_field(field),
}
}
}