wasm_graph/
lib.rs

1//! WebAssembly binary format graph representation
2
3#![warn(missing_docs)]
4
5mod graph;
6mod ref_list;
7
8pub use graph::{
9    Module, Func, Memory, Table, Global, Export, ElementSegment,
10    DataSegment, ImportedOrDeclared, Instruction, Error, parse, FuncBody,
11    generate, ExportLocal, SegmentLocation,
12};
13
14pub use ref_list::{RefList, EntryRef};
15
16/// Parse file to graph representation.
17// TODO: feature gate io
18pub fn parse_file<P: AsRef<std::path::Path>>(p: P) -> Result<Module, graph::Error> {
19    use parity_wasm::elements::Deserialize;
20
21    let mut f = ::std::fs::File::open(p)
22        .map_err(|e| parity_wasm::elements::Error::HeapOther(format!("Can't read from the file: {:?}", e)))?;
23
24    graph::Module::from_elements(&parity_wasm::elements::Module::deserialize(&mut f)?)
25}
26
27/// Save graph representation to file.
28// TODO: feature gate io
29pub fn generate_file<P: AsRef<std::path::Path>>(module: &Module, p: P) -> Result<(), graph::Error> {
30    use parity_wasm::elements::Serialize;
31
32    let mut f = ::std::fs::File::create(p)
33        .map_err(|e| parity_wasm::elements::Error::HeapOther(format!("Can't create file: {:?}", e)))?;
34
35    Ok(module.generate()?.serialize(&mut f)?)
36}