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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
use std::io; use serde; use toml; use crate::error; use crate::value; #[derive(Debug)] pub struct Source(Option<String>); #[derive(Debug)] pub struct Sink<W: io::Write>(W); #[inline] pub fn source<R>(mut r: R) -> error::Result<Source> where R: io::Read, { let mut string = String::new(); r.read_to_string(&mut string)?; Ok(Source(Some(string))) } #[inline] pub fn sink<W>(w: W) -> Sink<W> where W: io::Write, { Sink(w) } impl value::Source for Source { #[inline] fn read(&mut self) -> error::Result<Option<value::Value>> { match self.0.take() { Some(v) => { let mut de = toml::de::Deserializer::new(v.as_str()); match serde::Deserialize::deserialize(&mut de) { Ok(v) => Ok(Some(v)), Err(e) => Err(error::Error::from(e)), } } None => Ok(None), } } } impl<W> value::Sink for Sink<W> where W: io::Write, { #[inline] fn write(&mut self, value: value::Value) -> error::Result<()> { let mut string = String::new(); { let mut ser = toml::ser::Serializer::new(&mut string); serde::Serialize::serialize(&value, &mut ser)?; } self.0.write_all(string.as_bytes())?; self.0.write_all(b"\n")?; Ok(()) } }