1 use proc_macro2::{Ident, Span};
2 use std::fmt::{self, Display};
3
4 #[derive(Copy, Clone)]
5 pub struct Derive {
6 pub what: Trait,
7 pub span: Span,
8 }
9
10 #[derive(Copy, Clone, PartialEq)]
11 pub enum Trait {
12 Clone,
13 Copy,
14 Debug,
15 Default,
16 Eq,
17 ExternType,
18 Hash,
19 Ord,
20 PartialEq,
21 PartialOrd,
22 }
23
24 impl Derive {
from(ident: &Ident) -> Option<Self>25 pub fn from(ident: &Ident) -> Option<Self> {
26 let what = match ident.to_string().as_str() {
27 "Clone" => Trait::Clone,
28 "Copy" => Trait::Copy,
29 "Debug" => Trait::Debug,
30 "Default" => Trait::Default,
31 "Eq" => Trait::Eq,
32 "ExternType" => Trait::ExternType,
33 "Hash" => Trait::Hash,
34 "Ord" => Trait::Ord,
35 "PartialEq" => Trait::PartialEq,
36 "PartialOrd" => Trait::PartialOrd,
37 _ => return None,
38 };
39 let span = ident.span();
40 Some(Derive { what, span })
41 }
42 }
43
44 impl PartialEq<Trait> for Derive {
eq(&self, other: &Trait) -> bool45 fn eq(&self, other: &Trait) -> bool {
46 self.what == *other
47 }
48 }
49
50 impl AsRef<str> for Trait {
as_ref(&self) -> &str51 fn as_ref(&self) -> &str {
52 match self {
53 Trait::Clone => "Clone",
54 Trait::Copy => "Copy",
55 Trait::Debug => "Debug",
56 Trait::Default => "Default",
57 Trait::Eq => "Eq",
58 Trait::ExternType => "ExternType",
59 Trait::Hash => "Hash",
60 Trait::Ord => "Ord",
61 Trait::PartialEq => "PartialEq",
62 Trait::PartialOrd => "PartialOrd",
63 }
64 }
65 }
66
67 impl Display for Derive {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result68 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
69 formatter.write_str(self.what.as_ref())
70 }
71 }
72
contains(derives: &[Derive], query: Trait) -> bool73 pub fn contains(derives: &[Derive], query: Trait) -> bool {
74 derives.iter().any(|derive| derive.what == query)
75 }
76