Created
December 12, 2018 05:09
-
-
Save terabyte/70011a505f3b45bbeb038e92b6af54a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate json; | |
extern crate rhai; | |
use rhai::{Engine, RegisterFn, Scope}; | |
use std::env; | |
use std::io::prelude::*; | |
use std::io; | |
fn jv_put_string(rec: &mut json::JsonValue, key: String, value: String) { | |
jv_put_jv(rec, key, json::JsonValue::from(value)); | |
} | |
fn jv_put_jv(rec: &mut json::JsonValue, key: String, value: json::JsonValue) { | |
rec[key] = value; | |
} | |
fn jv_get_jv(rec: &mut json::JsonValue, key: String) -> json::JsonValue { | |
return rec[key].clone(); | |
} | |
fn jv_to_string(jv: &mut json::JsonValue) -> String { | |
return jv.to_string(); | |
} | |
fn main() { | |
let stdin = io::stdin(); | |
let mut engine = Engine::new(); | |
engine.register_fn("put", jv_put_string); | |
engine.register_fn("put", jv_put_jv); | |
engine.register_fn("get", jv_get_jv); | |
engine.register_fn("to_string", jv_to_string); | |
for res in stdin.lock().lines() { | |
match res { | |
Ok(x) => { | |
//println!("Got line: {}", x); | |
let mut rec = json::parse(&x).expect(&format!("Unable to parse record: {}", x)); | |
if !rec.is_object() { | |
panic!("Record is not an object: {}", &x); | |
} | |
let mut scope: Scope = vec![("r".to_string(), Box::new(rec))]; | |
for arg in env::args().skip(1) { | |
println!("arg {}", arg); | |
engine.eval_with_scope::<()>(&mut scope, &arg).unwrap(); | |
} | |
rec = engine.eval_with_scope(&mut scope, "r").unwrap(); | |
println!("{}", rec.dump()); | |
}, | |
Err(x) => { | |
println!("Error: {}", x); | |
return; | |
}, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment