1 use crate::syntax::Type;
2 use proc_macro2::Ident;
3 use std::fmt::{self, Display};
4 
5 #[derive(Copy, Clone, PartialEq)]
6 pub enum Atom {
7     Bool,
8     Char, // C char, not Rust char
9     U8,
10     U16,
11     U32,
12     U64,
13     Usize,
14     I8,
15     I16,
16     I32,
17     I64,
18     Isize,
19     F32,
20     F64,
21     CxxString,
22     RustString,
23 }
24 
25 impl Atom {
from(ident: &Ident) -> Option<Self>26     pub fn from(ident: &Ident) -> Option<Self> {
27         Self::from_str(ident.to_string().as_str())
28     }
29 
from_str(s: &str) -> Option<Self>30     pub fn from_str(s: &str) -> Option<Self> {
31         use self::Atom::*;
32         match s {
33             "bool" => Some(Bool),
34             "c_char" => Some(Char),
35             "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),
44             "isize" => Some(Isize),
45             "f32" => Some(F32),
46             "f64" => Some(F64),
47             "CxxString" => Some(CxxString),
48             "String" => Some(RustString),
49             _ => None,
50         }
51     }
52 }
53 
54 impl Display for Atom {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result55     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
56         formatter.write_str(self.as_ref())
57     }
58 }
59 
60 impl AsRef<str> for Atom {
as_ref(&self) -> &str61     fn as_ref(&self) -> &str {
62         use self::Atom::*;
63         match self {
64             Bool => "bool",
65             Char => "c_char",
66             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         }
81     }
82 }
83 
84 impl PartialEq<Atom> for Type {
eq(&self, atom: &Atom) -> bool85     fn eq(&self, atom: &Atom) -> bool {
86         match self {
87             Type::Ident(ident) => ident.rust == atom,
88             _ => false,
89         }
90     }
91 }
92 
93 impl PartialEq<Atom> for &Ident {
eq(&self, atom: &Atom) -> bool94     fn eq(&self, atom: &Atom) -> bool {
95         *self == atom
96     }
97 }
98 
99 impl PartialEq<Atom> for &Type {
eq(&self, atom: &Atom) -> bool100     fn eq(&self, atom: &Atom) -> bool {
101         *self == atom
102     }
103 }
104