use crate::if_else;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Type {
pub kind: TypeKind,
pub generics: Option<Vec<Type>>,
}
impl Type {
pub fn new(kind: TypeKind, generics: Option<Vec<Type>>) -> Self {
Self { kind, generics }
}
pub fn full_type(&self) -> String {
let g = if_else!(
self.generics.is_some(),
format!("<{}>", self.get_generics()),
"".into()
);
format!("{}{}", self.kind.rust_name(), g)
}
pub fn get_generics(&self) -> String {
let mut generics = Vec::new();
if let Some(items) = &self.generics {
for g in items {
generics.push(g.full_type());
}
}
generics.join(", ")
}
pub fn full_type_java(&self) -> String {
let g = if_else!(
self.generics.is_some(),
format!("<{}>", self.get_generics_java()),
"".into()
);
format!("{}{}", self.kind.java_name(), g)
}
pub fn get_generics_java(&self) -> String {
let mut generics = Vec::new();
if let Some(items) = &self.generics {
for g in items {
generics.push(g.full_type_java());
}
}
generics.join(", ")
}
pub fn convert_func(&self) -> String {
self.kind.convert_func()
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum TypeKind {
#[default]
Void,
String,
I8,
I16,
I32,
I64,
U8,
U16,
U32,
U64,
F32,
F64,
Bool,
Char,
Other(String),
}
impl TypeKind {
pub fn is_primitive(&self) -> bool {
match self {
Self::Other(_) => false,
_ => true,
}
}
pub fn rust_name(&self) -> String {
match self {
Self::Void => "()".into(),
Self::String => "String".into(),
Self::I8 => "i8".into(),
Self::I16 => "i16".into(),
Self::I32 => "i32".into(),
Self::I64 => "i64".into(),
Self::U8 => "u8".into(),
Self::U16 => "u16".into(),
Self::U32 => "u32".into(),
Self::U64 => "u64".into(),
Self::F32 => "f32".into(),
Self::F64 => "f64".into(),
Self::Bool => "bool".into(),
Self::Char => "char".into(),
Self::Other(o) => o.to_owned(),
}
}
pub fn can_cast(&self) -> bool {
match self {
Self::String | Self::Other(_) => false,
_ => true,
}
}
pub fn java_name(&self) -> String {
match self {
Self::Void => "void".into(),
Self::String => "String".into(),
Self::I8 | Self::U8 => "byte".into(),
Self::I16 | Self::U16 => "short".into(),
Self::I32 | Self::U32 => "int".into(),
Self::I64 | Self::U64 => "long".into(),
Self::F32 => "float".into(),
Self::F64 => "double".into(),
Self::Bool => "boolean".into(),
Self::Char => "char".into(),
Self::Other(o) => o.to_owned(),
}
}
pub fn native_name(&self) -> String {
match self {
Self::Other(_) => "long".into(),
_ => self.java_name(),
}
}
pub fn is_number(&self) -> bool {
match self {
Self::I8
| Self::I16
| Self::I32
| Self::I64
| Self::U8
| Self::U16
| Self::U32
| Self::U64
| Self::F32
| Self::F64
| Self::Char => true,
_ => false,
}
}
pub fn jni_name(&self) -> String {
match self {
Self::Void => "()".into(),
Self::String => "jstring".into(),
Self::I8 | Self::U8 => "jbyte".into(),
Self::I16 | Self::U16 => "jshort".into(),
Self::I32 | Self::U32 => "jint".into(),
Self::I64 | Self::U64 => "jlong".into(),
Self::F32 => "jfloat".into(),
Self::F64 => "jdouble".into(),
Self::Bool => "jboolean".into(),
Self::Char => "jchar".into(),
Self::Other(_) => "jlong".into(),
}
}
pub fn jni_arg_name(&self) -> String {
match self {
Self::String => "JString<'local>".into(),
_ => self.jni_name(),
}
}
pub fn convert_func(&self) -> String {
match self {
Self::Void => "Blaze3D.youJustLostTheGame".into(),
Self::String => "NativeTools.getString".into(),
Self::I8 | Self::U8 => "NativeTools.getByte".into(),
Self::I16 | Self::U16 => "NativeTools.getShort".into(),
Self::I32 | Self::U32 => "NativeTools.getInt".into(),
Self::I64 | Self::U64 => "NativeTools.getLong".into(),
Self::F32 => "NativeTools.getFloat".into(),
Self::F64 => "NativeTools.getDouble".into(),
Self::Bool => "NativeTools.getBool".into(),
Self::Char => "NativeTools.getChar".into(),
Self::Other(it) => format!("{}.from", it),
}
}
}