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
use std::default::Default;
use syn::{spanned::Spanned, Meta, NestedMeta, Result};
use crate::{DeriveWhere, Error, Trait, TraitImpl};
#[cfg_attr(test, derive(Debug))]
pub enum Skip {
None,
All,
Traits(Vec<Trait>),
}
impl Default for Skip {
fn default() -> Self {
Skip::None
}
}
impl Skip {
pub const SKIP: &'static str = "skip";
pub const SKIP_INNER: &'static str = "skip_inner";
pub fn is_none(&self) -> bool {
#[allow(clippy::match_like_matches_macro)]
{
if let Skip::None = self {
true
} else {
false
}
}
}
pub fn add_attribute(
&mut self,
derive_wheres: &[DeriveWhere],
skip_inner: Option<&Skip>,
meta: &Meta,
) -> Result<()> {
debug_assert!(meta.path().is_ident(Self::SKIP) || meta.path().is_ident(Self::SKIP_INNER));
match meta {
Meta::Path(path) => {
if self.is_none() {
match skip_inner {
Some(Skip::None) | Some(Skip::Traits(..)) | None => {
if derive_wheres
.iter()
.any(|derive_where| derive_where.any_skip())
{
*self = Skip::All;
Ok(())
} else {
Err(Error::option_skip_no_trait(path.span()))
}
}
Some(Skip::All) => Err(Error::option_skip_inner(path.span())),
}
} else {
Err(Error::option_duplicate(
path.span(),
&meta
.path()
.get_ident()
.expect("unexpected skip syntax")
.to_string(),
))
}
}
Meta::List(list) => {
if list.nested.is_empty() {
return Err(Error::option_empty(list.span()));
}
let traits = match self {
Skip::None => {
*self = Skip::Traits(Vec::new());
if let Skip::Traits(traits) = self {
traits
} else {
unreachable!("unexpected variant")
}
}
Skip::All => return Err(Error::option_skip_all(list.span())),
Skip::Traits(traits) => traits,
};
for nested_meta in &list.nested {
if let NestedMeta::Meta(Meta::Path(path)) = nested_meta {
let trait_ = Trait::from_path(path)?;
if trait_.supports_skip() {
if traits.contains(&trait_) {
return Err(Error::option_skip_duplicate(
path.span(),
trait_.as_str(),
));
} else {
match skip_inner {
Some(skip_inner) if skip_inner.skip(&trait_) => {
return Err(Error::option_skip_inner(path.span()))
}
_ => {
if derive_wheres.iter().any(|derive_where| {
derive_where.trait_(&trait_).is_some()
}) {
traits.push(trait_)
} else {
return Err(Error::option_skip_trait(path.span()));
}
}
}
}
} else {
return Err(Error::option_skip_support(path.span(), trait_.as_str()));
}
} else {
return Err(Error::option_syntax(nested_meta.span()));
}
}
Ok(())
}
_ => Err(Error::option_syntax(meta.span())),
}
}
pub fn skip(&self, trait_: &Trait) -> bool {
match self {
Skip::None => false,
Skip::All => trait_.supports_skip(),
Skip::Traits(traits) => {
let skip = traits.contains(trait_);
debug_assert!(!skip || trait_.supports_skip());
skip
}
}
}
}