Separate out the wasm test into its own feature/binary.

This commit is contained in:
Tom Alexander 2023-12-25 13:10:40 -05:00
parent 67e5829fd9
commit 65abaa332f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
8 changed files with 53 additions and 33 deletions

View File

@ -44,6 +44,11 @@ path = "src/lib.rs"
path = "src/bin_wasm.rs"
required-features = ["wasm"]
[[bin]]
name = "wasm_test"
path = "src/bin_wasm_test.rs"
required-features = ["wasm_test"]
[dependencies]
futures = { version = "0.3.28", optional = true }
nom = "7.1.1"
@ -69,7 +74,8 @@ compare = ["tokio/process", "tokio/macros"]
foreign_document_test = ["compare", "dep:futures", "tokio/sync", "dep:walkdir", "tokio/process"]
tracing = ["dep:opentelemetry", "dep:opentelemetry-otlp", "dep:opentelemetry-semantic-conventions", "dep:tokio", "dep:tracing", "dep:tracing-opentelemetry", "dep:tracing-subscriber"]
event_count = []
wasm = ["dep:serde", "dep:wasm-bindgen", "dep:serde-wasm-bindgen", "dep:serde_json"]
wasm = ["dep:serde", "dep:wasm-bindgen", "dep:serde-wasm-bindgen"]
wasm_test = ["wasm", "dep:serde_json"]
# Optimized build for any sort of release.
[profile.release-lto]

View File

@ -36,7 +36,7 @@ wasm:
.PHONY: run_wasm
run_wasm:
> cargo run --profile wasm --bin wasm --features wasm | jq
> cargo run --profile wasm --bin wasm_test --features wasm_test | jq
.PHONY: clean
clean:

View File

@ -1,10 +1,6 @@
// #![no_main]
#![no_main]
use organic::parser::parse_with_settings;
use organic::settings::GlobalSettings;
use wasm::ParseResult;
use wasm::ToWasm;
use wasm::ToWasmContext;
use wasm::wasm_parse_org;
use wasm_bindgen::prelude::wasm_bindgen;
mod error;
@ -12,29 +8,6 @@ mod wasm;
#[wasm_bindgen]
pub fn parse_org(org_contents: &str) -> wasm_bindgen::JsValue {
let rust_parsed = impl_parse_org(org_contents);
let rust_parsed = wasm_parse_org(org_contents);
serde_wasm_bindgen::to_value(&rust_parsed).unwrap()
}
fn impl_parse_org(org_contents: &str) -> ParseResult<'_> {
let global_settings = GlobalSettings::default();
let to_wasm_context = ToWasmContext::new(org_contents);
let rust_parsed = match parse_with_settings(org_contents, &global_settings)
.map(|document| document.to_wasm(to_wasm_context))
.map(|wasm_document| match wasm_document {
Ok(wasm_document) => ParseResult::Success(wasm_document),
Err(err) => ParseResult::Error(format!("{:?}", err)),
}) {
Ok(wasm_document) => wasm_document,
Err(err) => ParseResult::Error(format!("{:?}", err)),
};
rust_parsed
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let body = include_str!("/tmp/test.org");
let result = impl_parse_org(body);
println!("{}", serde_json::to_string(&result)?);
Ok(())
}

12
src/bin_wasm_test.rs Normal file
View File

@ -0,0 +1,12 @@
use wasm::wasm_parse_org;
mod error;
mod wasm;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let body = include_str!("/tmp/test.org");
let result = wasm_parse_org(body);
println!("{}", serde_json::to_string(&result)?);
Ok(())
}

1
src/wasm/compare/mod.rs Normal file
View File

@ -0,0 +1 @@
mod runner;

View File

@ -0,0 +1,7 @@
// pub async fn run_anonymous_compare_with_settings<'g, 's, P: AsRef<str>>(
// org_contents: P,
// global_settings: &GlobalSettings<'g, 's>,
// silent: bool,
// ) -> Result<bool, Box<dyn std::error::Error>> {
// todo!()
// }

View File

@ -9,6 +9,8 @@ mod clock;
mod code;
mod comment;
mod comment_block;
#[cfg(feature = "wasm_test")]
mod compare;
mod diary_sexp;
mod document;
mod drawer;
@ -62,6 +64,7 @@ mod underline;
mod verbatim;
mod verse_block;
pub(crate) use parse_result::wasm_parse_org;
pub(crate) use parse_result::ParseResult;
pub(crate) use to_wasm::ToWasm;
pub(crate) use to_wasm::ToWasmContext;

View File

@ -1,7 +1,10 @@
use serde::Deserialize;
use organic::parser::parse_with_settings;
use organic::settings::GlobalSettings;
use serde::Serialize;
use super::document::WasmDocument;
use super::ToWasm;
use super::ToWasmContext;
#[derive(Debug, Serialize)]
#[serde(tag = "status", content = "content")]
@ -12,3 +15,18 @@ pub(crate) enum ParseResult<'s> {
#[serde(rename = "error")]
Error(String),
}
pub(crate) fn wasm_parse_org(org_contents: &str) -> ParseResult<'_> {
let global_settings = GlobalSettings::default();
let to_wasm_context = ToWasmContext::new(org_contents);
let rust_parsed = match parse_with_settings(org_contents, &global_settings)
.map(|document| document.to_wasm(to_wasm_context))
.map(|wasm_document| match wasm_document {
Ok(wasm_document) => ParseResult::Success(wasm_document),
Err(err) => ParseResult::Error(format!("{:?}", err)),
}) {
Ok(wasm_document) => wasm_document,
Err(err) => ParseResult::Error(format!("{:?}", err)),
};
rust_parsed
}