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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::error;
use crate::value;
use csv;
use ordered_float;
use std::fmt;
use std::io;

pub struct Source<R>(csv::StringRecordsIntoIter<R>)
where
    R: io::Read;

pub struct Sink<W>(csv::Writer<W>)
where
    W: io::Write;

#[inline]
pub fn source<R>(r: R) -> Source<R>
where
    R: io::Read,
{
    Source(
        csv::ReaderBuilder::new()
            .has_headers(false)
            .from_reader(r)
            .into_records(),
    )
}

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

impl<R> value::Source for Source<R>
where
    R: io::Read,
{
    #[inline]
    fn read(&mut self) -> error::Result<Option<value::Value>> {
        match self.0.next() {
            Some(Ok(v)) => Ok(Some(value::Value::Sequence(
                v.iter()
                    .map(|s| value::Value::String(s.to_string()))
                    .collect(),
            ))),
            Some(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<()> {
        match value {
            value::Value::Sequence(seq) => {
                let record: Vec<String> = seq
                    .into_iter()
                    .map(value_to_csv)
                    .collect::<error::Result<Vec<_>>>()?;
                self.0.write_record(record)?;
                Ok(())
            }
            x => Err(error::Error::Format {
                msg: format!("csv can only output sequences, got: {:?}", x),
            }),
        }
    }
}

fn value_to_csv(value: value::Value) -> error::Result<String> {
    match value {
        value::Value::Unit => Err(error::Error::Format {
            msg: "csv cannot output nested Unit".to_owned(),
        }),
        value::Value::Bool(v) => Ok(v.to_string()),

        value::Value::I8(v) => Ok(v.to_string()),
        value::Value::I16(v) => Ok(v.to_string()),
        value::Value::I32(v) => Ok(v.to_string()),
        value::Value::I64(v) => Ok(v.to_string()),

        value::Value::U8(v) => Ok(v.to_string()),
        value::Value::U16(v) => Ok(v.to_string()),
        value::Value::U32(v) => Ok(v.to_string()),
        value::Value::U64(v) => Ok(v.to_string()),

        value::Value::F32(ordered_float::OrderedFloat(v)) => Ok(v.to_string()),
        value::Value::F64(ordered_float::OrderedFloat(v)) => Ok(v.to_string()),

        value::Value::Char(v) => Ok(v.to_string()),
        value::Value::String(v) => Ok(v.to_string()),
        value::Value::Bytes(_) => Err(error::Error::Format {
            msg: "csv cannot output nested bytes".to_owned(),
        }),

        value::Value::Sequence(_) => Err(error::Error::Format {
            msg: "csv cannot output nested sequences".to_owned(),
        }),
        value::Value::Map(_) => Err(error::Error::Format {
            msg: "csv cannot output nested maps".to_owned(),
        }),
    }
}

impl<R> fmt::Debug for Source<R>
where
    R: io::Read,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("CsvSource").finish()
    }
}

impl<W> fmt::Debug for Sink<W>
where
    W: io::Write,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("CsvSink").finish()
    }
}