Skip to content
This repository was archived by the owner on Jun 15, 2020. It is now read-only.

Commit d3e9738

Browse files
committed
feat(wasm) Export Vec<Block> to wasm.
So, `wasm-bindgen` does not support `Vec<T>` (see rustwasm/wasm-bindgen#111), so my quick and dirty solution right now is to serialize the vector to JSON, and parse it from the JS land.
1 parent 420c3a9 commit d3e9738

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ default = []
1212

1313
[dependencies]
1414
nom = { version = "4.0.0-beta2" }
15+
serde = "^1.0"
16+
serde_derive = "^1.0"
1517
serde_json = "^1.0"
1618
wasm-bindgen = { version = "^0.2", optional = true }
1719
wee_alloc = { version = "^0.2", optional = true }

src/ast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use super::Input;
12
use serde_json as json;
23

3-
#[derive(Debug, PartialEq)]
4+
#[derive(Debug, PartialEq, Serialize)]
45
pub struct Block<'a> {
5-
pub name: (&'a [u8], &'a [u8]),
6+
pub name: (Input<'a>, Input<'a>),
67
pub attributes: Option<json::Value>,
78
pub inner_blocks: Vec<Block<'a>>
89
}

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
#[macro_use] extern crate nom;
44
#[cfg(feature = "wasm")] extern crate wasm_bindgen;
55
#[cfg(feature = "wasm")] extern crate wee_alloc;
6+
extern crate serde;
7+
#[macro_use] extern crate serde_derive;
68
#[cfg_attr(test, macro_use)] extern crate serde_json;
79

810
pub mod ast;
911
#[macro_use] pub mod combinators;
1012
pub mod parser;
1113
#[cfg(feature = "wasm")] pub mod wasm;
1214

13-
14-
#[cfg(feature = "wasm")] use wee_alloc::WeeAlloc;
15-
1615
#[cfg(feature = "wasm")]
1716
#[global_allocator]
18-
static ALLOC: WeeAlloc = WeeAlloc::INIT;
17+
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
1918

2019

2120
/// Represent the type of the input elements.

src/wasm.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use serde_json as json;
12
use wasm_bindgen::prelude::*;
23

34
#[wasm_bindgen]
4-
pub fn root(input: &str) -> bool {
5-
if let Ok(_) = super::root(input.as_bytes()) {
6-
true
5+
pub fn root(input: &str) -> String {
6+
if let Ok((_remaining, blocks)) = super::root(input.as_bytes()) {
7+
json::to_string(&blocks).unwrap()
78
} else {
8-
false
9+
"".to_owned()
910
}
1011
}

0 commit comments

Comments
 (0)