use alloc::boxed::Box;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldType {
Bool,
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
F32,
F64,
String,
Bytes,
Option(Box<FieldType>),
Vec(Box<FieldType>),
Struct(&'static str),
}
impl FieldType {
pub fn is_scalar(&self) -> bool {
matches!(
self,
Self::Bool
| Self::U8
| Self::U16
| Self::U32
| Self::U64
| Self::I8
| Self::I16
| Self::I32
| Self::I64
| Self::F32
| Self::F64
)
}
pub fn alignment(&self) -> usize {
match self {
Self::Bool | Self::U8 | Self::I8 => 1,
Self::U16 | Self::I16 => 2,
Self::U32 | Self::I32 | Self::F32 => 4,
Self::U64 | Self::I64 | Self::F64 => 8,
Self::String | Self::Bytes | Self::Option(_) | Self::Vec(_) | Self::Struct(_) => 8,
}
}
pub fn size(&self) -> Option<usize> {
match self {
Self::Bool | Self::U8 | Self::I8 => Some(1),
Self::U16 | Self::I16 => Some(2),
Self::U32 | Self::I32 | Self::F32 => Some(4),
Self::U64 | Self::I64 | Self::F64 => Some(8),
Self::String | Self::Bytes | Self::Option(_) | Self::Vec(_) | Self::Struct(_) => None,
}
}
}