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
use proc_macro2::Span;
use syn::{spanned::Spanned, Meta, Result};
use crate::{DeriveWhere, Error, Trait};
#[derive(Clone, Copy, Default)]
#[cfg_attr(test, derive(Debug))]
pub struct Default(pub Option<Span>);
impl Default {
pub const DEFAULT: &'static str = "default";
pub fn add_attribute(&mut self, meta: &Meta, derive_wheres: &[DeriveWhere]) -> Result<()> {
debug_assert!(meta.path().is_ident(Self::DEFAULT));
if let Meta::Path(path) = meta {
if self.0.is_some() {
Err(Error::option_duplicate(path.span(), Self::DEFAULT))
} else {
let mut impl_default = false;
for derive_where in derive_wheres {
if derive_where.trait_(&Trait::Default).is_some() {
impl_default = true;
break;
}
}
if impl_default {
self.0 = Some(path.span());
Ok(())
} else {
Err(Error::default(path.span()))
}
}
} else {
Err(Error::option_syntax(meta.span()))
}
}
}