blob: a8b6bde548595f98ea475934a3f41de53dd022ca [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 Tolnaya6a9e942020-11-27 17:22:35 -080014 Default,
David Tolnay21466df2020-11-27 14:04:42 -080015 Eq,
David Tolnay577135e2020-11-27 16:23:53 -080016 Ord,
David Tolnay21466df2020-11-27 14:04:42 -080017 PartialEq,
David Tolnay577135e2020-11-27 16:23:53 -080018 PartialOrd,
David Tolnay64703b42020-05-10 22:12:33 -070019}
20
21impl Derive {
22 pub fn from(ident: &Ident) -> Option<Self> {
David Tolnayb247df12020-11-27 12:06:12 -080023 let what = match ident.to_string().as_str() {
24 "Clone" => Trait::Clone,
25 "Copy" => Trait::Copy,
David Tolnayf84c98b2020-11-27 12:59:42 -080026 "Debug" => Trait::Debug,
David Tolnaya6a9e942020-11-27 17:22:35 -080027 "Default" => Trait::Default,
David Tolnay21466df2020-11-27 14:04:42 -080028 "Eq" => Trait::Eq,
David Tolnay577135e2020-11-27 16:23:53 -080029 "Ord" => Trait::Ord,
David Tolnay21466df2020-11-27 14:04:42 -080030 "PartialEq" => Trait::PartialEq,
David Tolnay577135e2020-11-27 16:23:53 -080031 "PartialOrd" => Trait::PartialOrd,
David Tolnayb247df12020-11-27 12:06:12 -080032 _ => return None,
33 };
34 let span = ident.span();
35 Some(Derive { what, span })
36 }
37}
38
39impl PartialEq<Trait> for Derive {
40 fn eq(&self, other: &Trait) -> bool {
41 self.what == *other
David Tolnay64703b42020-05-10 22:12:33 -070042 }
43}
David Tolnaybc047bb2020-11-27 14:30:12 -080044
David Tolnayb600e4f2020-11-27 17:26:49 -080045impl AsRef<str> for Trait {
46 fn as_ref(&self) -> &str {
47 match self {
48 Trait::Clone => "Clone",
49 Trait::Copy => "Copy",
50 Trait::Debug => "Debug",
David Tolnaya6a9e942020-11-27 17:22:35 -080051 Trait::Default => "Default",
David Tolnayb600e4f2020-11-27 17:26:49 -080052 Trait::Eq => "Eq",
53 Trait::Ord => "Ord",
54 Trait::PartialEq => "PartialEq",
55 Trait::PartialOrd => "PartialOrd",
56 }
57 }
58}
59
David Tolnaybc047bb2020-11-27 14:30:12 -080060pub fn contains(derives: &[Derive], query: Trait) -> bool {
61 derives.iter().any(|derive| derive.what == query)
62}