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
use crate::error;
use crate::value;
use serde_yaml;
use std::io;

#[derive(Debug)]
pub struct Source<R>(Option<R>);

#[derive(Debug)]
pub struct Sink<W>(W)
where
    W: io::Write;

#[inline]
pub fn source<R>(r: R) -> Source<R>
where
    R: io::Read,
{
    Source(Some(r))
}

#[inline]
pub fn sink<W>(w: W) -> Sink<W>
where
    W: io::Write,
{
    Sink(w)
}

impl<R> value::Source for Source<R>
where
    R: io::Read,
{
    #[inline]
    fn read(&mut self) -> error::Result<Option<value::Value>> {
        if let Some(r) = self.0.take() {
            match serde_yaml::from_reader(r) {
                Ok(v) => Ok(Some(v)),
                Err(e) => Err(error::Error::from(e)),
            }
        } else {
            Ok(None)
        }
    }
}

impl<W> value::Sink for Sink<W>
where
    W: io::Write,
{
    #[inline]
    fn write(&mut self, value: value::Value) -> error::Result<()> {
        serde_yaml::to_writer(&mut self.0, &value)?;
        self.0.write_all(b"\n")?;
        Ok(())
    }
}