jsonpath/wasm/src/lib.rs

367 lines
11 KiB
Rust
Raw Normal View History

2019-03-03 00:33:27 +09:00
extern crate cfg_if;
extern crate js_sys;
2019-03-06 18:44:39 +09:00
extern crate jsonpath_lib as jsonpath;
2019-03-11 17:35:15 +09:00
extern crate serde_json;
extern crate wasm_bindgen;
2019-03-03 00:33:27 +09:00
use cfg_if::cfg_if;
use jsonpath::Selector as _Selector;
2019-06-10 18:16:08 +09:00
use jsonpath::SelectorMut as _SelectorMut;
2019-06-19 11:34:47 +09:00
use jsonpath::{JsonPathError, Parser};
use serde_json::Value;
use wasm_bindgen::prelude::*;
2019-03-03 00:33:27 +09:00
cfg_if! {
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}
2019-04-08 14:44:18 +09:00
cfg_if! {
if #[cfg(feature = "console_error_panic_hook")] {
extern crate console_error_panic_hook;
pub use self::console_error_panic_hook::set_once as set_panic_hook;
} else {
#[inline]
pub fn set_panic_hook() {}
}
}
2019-06-10 18:16:08 +09:00
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn error(s: &str);
}
macro_rules! console_error {
($($t:tt)*) => (error(&format_args!($($t)*).to_string()))
}
fn into_serde_json<D>(js_value: &JsValue) -> Result<D, String>
2019-06-19 11:34:47 +09:00
where
D: for<'a> serde::de::Deserialize<'a>,
{
2019-03-03 00:33:27 +09:00
if js_value.is_string() {
match serde_json::from_str(js_value.as_string().unwrap().as_str()) {
Ok(json) => Ok(json),
2019-06-19 11:34:47 +09:00
Err(e) => Err(e.to_string()),
2019-03-03 00:33:27 +09:00
}
} else {
match js_value.into_serde() {
Ok(json) => Ok(json),
2019-06-19 11:34:47 +09:00
Err(e) => Err(e.to_string()),
2019-03-03 00:33:27 +09:00
}
}
}
2019-06-10 18:16:08 +09:00
fn replace_fun(v: &Value, fun: &js_sys::Function) -> Value {
match JsValue::from_serde(v) {
2019-06-19 11:34:47 +09:00
Ok(js_v) => match fun.call1(&JsValue::NULL, &js_v) {
Ok(result) => match into_serde_json(&result) {
Ok(json) => json,
2019-06-10 18:16:08 +09:00
Err(e) => {
2019-06-19 11:34:47 +09:00
console_error!("replace_with - closure returned a invalid JSON: {:?}", e);
2019-06-10 18:16:08 +09:00
Value::Null
}
2019-06-19 11:34:47 +09:00
},
Err(e) => {
console_error!("replace_with - fail to call closure: {:?}", e);
Value::Null
2019-06-10 18:16:08 +09:00
}
2019-06-19 11:34:47 +09:00
},
2019-06-10 18:16:08 +09:00
Err(e) => {
console_error!("replace_with - invalid JSON object: {:?}", e);
Value::Null
}
}
}
2019-03-03 00:33:27 +09:00
#[wasm_bindgen]
pub fn compile(path: &str) -> JsValue {
2019-06-03 18:45:26 +09:00
let node = Parser::compile(path);
2019-06-02 22:03:35 +09:00
2019-03-03 00:33:27 +09:00
let cb = Closure::wrap(Box::new(move |js_value: JsValue| {
let json = match into_serde_json(&js_value) {
Ok(json) => json,
2019-06-19 11:34:47 +09:00
Err(e) => return JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e))),
};
2019-06-02 22:03:35 +09:00
let mut selector = _Selector::new();
2019-03-03 00:33:27 +09:00
match &node {
Ok(node) => selector.compiled_path(node),
2019-06-19 11:34:47 +09:00
Err(e) => return JsValue::from_str(&format!("{:?}", JsonPathError::Path(e.clone()))),
2019-06-02 22:03:35 +09:00
};
2019-06-02 22:03:35 +09:00
match selector.value(&json).select() {
Ok(ret) => match JsValue::from_serde(&ret) {
Ok(ret) => ret,
2019-06-19 11:34:47 +09:00
Err(e) => JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e.to_string()))),
2019-06-02 22:03:35 +09:00
},
2019-06-19 11:34:47 +09:00
Err(e) => JsValue::from_str(&format!("{:?}", e)),
2019-03-03 00:33:27 +09:00
}
2019-06-25 23:31:52 +09:00
}) as Box<dyn Fn(JsValue) -> JsValue>);
2019-03-03 00:33:27 +09:00
let ret = cb.as_ref().clone();
cb.forget();
ret
}
2019-03-11 17:35:15 +09:00
#[wasm_bindgen]
pub fn selector(js_value: JsValue) -> JsValue {
2019-06-02 22:03:35 +09:00
let json: Value = match JsValue::into_serde(&js_value) {
Ok(json) => json,
2019-06-19 11:34:47 +09:00
Err(e) => return JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e.to_string()))),
2019-05-27 11:42:46 +09:00
};
2019-05-26 23:30:01 +09:00
2019-06-19 11:34:47 +09:00
let cb = Closure::wrap(
Box::new(move |path: String| match Parser::compile(path.as_str()) {
2019-06-02 22:03:35 +09:00
Ok(node) => {
let mut selector = _Selector::new();
let _ = selector.compiled_path(&node);
2019-06-02 22:03:35 +09:00
match selector.value(&json).select() {
Ok(ret) => match JsValue::from_serde(&ret) {
Ok(ret) => ret,
2019-06-19 11:34:47 +09:00
Err(e) => {
JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e.to_string())))
}
2019-06-02 22:03:35 +09:00
},
2019-06-19 11:34:47 +09:00
Err(e) => JsValue::from_str(&format!("{:?}", e)),
2019-06-02 22:03:35 +09:00
}
}
2019-06-25 23:31:52 +09:00
Err(e) => JsValue::from_str(&format!("{:?}", JsonPathError::Path(e))),
}) as Box<dyn Fn(String) -> JsValue>,
2019-06-19 11:34:47 +09:00
);
2019-05-26 23:30:01 +09:00
2019-05-27 11:42:46 +09:00
let ret = cb.as_ref().clone();
cb.forget();
ret
2019-03-03 00:33:27 +09:00
}
#[wasm_bindgen]
2019-03-11 17:35:15 +09:00
pub fn select(js_value: JsValue, path: &str) -> JsValue {
2019-06-10 18:16:08 +09:00
let json = match into_serde_json(&js_value) {
Ok(json) => json,
2019-06-19 11:34:47 +09:00
Err(e) => return JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e))),
2019-06-10 18:16:08 +09:00
};
match jsonpath::select(&json, path) {
Ok(ret) => match JsValue::from_serde(&ret) {
Ok(ret) => ret,
2019-06-19 11:34:47 +09:00
Err(e) => JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e.to_string()))),
2019-06-10 18:16:08 +09:00
},
2019-06-19 11:34:47 +09:00
Err(e) => JsValue::from_str(&format!("{:?}", e)),
2019-06-10 18:16:08 +09:00
}
}
2019-06-02 22:03:35 +09:00
2019-06-10 18:16:08 +09:00
#[wasm_bindgen(catch, js_name = "deleteValue")]
pub fn delete(js_value: JsValue, path: &str) -> JsValue {
2019-06-02 22:03:35 +09:00
let json = match into_serde_json(&js_value) {
Ok(json) => json,
2019-06-19 11:34:47 +09:00
Err(e) => return JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e))),
2019-06-02 22:03:35 +09:00
};
2019-06-10 18:16:08 +09:00
match jsonpath::delete(json, path) {
2019-06-19 11:34:47 +09:00
Ok(ret) => match JsValue::from_serde(&ret) {
Ok(ret) => ret,
Err(e) => JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e.to_string()))),
},
Err(e) => JsValue::from_str(&format!("{:?}", e)),
2019-06-10 18:16:08 +09:00
}
}
#[wasm_bindgen(catch, js_name = "replaceWith")]
pub fn replace_with(js_value: JsValue, path: &str, fun: js_sys::Function) -> JsValue {
let json = match into_serde_json(&js_value) {
Ok(json) => json,
2019-06-19 11:34:47 +09:00
Err(e) => return JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e))),
2019-06-10 18:16:08 +09:00
};
match jsonpath::replace_with(json, path, &mut |v| replace_fun(v, &fun)) {
2019-06-02 22:03:35 +09:00
Ok(ret) => match JsValue::from_serde(&ret) {
Ok(ret) => ret,
2019-06-19 11:34:47 +09:00
Err(e) => JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e.to_string()))),
2019-06-02 22:03:35 +09:00
},
2019-06-19 11:34:47 +09:00
Err(e) => JsValue::from_str(&format!("{:?}", e)),
2019-03-03 00:33:27 +09:00
}
2019-03-07 18:44:06 +09:00
}
///
/// `wasm_bindgen` 제약으로 builder-pattern을 구사 할 수 없다.
2019-06-02 22:03:35 +09:00
/// lifetime 제약으로 Selector를 사용 할 수 없다.
///
#[wasm_bindgen]
2019-06-25 23:31:52 +09:00
#[derive(Default)]
pub struct Selector {
2019-06-02 22:03:35 +09:00
path: Option<String>,
value: Option<Value>,
}
#[wasm_bindgen]
impl Selector {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
2019-06-25 23:31:52 +09:00
Selector::default()
}
#[wasm_bindgen(catch)]
2019-06-02 22:03:35 +09:00
pub fn path(&mut self, path: &str) -> Result<(), JsValue> {
self.path = Some(path.to_string());
Ok(())
}
#[wasm_bindgen(catch)]
2019-06-02 22:03:35 +09:00
pub fn value(&mut self, value: JsValue) -> Result<(), JsValue> {
let json = into_serde_json(&value)
.map_err(|e| JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e))))?;
self.value = Some(json);
Ok(())
}
2019-06-02 22:03:35 +09:00
#[wasm_bindgen(catch, js_name = select)]
pub fn select(&mut self) -> Result<JsValue, JsValue> {
let mut selector = _Selector::new();
2019-06-02 22:03:35 +09:00
if let Some(path) = &self.path {
2019-06-19 11:34:47 +09:00
let _ = selector
.str_path(&path)
.map_err(|e| JsValue::from_str(&format!("{:?}", e)))?;
2019-06-02 22:03:35 +09:00
} else {
2019-06-19 11:34:47 +09:00
return Err(JsValue::from_str(&format!(
"{:?}",
JsonPathError::EmptyPath
)));
}
2019-06-02 22:03:35 +09:00
if let Some(value) = &self.value {
let _ = selector.value(value);
} else {
2019-06-19 11:34:47 +09:00
return Err(JsValue::from_str(&format!(
"{:?}",
JsonPathError::EmptyValue
)));
2019-06-02 22:03:35 +09:00
}
2019-06-02 22:03:35 +09:00
match selector.select() {
Ok(ret) => match JsValue::from_serde(&ret) {
Ok(ret) => Ok(ret),
2019-06-19 11:34:47 +09:00
Err(e) => Err(JsValue::from_str(&format!(
"{:?}",
JsonPathError::Serde(e.to_string())
))),
2019-06-02 22:03:35 +09:00
},
2019-06-19 11:34:47 +09:00
Err(e) => Err(JsValue::from_str(&format!("{:?}", e))),
2019-06-02 22:03:35 +09:00
}
}
2019-06-10 18:16:08 +09:00
}
///
/// `wasm_bindgen` 제약으로 builder-pattern을 구사 할 수 없다.
///
#[wasm_bindgen]
2019-06-25 23:31:52 +09:00
#[derive(Default)]
2019-06-10 18:16:08 +09:00
pub struct SelectorMut {
path: Option<String>,
value: Option<Value>,
}
#[wasm_bindgen]
impl SelectorMut {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
2019-06-25 23:31:52 +09:00
SelectorMut::default()
2019-06-10 18:16:08 +09:00
}
#[wasm_bindgen(catch)]
pub fn path(&mut self, path: &str) -> Result<(), JsValue> {
self.path = Some(path.to_string());
Ok(())
}
#[wasm_bindgen(catch)]
pub fn value(&mut self, value: JsValue) -> Result<(), JsValue> {
let json = into_serde_json(&value)
.map_err(|e| JsValue::from_str(&format!("{:?}", JsonPathError::Serde(e))))?;
self.value = Some(json);
Ok(())
}
#[wasm_bindgen(catch, js_name = "deleteValue")]
pub fn delete(&mut self) -> Result<(), JsValue> {
let mut selector = _SelectorMut::new();
if let Some(path) = &self.path {
let _ = selector.str_path(path);
} else {
2019-06-19 11:34:47 +09:00
return Err(JsValue::from_str(&format!(
"{:?}",
JsonPathError::EmptyPath
)));
2019-06-10 18:16:08 +09:00
};
if let Some(value) = self.value.take() {
selector.value(value);
} else {
2019-06-19 11:34:47 +09:00
return Err(JsValue::from_str(&format!(
"{:?}",
JsonPathError::EmptyValue
)));
2019-06-10 18:16:08 +09:00
};
match selector.delete() {
Err(e) => Err(JsValue::from_str(&format!("{:?}", e))),
_ => {
self.value = selector.take();
Ok(())
}
}
}
#[wasm_bindgen(catch, js_name = replaceWith)]
pub fn replace_with(&mut self, fun: js_sys::Function) -> Result<(), JsValue> {
let mut selector = _SelectorMut::new();
if let Some(path) = &self.path {
let _ = selector.str_path(path);
} else {
2019-06-19 11:34:47 +09:00
return Err(JsValue::from_str(&format!(
"{:?}",
JsonPathError::EmptyPath
)));
2019-06-10 18:16:08 +09:00
};
if let Some(value) = self.value.take() {
selector.value(value);
} else {
2019-06-19 11:34:47 +09:00
return Err(JsValue::from_str(&format!(
"{:?}",
JsonPathError::EmptyValue
)));
2019-06-10 18:16:08 +09:00
};
match selector.replace_with(&mut |v| replace_fun(v, &fun)) {
Err(e) => Err(JsValue::from_str(&format!("{:?}", e))),
_ => {
self.value = selector.take();
Ok(())
}
}
}
#[wasm_bindgen(catch)]
pub fn take(&mut self) -> Result<JsValue, JsValue> {
match self.value.take() {
Some(ret) => match JsValue::from_serde(&ret) {
Ok(ret) => Ok(ret),
2019-06-19 11:34:47 +09:00
Err(e) => Err(JsValue::from_str(&format!("{:?}", e))),
2019-06-10 18:16:08 +09:00
},
2019-06-19 11:34:47 +09:00
None => Err(JsValue::from_str(&format!(
"{:?}",
JsonPathError::EmptyValue
))),
2019-06-10 18:16:08 +09:00
}
}
2019-06-19 11:34:47 +09:00
}