summaryrefslogtreecommitdiff
path: root/crates/parser
diff options
context:
space:
mode:
authorHenry Gressmann <mail@henrygressmann.de>2023-12-09 14:56:03 +0100
committerHenry Gressmann <mail@henrygressmann.de>2023-12-09 14:56:03 +0100
commitd08750034a582b7ef338432a360c62a2969f6aa0 (patch)
tree577296351a97ed044dd2ceabc7d772c42cf21c0f /crates/parser
parentea0a362c585c2a2da39ec8fbafacd79c17680665 (diff)
chore: improve documentation
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser')
-rw-r--r--crates/parser/Cargo.toml2
-rw-r--r--crates/parser/README.md21
-rw-r--r--crates/parser/src/lib.rs7
3 files changed, 27 insertions, 3 deletions
diff --git a/crates/parser/Cargo.toml b/crates/parser/Cargo.toml
index 17c5557..823e69b 100644
--- a/crates/parser/Cargo.toml
+++ b/crates/parser/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name="tinywasm-parser"
-version="0.0.0"
+version="0.0.1"
edition="2021"
[dependencies]
diff --git a/crates/parser/README.md b/crates/parser/README.md
new file mode 100644
index 0000000..5772bb7
--- /dev/null
+++ b/crates/parser/README.md
@@ -0,0 +1,21 @@
+# `tinywasm_parser`
+
+This crate provides a parser that can parse WebAssembly modules into a TinyWasm module. It is based on
+[`wasmparser_nostd`](https://crates.io/crates/wasmparser_nostd) and used by [`tinywasm`](https://crates.io/crates/tinywasm).
+
+## Features
+
+- `std`: Enables the use of `std` and `std::io` for parsing from files and streams.
+- `logging`: Enables logging of the parsing process using the `log` crate.
+
+## Usage
+
+```rust
+use tinywasm_parser::{Parser, TinyWasmModule};
+let bytes = include_bytes!("./file.wasm");
+
+let parser = Parser::new();
+let module: TinyWasmModule = parser.parse_module_bytes(bytes).unwrap();
+let mudule: TinyWasmModule = parser.parse_module_file("path/to/file.wasm").unwrap();
+let module: TinyWasmModule = parser.parse_module_stream(&mut stream).unwrap();
+```
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index 343350a..0719c96 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -22,9 +22,11 @@ mod module;
use alloc::vec::Vec;
pub use error::*;
use module::ModuleReader;
-use tinywasm_types::{Function, TinyWasmModule};
+use tinywasm_types::Function;
use wasmparser::Validator;
+pub use tinywasm_types::TinyWasmModule;
+
#[derive(Default)]
pub struct Parser {}
@@ -33,7 +35,8 @@ impl Parser {
Self {}
}
- pub fn parse_module_bytes(&self, wasm: &[u8]) -> Result<TinyWasmModule> {
+ pub fn parse_module_bytes(&self, wasm: impl AsRef<[u8]>) -> Result<TinyWasmModule> {
+ let wasm = wasm.as_ref();
let mut validator = Validator::new();
let mut reader = ModuleReader::new();