blob: 5b20c4c3896c5d80973a049230040866a893f3a5 [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 Tolnay9542f222020-03-13 13:55:28 -070037 "isize" => Some(Isize),
David Tolnay3383ae72020-03-13 01:12:26 -070038 "f32" => Some(F32),
39 "f64" => Some(F64),
David Tolnay7db73692019-10-20 14:51:12 -040040 "CxxString" => Some(CxxString),
41 "String" => Some(RustString),
42 _ => None,
43 }
44 }
45}
David Tolnayba5eb2d2020-03-06 09:58:20 -080046
David Tolnaya4596c42020-05-10 22:55:45 -070047impl AsRef<str> for Atom {
48 fn as_ref(&self) -> &str {
49 use self::Atom::*;
50 match self {
51 Bool => "bool",
52 U8 => "u8",
53 U16 => "u16",
54 U32 => "u32",
55 U64 => "u64",
56 Usize => "usize",
57 I8 => "i8",
58 I16 => "i16",
59 I32 => "i32",
60 I64 => "i64",
61 Isize => "isize",
62 F32 => "f32",
63 F64 => "f64",
64 CxxString => "CxxString",
65 RustString => "String",
66 }
David Tolnayba5eb2d2020-03-06 09:58:20 -080067 }
68}
69
70impl PartialEq<Atom> for Type {
71 fn eq(&self, atom: &Atom) -> bool {
72 match self {
73 Type::Ident(ident) => ident == atom,
74 _ => false,
75 }
76 }
77}
David Tolnay438e2602020-03-06 10:21:35 -080078
79impl PartialEq<Atom> for &Ident {
80 fn eq(&self, atom: &Atom) -> bool {
81 *self == atom
82 }
83}
84
85impl PartialEq<Atom> for &Type {
86 fn eq(&self, atom: &Atom) -> bool {
87 *self == atom
88 }
89}