blob: 7d0ef6b623ef8ecaaf15a6a3c5c4a924100169f5 [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> {
David Tolnay9bcb4c62020-05-10 17:42:06 -070026 Self::from_str(ident.to_string().as_str())
27 }
28
29 pub fn from_str(s: &str) -> Option<Self> {
David Tolnay7db73692019-10-20 14:51:12 -040030 use self::Atom::*;
David Tolnay9bcb4c62020-05-10 17:42:06 -070031 match s {
David Tolnay7db73692019-10-20 14:51:12 -040032 "bool" => Some(Bool),
33 "u8" => Some(U8),
34 "u16" => Some(U16),
35 "u32" => Some(U32),
36 "u64" => Some(U64),
37 "usize" => Some(Usize),
38 "i8" => Some(I8),
39 "i16" => Some(I16),
40 "i32" => Some(I32),
41 "i64" => Some(I64),
David Tolnay9542f222020-03-13 13:55:28 -070042 "isize" => Some(Isize),
David Tolnay3383ae72020-03-13 01:12:26 -070043 "f32" => Some(F32),
44 "f64" => Some(F64),
David Tolnay7db73692019-10-20 14:51:12 -040045 "CxxString" => Some(CxxString),
46 "String" => Some(RustString),
47 _ => None,
48 }
49 }
50}
David Tolnayba5eb2d2020-03-06 09:58:20 -080051
David Tolnay699351b2020-05-10 22:57:08 -070052impl Display for Atom {
53 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
54 formatter.write_str(self.as_ref())
55 }
56}
57
David Tolnaya4596c42020-05-10 22:55:45 -070058impl AsRef<str> for Atom {
59 fn as_ref(&self) -> &str {
60 use self::Atom::*;
61 match self {
62 Bool => "bool",
63 U8 => "u8",
64 U16 => "u16",
65 U32 => "u32",
66 U64 => "u64",
67 Usize => "usize",
68 I8 => "i8",
69 I16 => "i16",
70 I32 => "i32",
71 I64 => "i64",
72 Isize => "isize",
73 F32 => "f32",
74 F64 => "f64",
75 CxxString => "CxxString",
76 RustString => "String",
77 }
David Tolnayba5eb2d2020-03-06 09:58:20 -080078 }
79}
80
81impl PartialEq<Atom> for Type {
82 fn eq(&self, atom: &Atom) -> bool {
83 match self {
Adrian Taylorc8713432020-10-21 18:20:55 -070084 Type::Ident(ident) => ident.rust == atom,
David Tolnayba5eb2d2020-03-06 09:58:20 -080085 _ => false,
86 }
87 }
88}
David Tolnay438e2602020-03-06 10:21:35 -080089
90impl PartialEq<Atom> for &Ident {
91 fn eq(&self, atom: &Atom) -> bool {
92 *self == atom
93 }
94}
95
96impl PartialEq<Atom> for &Type {
97 fn eq(&self, atom: &Atom) -> bool {
98 *self == atom
99 }
100}