2024-06-19 01:36:07 -04:00
|
|
|
mod guac;
|
|
|
|
|
|
|
|
|
|
use neon::prelude::*;
|
|
|
|
|
|
|
|
|
|
fn guac_decode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsArray> {
|
|
|
|
|
let input = cx.argument::<JsString>(0)?.value(cx);
|
|
|
|
|
|
|
|
|
|
match guac::decode_instruction(&input) {
|
|
|
|
|
Ok(data) => {
|
|
|
|
|
let array = JsArray::new(cx, data.len());
|
|
|
|
|
|
2024-06-19 01:37:17 -04:00
|
|
|
let conv = data
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|v| cx.string(v))
|
|
|
|
|
.collect::<Vec<Handle<JsString>>>();
|
2024-06-19 01:36:07 -04:00
|
|
|
|
|
|
|
|
for (i, str) in conv.iter().enumerate() {
|
|
|
|
|
array.set(cx, i as u32, *str)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Err(e) => {
|
2024-06-19 02:34:38 -04:00
|
|
|
let err = cx.string(format!("{}", e));
|
2024-06-19 01:36:07 -04:00
|
|
|
return cx.throw(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn guac_encode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsString> {
|
2024-06-19 01:37:17 -04:00
|
|
|
let mut elements: Vec<String> = Vec::with_capacity(cx.len());
|
2024-06-19 01:36:07 -04:00
|
|
|
|
2024-06-19 01:37:17 -04:00
|
|
|
// Capture varadic arguments
|
|
|
|
|
for i in 0..cx.len() {
|
|
|
|
|
let input = cx.argument::<JsString>(i)?.value(cx);
|
|
|
|
|
elements.push(input);
|
|
|
|
|
}
|
2024-06-19 01:36:07 -04:00
|
|
|
|
|
|
|
|
Ok(cx.string(guac::encode_instruction(&elements)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn guac_decode(mut cx: FunctionContext) -> JsResult<JsArray> {
|
|
|
|
|
guac_decode_impl(&mut cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn guac_encode(mut cx: FunctionContext) -> JsResult<JsString> {
|
2024-06-19 01:37:17 -04:00
|
|
|
guac_encode_impl(&mut cx)
|
2024-06-19 01:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[neon::main]
|
|
|
|
|
fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
|
|
|
|
cx.export_function("guacDecode", guac_decode)?;
|
|
|
|
|
cx.export_function("guacEncode", guac_encode)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|