use crate::{
BinaryReader, ExternalKind, FromReader, GlobalType, MemoryType, Result, SectionLimited,
TableType, TagType,
};
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum TypeRef {
Func(u32),
Table(TableType),
Memory(MemoryType),
Global(GlobalType),
Tag(TagType),
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Import<'a> {
pub module: &'a str,
pub name: &'a str,
pub ty: TypeRef,
}
pub type ImportSectionReader<'a> = SectionLimited<'a, Import<'a>>;
impl<'a> FromReader<'a> for Import<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(Import {
module: reader.read()?,
name: reader.read()?,
ty: reader.read()?,
})
}
}
impl<'a> FromReader<'a> for TypeRef {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(match reader.read()? {
ExternalKind::Func => TypeRef::Func(reader.read_var_u32()?),
ExternalKind::Table => TypeRef::Table(reader.read()?),
ExternalKind::Memory => TypeRef::Memory(reader.read()?),
ExternalKind::Global => TypeRef::Global(reader.read()?),
ExternalKind::Tag => TypeRef::Tag(reader.read()?),
})
}
}