use std::io; mod module; mod section; mod primitives; mod types; mod import_entry; mod export_entry; mod ops; pub use self::module::Module; pub use self::section::Section; pub use self::import_entry::{ImportEntry, MemoryType, TableType, External}; pub use self::export_entry::{ExportEntry, Internal}; pub use self::primitives::{VarUint32, VarUint7, VarUint1, VarInt7, Uint32, CountedList}; pub use self::types::{ValueType, BlockType}; pub trait Deserialize : Sized { type Error; fn deserialize(reader: &mut R) -> Result; } #[derive(Debug)] pub enum Error { UnexpectedEof, InconsistentLength { expected: usize, actual: usize }, Other(&'static str), HeapOther(String), UnknownValueType(i8), NonUtf8String, UnknownExternalKind(u8), UnknownInternalKind(u8), } impl From for Error { fn from(err: io::Error) -> Self { Error::HeapOther(format!("I/O Error: {}", err)) } } struct Unparsed(pub Vec); impl Deserialize for Unparsed { type Error = Error; fn deserialize(reader: &mut R) -> Result { let len = VarUint32::deserialize(reader)?.into(); let mut vec = vec![0u8; len]; reader.read_exact(&mut vec[..])?; Ok(Unparsed(vec)) } } impl From for Vec { fn from(u: Unparsed) -> Vec { u.0 } } pub fn deserialize_file>(p: P) -> Result { use std::io::Read; let mut contents = Vec::new(); ::std::fs::File::open(p)?.read_to_end(&mut contents)?; deserialize_buffer(contents) } pub fn deserialize_buffer(contents: Vec) -> Result { let mut reader = io::Cursor::new(contents); T::deserialize(&mut reader) }