Created
April 29, 2017 21:06
-
-
Save valarauca/98c410ff59a84b4b042bceaf89fe4bc3 to your computer and use it in GitHub Desktop.
Life time elison
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; | |
| use json::JsonValue; | |
| use json::object::Object; | |
| use std::iter::Iterator; | |
| enum Output<'a> { | |
| Nothing, | |
| Obj(&'a JsonValue), | |
| Val(u64) | |
| } | |
| /// used to extract a json value | |
| #[inline(always)] | |
| fn get<'a>(x: &'a JsonValue, key: &str) -> Output<'a> { | |
| match x { | |
| &JsonValue::Object(ref x) => match x.get(key) { | |
| Option::None => Output::Nothing, | |
| Option::Some(y) => match y.as_u64() { | |
| Option::None => Output::Obj(y), | |
| Option::Some(z) => Output::Val(z) | |
| } | |
| }, | |
| _ => Output::Nothing | |
| } | |
| } | |
| /// Json Graph | |
| pub struct JsonGraph(json::JsonValue); | |
| impl JsonGraph { | |
| /// Parse Json Structure | |
| pub fn new(x: &str) -> Option<JsonGraph> { | |
| match json::parse(x) { | |
| Ok(x) => Some(JsonGraph(x)), | |
| _ => None | |
| } | |
| } | |
| /// Walk down the graph | |
| pub fn walk<'a, 'b, I>(& 'b self, mut x: I) | |
| -> Option<u64> | |
| where I: Iterator<Item=&'a str> | |
| { | |
| let mut z: Option<&'b JsonValue> = Some(&self.0); | |
| loop { | |
| match x.next() { | |
| Option::None => return None, | |
| Option::Some(key) => { | |
| let local = match z { | |
| Option::Some(x) => x, | |
| Option::None => return None | |
| }; | |
| match get(local, key) { | |
| Output::Nothing => return None, | |
| Output::Val(val) => return Some(val), | |
| Output::Obj(o) => { | |
| z = Option::Some(o); | |
| continue; | |
| } | |
| }; | |
| } | |
| }; | |
| } | |
| } | |
| /// Consumes self to produce a vector of data | |
| #[inline(always)] | |
| pub fn serialize(self) -> Vec<u8> { | |
| json::stringify(self.0).into_bytes() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment