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
use syn;
use {Error, FromMeta, Result};
mod core;
mod forward_attrs;
mod from_derive;
mod from_field;
mod from_meta;
mod from_type_param;
mod from_variant;
mod input_field;
mod input_variant;
mod outer_from;
mod shape;
pub use self::core::Core;
pub use self::forward_attrs::ForwardAttrs;
pub use self::from_derive::FdiOptions;
pub use self::from_field::FromFieldOptions;
pub use self::from_meta::FromMetaOptions;
pub use self::from_type_param::FromTypeParamOptions;
pub use self::from_variant::FromVariantOptions;
pub use self::input_field::InputField;
pub use self::input_variant::InputVariant;
pub use self::outer_from::OuterFrom;
pub use self::shape::{DataShape, Shape};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DefaultExpression {
Inherit,
Explicit(syn::Path),
Trait,
}
#[doc(hidden)]
impl FromMeta for DefaultExpression {
fn from_word() -> Result<Self> {
Ok(DefaultExpression::Trait)
}
fn from_value(value: &syn::Lit) -> Result<Self> {
syn::Path::from_value(value).map(DefaultExpression::Explicit)
}
}
macro_rules! collect_error {
($errors:ident, $task:expr) => {
if let Err(e) = $task {
$errors.push(e);
}
};
}
pub trait ParseAttribute: Sized {
fn parse_attributes(mut self, attrs: &[syn::Attribute]) -> Result<Self> {
let mut errors = Vec::new();
for attr in attrs {
if attr.path == parse_quote!(darling) {
collect_error!(errors, parse_attr(attr, &mut self));
}
}
if !errors.is_empty() {
Err(Error::multiple(errors))
} else {
Ok(self)
}
}
fn parse_nested(&mut self, mi: &syn::Meta) -> Result<()>;
}
fn parse_attr<T: ParseAttribute>(attr: &syn::Attribute, target: &mut T) -> Result<()> {
let mut errors = Vec::new();
match attr.parse_meta().ok() {
Some(syn::Meta::List(data)) => {
for item in data.nested {
if let syn::NestedMeta::Meta(ref mi) = item {
collect_error!(errors, target.parse_nested(mi));
} else {
panic!("Wasn't able to parse: `{:?}`", item);
}
}
if !errors.is_empty() {
Err(Error::multiple(errors))
} else {
Ok(())
}
}
Some(ref item) => panic!("Wasn't able to parse: `{:?}`", item),
None => panic!("Unable to parse {:?}", attr),
}
}
pub trait ParseData: Sized {
fn parse_body(mut self, body: &syn::Data) -> Result<Self> {
use syn::{Data, Fields};
let mut errors = Vec::new();
match *body {
Data::Struct(ref data) => match data.fields {
Fields::Unit => {}
Fields::Named(ref fields) => {
for field in &fields.named {
collect_error!(errors, self.parse_field(field));
}
}
Fields::Unnamed(ref fields) => {
for field in &fields.unnamed {
collect_error!(errors, self.parse_field(field));
}
}
},
Data::Enum(ref data) => {
for variant in &data.variants {
collect_error!(errors, self.parse_variant(variant));
}
}
Data::Union(_) => unreachable!(),
};
if !errors.is_empty() {
Err(Error::multiple(errors))
} else {
Ok(self)
}
}
fn parse_variant(&mut self, variant: &syn::Variant) -> Result<()> {
Err(Error::unsupported_format("enum variant").with_span(variant))
}
fn parse_field(&mut self, field: &syn::Field) -> Result<()> {
Err(Error::unsupported_format("struct field").with_span(field))
}
}