record_query/value/
toml.rs1use std::io;
2
3use serde;
4use toml;
5
6use crate::error;
7use crate::value;
8
9#[derive(Debug)]
10pub struct Source(Option<String>);
11
12#[derive(Debug)]
13pub struct Sink<W: io::Write>(W);
14
15#[inline]
16pub fn source<R>(mut r: R) -> error::Result<Source>
17where
18 R: io::Read,
19{
20 let mut string = String::new();
21 r.read_to_string(&mut string)?;
22 Ok(Source(Some(string)))
23}
24
25#[inline]
26pub fn sink<W>(w: W) -> Sink<W>
27where
28 W: io::Write,
29{
30 Sink(w)
31}
32
33impl value::Source for Source {
34 #[inline]
35 fn read(&mut self) -> error::Result<Option<value::Value>> {
36 match self.0.take() {
37 Some(v) => {
38 let mut de = toml::de::Deserializer::new(v.as_str());
39 match serde::Deserialize::deserialize(&mut de) {
40 Ok(v) => Ok(Some(v)),
41 Err(e) => Err(error::Error::from(e)),
42 }
43 }
44 None => Ok(None),
45 }
46 }
47}
48
49impl<W> value::Sink for Sink<W>
50where
51 W: io::Write,
52{
53 #[inline]
54 fn write(&mut self, value: value::Value) -> error::Result<()> {
55 let mut string = String::new();
56 {
57 let mut ser = toml::ser::Serializer::new(&mut string);
58 serde::Serialize::serialize(&value, &mut ser)?;
59 }
60
61 self.0.write_all(string.as_bytes())?;
62 self.0.write_all(b"\n")?;
63 Ok(())
64 }
65}