blob: a2ff7b7f0859ec33b92d3b176eaaef3c74cb6fb8 [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;
David Tolnay699351b2020-05-10 22:57:08 -07003use std::fmt::{self, Display};
David Tolnay7db73692019-10-20 14:51:12 -04004
5#[derive(Copy, Clone, PartialEq)]
6pub enum Atom {
7 Bool,
8 U8,
9 U16,
10 U32,
11 U64,
12 Usize,
13 I8,
14 I16,
15 I32,
16 I64,
17 Isize,
David Tolnay3383ae72020-03-13 01:12:26 -070018 F32,
19 F64,
David Tolnay7db73692019-10-20 14:51:12 -040020 CxxString,
21 RustString,
22}
23
24impl Atom {
25 pub fn from(ident: &Ident) -> Option<Self> {
26 use self::Atom::*;
27 match ident.to_string().as_str() {
28 "bool" => Some(Bool),
29 "u8" => Some(U8),
30 "u16" => Some(U16),
31 "u32" => Some(U32),
32 "u64" => Some(U64),
33 "usize" => Some(Usize),
34 "i8" => Some(I8),
35 "i16" => Some(I16),
36 "i32" => Some(I32),
37 "i64" => Some(I64),
David Tolnay9542f222020-03-13 13:55:28 -070038 "isize" => Some(Isize),
David Tolnay3383ae72020-03-13 01:12:26 -070039 "f32" => Some(F32),
40 "f64" => Some(F64),
David Tolnay7db73692019-10-20 14:51:12 -040041 "CxxString" => Some(CxxString),
42 "String" => Some(RustString),
43 _ => None,
44 }
45 }
46}
David Tolnayba5eb2d2020-03-06 09:58:20 -080047
David Tolnay699351b2020-05-10 22:57:08 -070048impl Display for Atom {
49 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
50 formatter.write_str(self.as_ref())
51 }
52}
53
David Tolnaya4596c42020-05-10 22:55:45 -070054impl AsRef<str> for Atom {
55 fn as_ref(&self) -> &str {
56 use self::Atom::*;
57 match self {
58 Bool => "bool",
59 U8 => "u8",
60 U16 => "u16",
61 U32 => "u32",
62 U64 => "u64",
63 Usize => "usize",
64 I8 => "i8",
65 I16 => "i16",
66 I32 => "i32",
67 I64 => "i64",
68 Isize => "isize",
69 F32 => "f32",
70 F64 => "f64",
71 CxxString => "CxxString",
72 RustString => "String",
73 }
David Tolnayba5eb2d2020-03-06 09:58:20 -080074 }
75}
76
77impl PartialEq<Atom> for Type {
78 fn eq(&self, atom: &Atom) -> bool {
79 match self {
80 Type::Ident(ident) => ident == atom,
81 _ => false,
82 }
83 }
84}
David Tolnay438e2602020-03-06 10:21:35 -080085
86impl PartialEq<Atom> for &Ident {
87 fn eq(&self, atom: &Atom) -> bool {
88 *self == atom
89 }
90}
91
92impl PartialEq<Atom> for &Type {
93 fn eq(&self, atom: &Atom) -> bool {
94 *self == atom
95 }
96}