third_party_rust_cxx/syntax/atom.rs

104 lines
2.2 KiB
Rust
Raw Permalink Normal View History

use crate::syntax::Type;
2019-10-20 18:51:12 +00:00
use proc_macro2::Ident;
2020-05-11 05:57:08 +00:00
use std::fmt::{self, Display};
2019-10-20 18:51:12 +00:00
#[derive(Copy, Clone, PartialEq)]
pub enum Atom {
Bool,
2020-11-26 03:47:49 +00:00
Char, // C char, not Rust char
2019-10-20 18:51:12 +00:00
U8,
U16,
U32,
U64,
Usize,
I8,
I16,
I32,
I64,
Isize,
2020-03-13 08:12:26 +00:00
F32,
F64,
2019-10-20 18:51:12 +00:00
CxxString,
RustString,
}
impl Atom {
pub fn from(ident: &Ident) -> Option<Self> {
Self::from_str(ident.to_string().as_str())
}
pub fn from_str(s: &str) -> Option<Self> {
2019-10-20 18:51:12 +00:00
use self::Atom::*;
match s {
2019-10-20 18:51:12 +00:00
"bool" => Some(Bool),
2020-11-26 03:47:49 +00:00
"c_char" => Some(Char),
2019-10-20 18:51:12 +00:00
"u8" => Some(U8),
"u16" => Some(U16),
"u32" => Some(U32),
"u64" => Some(U64),
"usize" => Some(Usize),
"i8" => Some(I8),
"i16" => Some(I16),
"i32" => Some(I32),
"i64" => Some(I64),
2020-03-13 20:55:28 +00:00
"isize" => Some(Isize),
2020-03-13 08:12:26 +00:00
"f32" => Some(F32),
"f64" => Some(F64),
2019-10-20 18:51:12 +00:00
"CxxString" => Some(CxxString),
"String" => Some(RustString),
_ => None,
}
}
}
2020-05-11 05:57:08 +00:00
impl Display for Atom {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(self.as_ref())
}
}
impl AsRef<str> for Atom {
fn as_ref(&self) -> &str {
use self::Atom::*;
match self {
Bool => "bool",
2020-11-26 03:47:49 +00:00
Char => "c_char",
U8 => "u8",
U16 => "u16",
U32 => "u32",
U64 => "u64",
Usize => "usize",
I8 => "i8",
I16 => "i16",
I32 => "i32",
I64 => "i64",
Isize => "isize",
F32 => "f32",
F64 => "f64",
CxxString => "CxxString",
RustString => "String",
}
}
}
impl PartialEq<Atom> for Type {
fn eq(&self, atom: &Atom) -> bool {
match self {
Type::Ident(ident) => ident.rust == atom,
_ => false,
}
}
}
2020-03-06 18:21:35 +00:00
impl PartialEq<Atom> for &Ident {
fn eq(&self, atom: &Atom) -> bool {
*self == atom
}
}
impl PartialEq<Atom> for &Type {
fn eq(&self, atom: &Atom) -> bool {
*self == atom
}
}