| David Tolnay | ba5eb2d | 2020-03-06 09:58:20 -0800 | [diff] [blame] | 1 | use crate::syntax::Type; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 2 | use proc_macro2::Ident; |
| 3 | |
| 4 | #[derive(Copy, Clone, PartialEq)] |
| 5 | pub enum Atom { |
| 6 | Bool, |
| 7 | U8, |
| 8 | U16, |
| 9 | U32, |
| 10 | U64, |
| 11 | Usize, |
| 12 | I8, |
| 13 | I16, |
| 14 | I32, |
| 15 | I64, |
| 16 | Isize, |
| 17 | CxxString, |
| 18 | RustString, |
| 19 | } |
| 20 | |
| 21 | impl Atom { |
| 22 | pub fn from(ident: &Ident) -> Option<Self> { |
| 23 | use self::Atom::*; |
| 24 | match ident.to_string().as_str() { |
| 25 | "bool" => Some(Bool), |
| 26 | "u8" => Some(U8), |
| 27 | "u16" => Some(U16), |
| 28 | "u32" => Some(U32), |
| 29 | "u64" => Some(U64), |
| 30 | "usize" => Some(Usize), |
| 31 | "i8" => Some(I8), |
| 32 | "i16" => Some(I16), |
| 33 | "i32" => Some(I32), |
| 34 | "i64" => Some(I64), |
| 35 | "isize" => Some(Isize), |
| 36 | "CxxString" => Some(CxxString), |
| 37 | "String" => Some(RustString), |
| 38 | _ => None, |
| 39 | } |
| 40 | } |
| 41 | } |
| David Tolnay | ba5eb2d | 2020-03-06 09:58:20 -0800 | [diff] [blame] | 42 | |
| 43 | impl PartialEq<Atom> for Ident { |
| 44 | fn eq(&self, atom: &Atom) -> bool { |
| 45 | Atom::from(self) == Some(*atom) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl PartialEq<Atom> for Type { |
| 50 | fn eq(&self, atom: &Atom) -> bool { |
| 51 | match self { |
| 52 | Type::Ident(ident) => ident == atom, |
| 53 | _ => false, |
| 54 | } |
| 55 | } |
| 56 | } |
| David Tolnay | 438e260 | 2020-03-06 10:21:35 -0800 | [diff] [blame^] | 57 | |
| 58 | impl PartialEq<Atom> for &Ident { |
| 59 | fn eq(&self, atom: &Atom) -> bool { |
| 60 | *self == atom |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl PartialEq<Atom> for &Type { |
| 65 | fn eq(&self, atom: &Atom) -> bool { |
| 66 | *self == atom |
| 67 | } |
| 68 | } |