269 lines
8.3 KiB
Rust
Raw Normal View History

2019-03-14 22:30:42 +09:00
extern crate jsonpath_lib as jsonpath;
#[macro_use]
extern crate neon;
extern crate neon_serde;
extern crate serde_json;
2019-06-03 13:50:44 +09:00
use jsonpath::{JsonPathError, Node, Parser, Selector};
2019-03-14 22:30:42 +09:00
use neon::prelude::*;
use serde_json::Value;
///
/// `neon_serde::from_value` has very poor performance.
///
fn select(mut ctx: FunctionContext) -> JsResult<JsValue> {
let json_val = ctx.argument::<JsValue>(0)?;
let json: Value = neon_serde::from_value(&mut ctx, json_val)?;
let path = ctx.argument::<JsString>(1)?.value();
match jsonpath::select(&json, path.as_str()) {
Ok(value) => Ok(neon_serde::to_value(&mut ctx, &value)?),
Err(e) => panic!("{:?}", e)
}
}
fn select_str(mut ctx: FunctionContext) -> JsResult<JsValue> {
let json_val = ctx.argument::<JsString>(0)?.value();
let path = ctx.argument::<JsString>(1)?.value();
2019-04-04 12:19:51 +09:00
match jsonpath::select_as_str(&json_val, path.as_str()) {
Ok(value) => Ok(JsString::new(&mut ctx, &value).upcast()),
2019-03-14 22:30:42 +09:00
Err(e) => panic!("{:?}", e)
}
}
2019-06-11 00:02:05 +09:00
fn delete(mut ctx: FunctionContext) -> JsResult<JsValue> {
let json_val = ctx.argument::<JsString>(0)?.value();
let json: Value = match serde_json::from_str(&json_val) {
Ok(value) => value,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
};
let path = ctx.argument::<JsString>(1)?.value();
match jsonpath::delete(json, &path) {
Ok(value) => Ok(JsString::new(&mut ctx, match serde_json::to_string(&value) {
Ok(value) => value,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
}).upcast()),
Err(e) => panic!("{:?}", e)
}
}
fn replace_with(mut ctx: FunctionContext) -> JsResult<JsValue> {
let json_val = ctx.argument::<JsString>(0)?.value();
let json: Value = match serde_json::from_str(&json_val) {
Ok(value) => value,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
};
let path = ctx.argument::<JsString>(1)?.value();
let fun = ctx.argument::<JsFunction>(2)?;
match jsonpath::replace_with(json, &path, &mut |v| {
let json_str = JsString::new(&mut ctx, match serde_json::to_string(v) {
Ok(value) => value,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
});
let null = ctx.null();
let args = vec![ctx.string(json_str.value())];
let result = match fun.call(&mut ctx, null, args) {
Ok(result) => result,
Err(e) => panic!("{:?}", e)
};
let json_str = match result.downcast::<JsString>() {
Ok(v) => v.value(),
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
};
match serde_json::from_str(&json_str) {
Ok(v) => v,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
}
}) {
Ok(value) => Ok(JsString::new(&mut ctx, match serde_json::to_string(&value) {
Ok(value) => value,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
}).upcast()),
Err(e) => panic!("{:?}", e)
}
}
2019-03-14 22:30:42 +09:00
2019-06-03 13:50:44 +09:00
pub struct SelectorCls {
node: Option<Node>,
value: Option<Value>,
2019-03-14 22:30:42 +09:00
}
2019-06-03 13:50:44 +09:00
impl SelectorCls {
fn path(&mut self, path: &str) {
2019-06-03 18:45:26 +09:00
let node = match Parser::compile(path) {
2019-06-03 13:50:44 +09:00
Ok(node) => node,
Err(e) => panic!("{:?}", e)
};
self.node = Some(node);
}
fn value(&mut self, json_str: &str) {
let value: Value = match serde_json::from_str(&json_str) {
Ok(value) => value,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
};
self.value = Some(value);
}
fn select(&self) -> String {
let node = match &self.node {
Some(node) => node,
2019-06-03 13:50:44 +09:00
None => panic!("{:?}", JsonPathError::EmptyPath)
};
let value = match &self.value {
Some(value) => value,
None => panic!("{:?}", JsonPathError::EmptyValue)
};
let mut selector = Selector::new();
selector.compiled_path(node);
2019-06-03 13:50:44 +09:00
selector.value(&value);
match selector.select_as_str() {
Ok(ret) => ret,
Err(e) => panic!("{:?}", e)
}
}
2019-04-09 16:13:09 +09:00
}
2019-06-11 00:02:05 +09:00
pub struct SelectorMutCls {}
2019-03-14 22:30:42 +09:00
declare_types! {
2019-06-03 13:50:44 +09:00
pub class JsCompileFn for SelectorCls {
2019-03-14 22:30:42 +09:00
init(mut ctx) {
let path = ctx.argument::<JsString>(0)?.value();
2019-06-03 18:45:26 +09:00
let node = match Parser::compile(path.as_str()) {
2019-03-14 22:30:42 +09:00
Ok(node) => node,
Err(e) => panic!("{:?}", e)
};
2019-06-03 13:50:44 +09:00
Ok(SelectorCls { node: Some(node), value: None })
2019-03-14 22:30:42 +09:00
}
method template(mut ctx) {
2019-06-03 13:50:44 +09:00
let mut this = ctx.this();
2019-03-14 22:30:42 +09:00
2019-06-03 13:50:44 +09:00
let json_str = ctx.argument::<JsString>(0)?.value();
{
2019-03-14 22:30:42 +09:00
let guard = ctx.lock();
2019-06-03 13:50:44 +09:00
let mut this = this.borrow_mut(&guard);
let value: Value = match serde_json::from_str(&json_str) {
Ok(value) => value,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
};
this.value = Some(value);
2019-03-14 22:30:42 +09:00
};
2019-06-03 13:50:44 +09:00
let result_str = {
let guard = ctx.lock();
let this = this.borrow(&guard);
this.select()
2019-03-14 22:30:42 +09:00
};
2019-06-03 13:50:44 +09:00
Ok(JsString::new(&mut ctx, &result_str).upcast())
2019-03-14 22:30:42 +09:00
}
}
2019-06-03 13:50:44 +09:00
pub class JsSelectorFn for SelectorCls {
2019-03-14 22:30:42 +09:00
init(mut ctx) {
let json_str = ctx.argument::<JsString>(0)?.value();
2019-06-03 13:50:44 +09:00
let value: Value = match serde_json::from_str(&json_str) {
Ok(value) => value,
Err(e) => panic!("{:?}", JsonPathError::Serde(e.to_string()))
2019-03-14 22:30:42 +09:00
};
2019-06-03 13:50:44 +09:00
Ok(SelectorCls { node: None, value: Some(value) })
2019-03-14 22:30:42 +09:00
}
2019-04-09 16:13:09 +09:00
method select(mut ctx) {
2019-06-03 13:50:44 +09:00
let mut this = ctx.this();
2019-03-14 22:30:42 +09:00
let path = ctx.argument::<JsString>(0)?.value();
2019-06-03 13:50:44 +09:00
{
let guard = ctx.lock();
let mut this = this.borrow_mut(&guard);
this.path(&path);
}
2019-03-14 22:30:42 +09:00
2019-06-03 13:50:44 +09:00
let result_str = {
let guard = ctx.lock();
let this = this.borrow(&guard);
this.select()
2019-03-14 22:30:42 +09:00
};
2019-06-03 13:50:44 +09:00
Ok(JsString::new(&mut ctx, &result_str).upcast())
2019-03-14 22:30:42 +09:00
}
}
2019-04-09 16:13:09 +09:00
pub class JsSelector for SelectorCls {
init(mut _ctx) {
2019-06-03 13:50:44 +09:00
Ok(SelectorCls { node: None, value: None })
2019-04-09 16:13:09 +09:00
}
method path(mut ctx) {
let mut this = ctx.this();
let path = ctx.argument::<JsString>(0)?.value();
{
let guard = ctx.lock();
let mut this = this.borrow_mut(&guard);
2019-06-03 13:50:44 +09:00
let _ = this.path(&path);
2019-04-09 16:13:09 +09:00
}
Ok(JsUndefined::new().upcast())
}
2019-06-03 13:50:44 +09:00
method value(mut ctx) {
2019-05-16 14:14:09 +09:00
let mut this = ctx.this();
2019-06-03 13:50:44 +09:00
let json_str = ctx.argument::<JsString>(0)?.value();
2019-05-16 14:14:09 +09:00
{
let guard = ctx.lock();
let mut this = this.borrow_mut(&guard);
2019-06-03 13:50:44 +09:00
let _ = this.value(&json_str);
2019-05-16 14:14:09 +09:00
}
Ok(JsUndefined::new().upcast())
}
2019-06-03 13:50:44 +09:00
method select(mut ctx) {
let this = ctx.this();
2019-05-16 14:14:09 +09:00
2019-06-03 13:50:44 +09:00
let result_str = {
2019-05-16 14:14:09 +09:00
let guard = ctx.lock();
2019-06-03 13:50:44 +09:00
let this = this.borrow(&guard);
this.select()
};
2019-05-16 14:14:09 +09:00
2019-06-03 13:50:44 +09:00
Ok(JsString::new(&mut ctx, &result_str).upcast())
2019-05-16 14:14:09 +09:00
}
2019-04-09 16:13:09 +09:00
}
2019-06-11 00:02:05 +09:00
pub class JsSelectorMut for SelectorMutCls {
init(mut _ctx) {
Ok(SelectorMutCls {})
}
method emptyPathError(mut _ctx) {
panic!("{:?}", JsonPathError::EmptyPath);
}
method emptyValueError(mut _ctx) {
panic!("{:?}", JsonPathError::EmptyValue);
}
}
2019-03-14 22:30:42 +09:00
}
register_module!(mut m, {
2019-04-09 16:13:09 +09:00
m.export_class::<JsCompileFn>("CompileFn").expect("CompileFn class error");
m.export_class::<JsSelectorFn>("SelectorFn").expect("SelectorFn class error");
2019-03-14 22:30:42 +09:00
m.export_class::<JsSelector>("Selector").expect("Selector class error");
2019-06-11 00:02:05 +09:00
m.export_class::<JsSelectorMut>("SelectorMut").expect("SelectorMut class error");
2019-03-14 22:30:42 +09:00
m.export_function("select", select)?;
2019-06-11 00:02:05 +09:00
m.export_function("deleteValue", delete)?;
m.export_function("replaceWith", replace_with)?;
2019-03-14 22:30:42 +09:00
m.export_function("selectStr", select_str)?;
Ok(())
});