blob: 4522e68652284a6b8c7e9dfec504983e616cc36e [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 Tolnay64703b42020-05-10 22:12:33 -070014}
15
16impl Derive {
17 pub fn from(ident: &Ident) -> Option<Self> {
David Tolnayb247df12020-11-27 12:06:12 -080018 let what = match ident.to_string().as_str() {
19 "Clone" => Trait::Clone,
20 "Copy" => Trait::Copy,
David Tolnayf84c98b2020-11-27 12:59:42 -080021 "Debug" => Trait::Debug,
David Tolnayb247df12020-11-27 12:06:12 -080022 _ => return None,
23 };
24 let span = ident.span();
25 Some(Derive { what, span })
26 }
27}
28
29impl PartialEq<Trait> for Derive {
30 fn eq(&self, other: &Trait) -> bool {
31 self.what == *other
David Tolnay64703b42020-05-10 22:12:33 -070032 }
33}
David Tolnaybc047bb2020-11-27 14:30:12 -080034
35pub fn contains(derives: &[Derive], query: Trait) -> bool {
36 derives.iter().any(|derive| derive.what == query)
37}