blob: d4ad78f1779cfa0900345ebf50d26470928a123a [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,
David Tolnayb3873dc2020-11-25 19:47:49 -08008 Char, // C char, not Rust char
David Tolnay7db73692019-10-20 14:51:12 -04009 U8,
10 U16,
11 U32,
12 U64,
13 Usize,
14 I8,
15 I16,
16 I32,
17 I64,
18 Isize,
David Tolnay3383ae72020-03-13 01:12:26 -070019 F32,
20 F64,
David Tolnay7db73692019-10-20 14:51:12 -040021 CxxString,
22 RustString,
23}
24
25impl Atom {
26 pub fn from(ident: &Ident) -> Option<Self> {
David Tolnay9bcb4c62020-05-10 17:42:06 -070027 Self::from_str(ident.to_string().as_str())
28 }
29
30 pub fn from_str(s: &str) -> Option<Self> {
David Tolnay7db73692019-10-20 14:51:12 -040031 use self::Atom::*;
David Tolnay9bcb4c62020-05-10 17:42:06 -070032 match s {
David Tolnay7db73692019-10-20 14:51:12 -040033 "bool" => Some(Bool),
David Tolnayb3873dc2020-11-25 19:47:49 -080034 "c_char" => Some(Char),
David Tolnay7db73692019-10-20 14:51:12 -040035 "u8" => Some(U8),
36 "u16" => Some(U16),
37 "u32" => Some(U32),
38 "u64" => Some(U64),
39 "usize" => Some(Usize),
40 "i8" => Some(I8),
41 "i16" => Some(I16),
42 "i32" => Some(I32),
43 "i64" => Some(I64),
David Tolnay9542f222020-03-13 13:55:28 -070044 "isize" => Some(Isize),
David Tolnay3383ae72020-03-13 01:12:26 -070045 "f32" => Some(F32),
46 "f64" => Some(F64),
David Tolnay7db73692019-10-20 14:51:12 -040047 "CxxString" => Some(CxxString),
48 "String" => Some(RustString),
49 _ => None,
50 }
51 }
52}
David Tolnayba5eb2d2020-03-06 09:58:20 -080053
David Tolnay699351b2020-05-10 22:57:08 -070054impl Display for Atom {
55 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
56 formatter.write_str(self.as_ref())
57 }
58}
59
David Tolnaya4596c42020-05-10 22:55:45 -070060impl AsRef<str> for Atom {
61 fn as_ref(&self) -> &str {
62 use self::Atom::*;
63 match self {
64 Bool => "bool",
David Tolnayb3873dc2020-11-25 19:47:49 -080065 Char => "c_char",
David Tolnaya4596c42020-05-10 22:55:45 -070066 U8 => "u8",
67 U16 => "u16",
68 U32 => "u32",
69 U64 => "u64",
70 Usize => "usize",
71 I8 => "i8",
72 I16 => "i16",
73 I32 => "i32",
74 I64 => "i64",
75 Isize => "isize",
76 F32 => "f32",
77 F64 => "f64",
78 CxxString => "CxxString",
79 RustString => "String",
80 }
David Tolnayba5eb2d2020-03-06 09:58:20 -080081 }
82}
83
84impl PartialEq<Atom> for Type {
85 fn eq(&self, atom: &Atom) -> bool {
86 match self {
Adrian Taylorc8713432020-10-21 18:20:55 -070087 Type::Ident(ident) => ident.rust == atom,
David Tolnayba5eb2d2020-03-06 09:58:20 -080088 _ => false,
89 }
90 }
91}
David Tolnay438e2602020-03-06 10:21:35 -080092
93impl PartialEq<Atom> for &Ident {
94 fn eq(&self, atom: &Atom) -> bool {
95 *self == atom
96 }
97}
98
99impl PartialEq<Atom> for &Type {
100 fn eq(&self, atom: &Atom) -> bool {
101 *self == atom
102 }
103}