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
use serde::Serialize;

use crate::{Result, Writer};

/// An iterable CSV creator
pub struct Iter<I> {
    iter: I,

    writer: Writer,
}

impl<I: Iterator> Iter<I> {
    pub fn new(iter: impl IntoIterator<IntoIter = I>, writer: Writer) -> Self {
        Self {
            iter: iter.into_iter(),
            writer,
        }
    }
}

impl<I: Iterator> Iterator for Iter<I>
where
    I::Item: Serialize,
{
    type Item = Result<Vec<u8>>;

    fn next(&mut self) -> Option<Self::Item> {
        let s = self.iter.next()?;
        let mut buf = vec![];
        Some(self.writer.serialize(&mut buf, s).map(|_| buf))
    }
}

#[cfg(test)]
mod tests {
    use crate::{Terminator, WriterBuilder};
    use serde::Serialize;

    use super::Iter;

    #[derive(Serialize)]
    struct Row<'a> {
        city: &'a str,
        country: &'a str,
        // Serde allows us to name our headers exactly,
        // even if they don't match our struct field names.
        #[serde(rename = "popcount")]
        population: u64,
    }

    const ROWS: [Row<'static>; 2] = [
        Row {
            city: "Boston",
            country: "United States",
            population: 4628910,
        },
        Row {
            city: "Concord",
            country: "United States",
            population: 42695,
        },
    ];

    #[test]
    fn serialize() {

        let writer = WriterBuilder::default().build();

        let i = Iter::new(ROWS, writer);

        let buf = i
            .map(Result::unwrap)
            .flatten()
            .collect();

        let buf = String::from_utf8(buf).unwrap();

        assert_eq!(
            buf,
            r#"city,country,popcount
Boston,United States,4628910
Concord,United States,42695
"#
        )
    }

    #[test]
    fn config() {
        let writer = WriterBuilder::default()
            .has_headers(false)
            .delimiter(b';')
            .terminator(Terminator::CRLF)
            .build();

        let i = Iter::new(ROWS, writer);

        let buf = i
            .map(Result::unwrap)
            .flatten()
            .collect();

        let buf = String::from_utf8(buf).unwrap();

        assert_eq!(
            buf,
            r#"Boston;United States;4628910
Concord;United States;42695
"#.replace("\n", "\r\n")
        )
    }
}