record_query/value/
yaml.rs

1use crate::error;
2use crate::value;
3use serde_yaml;
4use std::io;
5
6#[derive(Debug)]
7pub struct Source<R>(Option<R>);
8
9#[derive(Debug)]
10pub struct Sink<W>(W)
11where
12    W: io::Write;
13
14#[inline]
15pub fn source<R>(r: R) -> Source<R>
16where
17    R: io::Read,
18{
19    Source(Some(r))
20}
21
22#[inline]
23pub fn sink<W>(w: W) -> Sink<W>
24where
25    W: io::Write,
26{
27    Sink(w)
28}
29
30impl<R> value::Source for Source<R>
31where
32    R: io::Read,
33{
34    #[inline]
35    fn read(&mut self) -> error::Result<Option<value::Value>> {
36        if let Some(r) = self.0.take() {
37            match serde_yaml::from_reader(r) {
38                Ok(v) => Ok(Some(v)),
39                Err(e) => Err(error::Error::from(e)),
40            }
41        } else {
42            Ok(None)
43        }
44    }
45}
46
47impl<W> value::Sink for Sink<W>
48where
49    W: io::Write,
50{
51    #[inline]
52    fn write(&mut self, value: value::Value) -> error::Result<()> {
53        serde_yaml::to_writer(&mut self.0, &value)?;
54        self.0.write_all(b"\n")?;
55        Ok(())
56    }
57}