2018-12-26 14:45:31 +09:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate env_logger;
|
2019-03-05 18:43:58 +09:00
|
|
|
|
2018-12-26 14:45:31 +09:00
|
|
|
extern crate serde;
|
2019-03-05 18:43:58 +09:00
|
|
|
#[cfg(test)]
|
2019-02-19 08:20:59 +09:00
|
|
|
#[macro_use]
|
2018-12-26 14:45:31 +09:00
|
|
|
extern crate serde_json;
|
2019-03-05 18:43:58 +09:00
|
|
|
#[cfg(not(test))]
|
|
|
|
extern crate serde_json;
|
|
|
|
|
2019-02-19 08:20:59 +09:00
|
|
|
extern crate core;
|
2019-02-25 16:43:46 +09:00
|
|
|
extern crate indexmap;
|
2019-02-26 23:04:04 +09:00
|
|
|
|
2019-03-03 00:33:27 +09:00
|
|
|
pub mod parser;
|
|
|
|
pub mod filter;
|
2019-02-26 23:04:04 +09:00
|
|
|
|
2019-03-03 00:33:27 +09:00
|
|
|
use parser::parser::*;
|
|
|
|
use filter::value_filter::*;
|
2019-02-26 23:04:04 +09:00
|
|
|
|
|
|
|
use std::result;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
type Result = result::Result<Value, String>;
|
|
|
|
|
|
|
|
pub fn compile<'a>(path: &'a str) -> impl FnMut(Value) -> Result + 'a {
|
|
|
|
let mut parser = Parser::new(path);
|
|
|
|
let node = parser.compile();
|
|
|
|
move |json| {
|
|
|
|
match &node {
|
|
|
|
Ok(n) => {
|
2019-03-03 00:33:27 +09:00
|
|
|
let mut jf = JsonValueFilter::new_from_value(json);
|
2019-02-26 23:04:04 +09:00
|
|
|
jf.visit(n.clone());
|
|
|
|
Ok(jf.take_value())
|
|
|
|
},
|
|
|
|
Err(e) => Err(e.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 00:33:27 +09:00
|
|
|
pub fn reader(json: Value) -> impl FnMut(&str) -> Result {
|
|
|
|
let mut jf = JsonValueFilter::new_from_value(json);
|
|
|
|
move |path: &str| {
|
|
|
|
let mut parser = Parser::new(path);
|
|
|
|
parser.parse(&mut jf)?;
|
|
|
|
Ok(jf.take_value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(json: Value, path: &str) -> Result {
|
|
|
|
let mut jf = JsonValueFilter::new_from_value(json);
|
2019-02-26 23:04:04 +09:00
|
|
|
let mut parser = Parser::new(path);
|
|
|
|
parser.parse(&mut jf)?;
|
|
|
|
Ok(jf.take_value())
|
2019-03-03 00:33:27 +09:00
|
|
|
}
|