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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! Individual implementation for all traits.

mod clone;
mod common_ord;
mod copy;
mod debug;
mod default;
mod eq;
mod hash;
mod ord;
mod partial_eq;
mod partial_ord;
#[cfg(feature = "zeroize")]
mod zeroize;
#[cfg(feature = "zeroize")]
mod zeroize_on_drop;

use proc_macro2::{Span, TokenStream};
use syn::{punctuated::Punctuated, spanned::Spanned, Meta, Path, Result, Token, TypeParamBound};

use crate::{Data, DeriveTrait, Error, Item};

/// Type implementing [`TraitImpl`] for every trait.
#[derive(Clone, Copy, Eq, PartialEq)]
#[cfg_attr(test, derive(Debug))]
pub enum Trait {
	/// [`Clone`].
	Clone,
	/// [`Copy`].
	Copy,
	/// [`Debug`](std::fmt::Debug).
	Debug,
	/// [`Default`].
	Default,
	/// [`Eq`].
	Eq,
	/// [`Hash`](std::hash::Hash).
	Hash,
	/// [`Ord`].
	Ord,
	/// [`PartialEq`].
	PartialEq,
	/// [`PartialOrd`].
	PartialOrd,
	/// [`Zeroize`](https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html).
	#[cfg(feature = "zeroize")]
	Zeroize,
	/// [`ZeroizeOnDrop`](https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html).
	#[cfg(feature = "zeroize")]
	ZeroizeOnDrop,
}

impl Trait {
	/// Return dummy-struct for the internal implementation.
	fn implementation(&self) -> &dyn TraitImpl {
		match self {
			Trait::Clone => &clone::Clone,
			Trait::Copy => &copy::Copy,
			Trait::Debug => &debug::Debug,
			Trait::Default => &default::Default,
			Trait::Eq => &eq::Eq,
			Trait::Hash => &hash::Hash,
			Trait::Ord => &ord::Ord,
			Trait::PartialEq => &partial_eq::PartialEq,
			Trait::PartialOrd => &partial_ord::PartialOrd,
			#[cfg(feature = "zeroize")]
			Trait::Zeroize => &zeroize::Zeroize,
			#[cfg(feature = "zeroize")]
			Trait::ZeroizeOnDrop => &zeroize_on_drop::ZeroizeOnDrop,
		}
	}

	/// Create [`Trait`] from [`Path`].
	pub fn from_path(path: &Path) -> Result<Self> {
		if let Some(ident) = path.get_ident() {
			use Trait::*;

			match ident.to_string().as_str() {
				"Clone" => Ok(Clone),
				"Copy" => Ok(Copy),
				"Debug" => Ok(Debug),
				"Default" => Ok(Default),
				"Eq" => Ok(Eq),
				"Hash" => Ok(Hash),
				"Ord" => Ok(Ord),
				"PartialEq" => Ok(PartialEq),
				"PartialOrd" => Ok(PartialOrd),
				#[cfg(feature = "zeroize")]
				"Zeroize" => Ok(Zeroize),
				#[cfg(feature = "zeroize")]
				"ZeroizeOnDrop" => Ok(ZeroizeOnDrop),
				"crate" => Err(Error::crate_(path.span())),
				_ => Err(Error::trait_(path.span())),
			}
		} else {
			Err(Error::trait_(path.span()))
		}
	}
}

impl TraitImpl for Trait {
	fn as_str(&self) -> &'static str {
		self.implementation().as_str()
	}

	fn default_derive_trait(&self) -> DeriveTrait {
		self.implementation().default_derive_trait()
	}

	fn parse_derive_trait(
		&self,
		span: Span,
		list: Punctuated<Meta, Token![,]>,
	) -> Result<DeriveTrait> {
		self.implementation().parse_derive_trait(span, list)
	}

	fn supports_union(&self) -> bool {
		self.implementation().supports_union()
	}

	fn additional_where_bounds(&self, data: &Item) -> Option<TypeParamBound> {
		self.implementation().additional_where_bounds(data)
	}

	fn additional_impl(&self, trait_: &DeriveTrait) -> Option<(Path, TokenStream)> {
		self.implementation().additional_impl(trait_)
	}

	fn impl_path(&self, trait_: &DeriveTrait) -> Path {
		self.implementation().impl_path(trait_)
	}

	fn build_signature(
		&self,
		item: &Item,
		traits: &[DeriveTrait],
		trait_: &DeriveTrait,
		body: &TokenStream,
	) -> TokenStream {
		self.implementation()
			.build_signature(item, traits, trait_, body)
	}

	fn build_body(&self, traits: &[DeriveTrait], trait_: &DeriveTrait, data: &Data) -> TokenStream {
		self.implementation().build_body(traits, trait_, data)
	}
}

/// Single trait implementation. Parses attributes and constructs `impl`s.
pub trait TraitImpl {
	/// [`str`] representation of this [`Trait`].
	/// Used to compare against [`Ident`](struct@syn::Ident)s and create error
	/// messages.
	fn as_str(&self) -> &'static str;

	/// Associated [`DeriveTrait`].
	fn default_derive_trait(&self) -> DeriveTrait;

	/// Parse a `derive_where` trait with it's options.
	fn parse_derive_trait(
		&self,
		span: Span,
		_list: Punctuated<Meta, Token![,]>,
	) -> Result<DeriveTrait> {
		Err(Error::options(span, self.as_str()))
	}

	/// Returns `true` if [`Trait`] supports unions.
	fn supports_union(&self) -> bool {
		false
	}

	/// Additional bounds to add to [`WhereClause`](syn::WhereClause).
	fn additional_where_bounds(&self, _data: &Item) -> Option<TypeParamBound> {
		None
	}

	/// Additional implementation to add for this [`Trait`].
	fn additional_impl(&self, _trait_: &DeriveTrait) -> Option<(Path, TokenStream)> {
		None
	}

	/// Trait to implement. Only used for [`ZeroizeOnDrop`](https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html)
	/// because it implements [`Drop`] and not itself.
	fn impl_path(&self, trait_: &DeriveTrait) -> Path {
		trait_.path()
	}

	/// Build method signature for this [`Trait`].
	fn build_signature(
		&self,
		_item: &Item,
		_traits: &[DeriveTrait],
		_trait_: &DeriveTrait,
		_body: &TokenStream,
	) -> TokenStream {
		TokenStream::new()
	}

	/// Build method body for this [`Trait`].
	fn build_body(
		&self,
		_traits: &[DeriveTrait],
		_trait_: &DeriveTrait,
		_data: &Data,
	) -> TokenStream {
		TokenStream::new()
	}
}