1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
use context::Context; use error::{Error, Result}; use path::Path; use render::Chomp; use serde_json::Value; use std::io::Write; impl<W: Write> Context<W> for Value { fn inject(&self, path: Path, sink: &mut W) -> Result<()> { use self::Value::*; let mut value = self; for part in path.parts() { if let Some(next_value) = value.get(part) { value = next_value; } else { return Err(Error::Undefined(path.to_owned())); } } match value { &String(ref s) => sink.write_all(s.as_bytes()), x => write!(sink, "{}", x), }?; Ok(()) } fn iterate(&self, path: Path, mut chomp: Chomp<W>) -> Result<()> { let mut value = self; for part in path.parts() { if let Some(next_value) = value.get(part) { value = next_value; } else { return Err(Error::Undefined(path.to_owned())); } } if let &Value::Array(ref array) = value { for value in array { chomp.chomp(value)?; } Ok(()) } else { Err(Error::NotIterable(path.to_owned())) } } }