blob: d258cc7b5e162efd8098c0b2ff523899a3c8885b [file] [log] [blame]
David Tolnayb247df12020-11-27 12:06:12 -08001use proc_macro2::{Ident, Span};
2
3#[derive(Copy, Clone)]
4pub struct Derive {
5 pub what: Trait,
6 pub span: Span,
7}
David Tolnay64703b42020-05-10 22:12:33 -07008
9#[derive(Copy, Clone, PartialEq)]
David Tolnayb247df12020-11-27 12:06:12 -080010pub enum Trait {
David Tolnay64703b42020-05-10 22:12:33 -070011 Clone,
12 Copy,
David Tolnayf84c98b2020-11-27 12:59:42 -080013 Debug,
David Tolnay21466df2020-11-27 14:04:42 -080014 Eq,
15 PartialEq,
David Tolnay64703b42020-05-10 22:12:33 -070016}
17
18impl Derive {
19 pub fn from(ident: &Ident) -> Option<Self> {
David Tolnayb247df12020-11-27 12:06:12 -080020 let what = match ident.to_string().as_str() {
21 "Clone" => Trait::Clone,
22 "Copy" => Trait::Copy,
David Tolnayf84c98b2020-11-27 12:59:42 -080023 "Debug" => Trait::Debug,
David Tolnay21466df2020-11-27 14:04:42 -080024 "Eq" => Trait::Eq,
25 "PartialEq" => Trait::PartialEq,
David Tolnayb247df12020-11-27 12:06:12 -080026 _ => return None,
27 };
28 let span = ident.span();
29 Some(Derive { what, span })
30 }
31}
32
33impl PartialEq<Trait> for Derive {
34 fn eq(&self, other: &Trait) -> bool {
35 self.what == *other
David Tolnay64703b42020-05-10 22:12:33 -070036 }
37}
David Tolnaybc047bb2020-11-27 14:30:12 -080038
39pub fn contains(derives: &[Derive], query: Trait) -> bool {
40 derives.iter().any(|derive| derive.what == query)
41}