blob: 0b3fd75f0c82c5f8df4c8f5506bc556c466e4efa [file] [log] [blame]
David Tolnayba5eb2d2020-03-06 09:58:20 -08001use crate::syntax::Type;
David Tolnay7db73692019-10-20 14:51:12 -04002use proc_macro2::Ident;
3
4#[derive(Copy, Clone, PartialEq)]
5pub enum Atom {
6 Bool,
7 U8,
8 U16,
9 U32,
10 U64,
11 Usize,
12 I8,
13 I16,
14 I32,
15 I64,
16 Isize,
David Tolnay3383ae72020-03-13 01:12:26 -070017 F32,
18 F64,
David Tolnay7db73692019-10-20 14:51:12 -040019 CxxString,
20 RustString,
21}
22
23impl Atom {
24 pub fn from(ident: &Ident) -> Option<Self> {
25 use self::Atom::*;
26 match ident.to_string().as_str() {
27 "bool" => Some(Bool),
28 "u8" => Some(U8),
29 "u16" => Some(U16),
30 "u32" => Some(U32),
31 "u64" => Some(U64),
32 "usize" => Some(Usize),
33 "i8" => Some(I8),
34 "i16" => Some(I16),
35 "i32" => Some(I32),
36 "i64" => Some(I64),
David Tolnay3383ae72020-03-13 01:12:26 -070037 "f32" => Some(F32),
38 "f64" => Some(F64),
David Tolnay7db73692019-10-20 14:51:12 -040039 "isize" => Some(Isize),
40 "CxxString" => Some(CxxString),
41 "String" => Some(RustString),
42 _ => None,
43 }
44 }
45}
David Tolnayba5eb2d2020-03-06 09:58:20 -080046
47impl PartialEq<Atom> for Ident {
48 fn eq(&self, atom: &Atom) -> bool {
49 Atom::from(self) == Some(*atom)
50 }
51}
52
53impl PartialEq<Atom> for Type {
54 fn eq(&self, atom: &Atom) -> bool {
55 match self {
56 Type::Ident(ident) => ident == atom,
57 _ => false,
58 }
59 }
60}
David Tolnay438e2602020-03-06 10:21:35 -080061
62impl PartialEq<Atom> for &Ident {
63 fn eq(&self, atom: &Atom) -> bool {
64 *self == atom
65 }
66}
67
68impl PartialEq<Atom> for &Type {
69 fn eq(&self, atom: &Atom) -> bool {
70 *self == atom
71 }
72}