Skip to content

Commit

Permalink
Pass settings to Rust backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Mar 21, 2024
1 parent f108fc1 commit eb1c753
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jaq-play/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ console_log = { version = "1.0", features = ["color"] }
getrandom = { version = "0.2", features = ["js"] }
wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = ["DedicatedWorkerGlobalScope"] }
js-sys = { version = "0.3" }
aho-corasick = "1.1.2"
20 changes: 6 additions & 14 deletions jaq-play/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,12 @@
<label for="join-output">join</label>
</li>
<li>
<fieldset>
<legend>Indentation</legend>
<ul>
<li>
<input type="radio" id="spaces" name="indent" checked>
<label for="spaces">Spaces</label>
<input type="number" id="spaces" name="spaces" value="4" min="0" style="width: 3em">
</li>
<li>
<input type="radio" id="tab" name="indent">
<label for="tab">Tab</label>
</li>
</ul>
</fieldset>
<input type="checkbox" id="tab" name="tab">
<label for="tab">tab</label>
</li>
<li>
<label for="indent">indent</label>
<input type="number" id="indent" name="indent" value="4" min="0" style="width: 3em">
</li>
</ul>
</fieldset>
Expand Down
39 changes: 37 additions & 2 deletions jaq-play/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,45 @@ impl<'a> Display for Pp<'a> {
}
}

#[allow(dead_code)]
#[derive(Debug)]
struct Settings {
raw_input: bool,
slurp: bool,
null_input: bool,
in_place: bool,
raw_output: bool,
compact: bool,
join_output: bool,
tab: bool,
}

impl Settings {
fn try_from(v: &JsValue) -> Option<Self> {
let get = |key: &str| js_sys::Reflect::get(v, &key.into()).ok();
let get_bool = |key: &str| get(key).and_then(|v| v.as_bool());
Some(Self {
raw_input: get_bool("raw-input")?,
slurp: get_bool("slurp")?,
null_input: get_bool("null-input")?,
in_place: get_bool("in-place")?,
raw_output: get_bool("raw-output")?,
compact: get_bool("compact")?,
join_output: get_bool("join-output")?,
tab: get_bool("tab")?,
})
}
}

use web_sys::DedicatedWorkerGlobalScope as Scope;

#[wasm_bindgen]
pub fn run(filter: &str, input: &str, scope: web_sys::DedicatedWorkerGlobalScope) {
pub fn run(filter: &str, input: &str, settings: &JsValue, scope: Scope) {
let _ = console_log::init();
log::info!("Starting run in Rust ...");
log::debug!("Starting run in Rust ...");

let settings = Settings::try_from(settings).unwrap();
log::debug!("{settings:?}");

let mut lexer = hifijson::SliceLexer::new(input.as_bytes());
let inputs = core::iter::from_fn(move || {
Expand Down
13 changes: 12 additions & 1 deletion jaq-play/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ function startWorker() {
//console.log("Starting run in JS ...");
const filter = document.getElementById('filter').value;
const input = document.getElementById('input').value;
worker.postMessage({filter, input});
const settings = getSettings();
worker.postMessage({filter, input, settings});
}

function getSettings() {
const get = id => document.getElementById(id);
const input = ["raw-input", "slurp", "null-input", "in-place"];
const output = ["raw-output", "compact", "join-output", "tab"];
var acc = {};
input.concat(output).forEach(id => acc[id] = get(id).checked);
acc["indent"] = get("indent").value;
return acc
}

function initWorker() {
Expand Down
4 changes: 2 additions & 2 deletions jaq-play/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ init();

// Set callback to handle messages passed to the worker.
self.onmessage = async event => {
const { filter, input } = event.data;
await run(filter, input, self);
const { filter, input, settings } = event.data;
await run(filter, input, settings, self);
};

0 comments on commit eb1c753

Please sign in to comment.