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
//! Attribute parsing for the `skip` and `skip_inner` options.

use std::default::Default;

use syn::{spanned::Spanned, Meta, NestedMeta, Result};

use crate::{DeriveWhere, Error, Trait, TraitImpl};

/// Stores what [`Trait`]s to skip this field or variant for.
#[cfg_attr(test, derive(Debug))]
pub enum Skip {
	/// Field skipped for no [`Trait`].
	None,
	/// Field skipped for all [`Trait`]s that support it.
	All,
	/// Field skipped for the [`Trait`]s listed.
	Traits(Vec<Trait>),
}

impl Default for Skip {
	fn default() -> Self {
		Skip::None
	}
}

impl Skip {
	/// Token used for the `skip` option.
	pub const SKIP: &'static str = "skip";
	/// Token used for the `skip_inner` option.
	pub const SKIP_INNER: &'static str = "skip_inner";

	/// Returns `true` if variant is [`Skip::None`].
	pub fn is_none(&self) -> bool {
		// MSRV: `matches!` was added in 1.42.0.
		#[allow(clippy::match_like_matches_macro)]
		{
			if let Skip::None = self {
				true
			} else {
				false
			}
		}
	}

	/// Adds a [`Meta`] to this [`Skip`].
	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) => {
				// Check for duplicates.
				if self.is_none() {
					// Check against parent `skip_inner`.
					match skip_inner {
						// Allow `Skip::All` on field if parent has a tighter constraint.
						Some(Skip::None) | Some(Skip::Traits(..)) | None => {
							// Don't allow to skip all traits if no trait to be implemented supports
							// skipping.
							if derive_wheres
								.iter()
								.any(|derive_where| derive_where.any_skip())
							{
								*self = Skip::All;
								Ok(())
							} else {
								Err(Error::option_skip_no_trait(path.span()))
							}
						}
						// Don't allow `Skip::All` on field if parent already covers it.
						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) => {
				// Don't allow an empty list.
				if list.nested.is_empty() {
					return Err(Error::option_empty(list.span()));
				}

				// Get traits already set to be skipped.
				let traits = match self {
					// If no traits are set, change to empty `Skip::Traits` and return that.
					Skip::None => {
						// MSRV: Could have used `Option::insert`, but it is only available in 1.53.
						*self = Skip::Traits(Vec::new());

						if let Skip::Traits(traits) = self {
							traits
						} else {
							unreachable!("unexpected variant")
						}
					}
					// If we are already skipping all traits, we can't skip again with constraints.
					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)?;

						// Don't allow unsupported traits to be skipped.
						if trait_.supports_skip() {
							// Don't allow to skip the same trait twice.
							if traits.contains(&trait_) {
								return Err(Error::option_skip_duplicate(
									path.span(),
									trait_.as_str(),
								));
							} else {
								// Don't allow to skip a trait already set to be skipped in the
								// parent.
								match skip_inner {
									Some(skip_inner) if skip_inner.skip(&trait_) => {
										return Err(Error::option_skip_inner(path.span()))
									}
									_ => {
										// Don't allow to skip trait that isn't being implemented.
										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())),
		}
	}

	/// Returns `true` if this item, variant or field is skipped with the given
	/// [`Trait`].
	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
			}
		}
	}
}