use crate::ne_vec::NEVec;
use serde::Deserialize;
use serde::Serialize;
use variant_count::VariantCount;
#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize, VariantCount)]
pub enum IType {
Boolean,
S8,
S16,
S32,
S64,
U8,
U16,
U32,
U64,
F32,
F64,
String,
ByteArray,
Array(Box<IType>),
I32,
I64,
Record(u64),
}
#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct RecordFieldType {
pub name: String,
pub ty: IType,
}
#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct RecordType {
pub name: String,
pub fields: NEVec<RecordFieldType>,
}
impl Default for RecordType {
fn default() -> Self {
Self {
name: String::new(),
fields: NEVec::new(vec![RecordFieldType {
name: String::new(),
ty: IType::S8,
}])
.unwrap(),
}
}
}
impl ToString for &IType {
fn to_string(&self) -> String {
match &self {
IType::Boolean => "bool".to_string(),
IType::S8 => "s8".to_string(),
IType::S16 => "s16".to_string(),
IType::S32 => "s32".to_string(),
IType::S64 => "s64".to_string(),
IType::U8 => "u8".to_string(),
IType::U16 => "u16".to_string(),
IType::U32 => "u32".to_string(),
IType::U64 => "u64".to_string(),
IType::F32 => "f32".to_string(),
IType::F64 => "f64".to_string(),
IType::String => "string".to_string(),
IType::ByteArray => "array (u8)".to_string(),
IType::Array(ty) => format!("array ({})", ty.as_ref().to_string()),
IType::I32 => "i32".to_string(),
IType::I64 => "i64".to_string(),
IType::Record(record_type_id) => format!("record {}", record_type_id),
}
}
}
impl ToString for &RecordType {
fn to_string(&self) -> String {
format!(
"record ${} (\n{fields})",
self.name,
fields = self
.fields
.iter()
.fold(String::new(), |mut accumulator, field_type| {
accumulator.push(' ');
accumulator.push_str(&format!(
"field ${}: {}\n",
field_type.name,
(&field_type.ty).to_string()
));
accumulator
}),
)
}
}