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
use crate::FieldDefs;
use macro_compose::{Collector, Context, Lint};
use syn::{Data, DeriveInput, Error, ItemStruct};
pub struct StructLint<'a> {
struct_defs: &'a FieldDefs<'a>,
fields_defs: &'a FieldDefs<'a>,
}
impl<'a> StructLint<'a> {
pub const fn new(struct_defs: &'a FieldDefs<'a>, fields_defs: &'a FieldDefs<'a>) -> Self {
StructLint {
struct_defs,
fields_defs,
}
}
}
impl Lint<ItemStruct> for StructLint<'_> {
fn lint(&self, input: &ItemStruct, c: &mut Collector) {
let derive_input = DeriveInput::from(input.clone());
let mut subcontext = Context::new_by_ref(c, &derive_input);
subcontext.lint(self);
}
}
impl Lint<DeriveInput> for StructLint<'_> {
fn lint(&self, input: &DeriveInput, c: &mut Collector) {
let mut subcontext = Context::new_by_ref(c, &input.attrs);
subcontext.lint(self.struct_defs);
match &input.data {
Data::Struct(s) => {
for field in s.fields.iter() {
let mut subcontext = Context::new_by_ref(c, &field.attrs);
subcontext.lint(self.fields_defs);
}
}
_ => c.error(Error::new_spanned(input, "expected a struct")),
}
}
}