summaryrefslogtreecommitdiffstats
path: root/src/csv.rs
blob: cb8ec7262ece85efbbcfa2bc2e7ab49368437cf2 (plain)
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
//! CSV impl and reexported types

extern crate csv;

pub use self::csv::{Reader, Writer, Result, ReaderBuilder};
use std::path::Path;
use std::io::{Read, Write};

impl<'a> super::TableSlice<'a> {
    /// Write the table to the specified writer.
    pub fn to_csv<W: Write>(&self, w: W) -> Result<Writer<W>> {
        self.to_csv_writer(Writer::from_writer(w))
    }

    /// Write the table to the specified writer.
    ///
    /// This allows for format customisation.
    pub fn to_csv_writer<W: Write>(&self,
                                mut writer: Writer<W>)
                                -> Result<Writer<W>> {
        for title in self.titles {
            writer.write_record(title.iter().map(|c| c.get_content()))?;
        }
        for row in self.rows {
            writer.write_record(row.iter().map(|c| c.get_content()))?;
        }

        writer.flush()?;
        Ok(writer)
    }
}

impl super::Table {
    /// Create a table from a CSV string
    ///
    /// For more customisability use `from_csv()`
    pub fn from_csv_string(csv_s: &str) -> Result<Self> {
        Ok(Self::from_csv(
            &mut ReaderBuilder::new()
                .has_headers(false)
                .from_reader(csv_s.as_bytes())))
    }

    /// Create a table from a CSV file
    ///
    /// For more customisability use `from_csv()`
    pub fn from_csv_file<P: AsRef<Path>>(csv_p: P) -> Result<Self> {
        Ok(Self::from_csv(
            &mut ReaderBuilder::new()
                .has_headers(false)
                .from_path(csv_p)?))
    }

    /// Create a table from a CSV reader
    pub fn from_csv<R: Read>(reader: &mut Reader<R>) -> Self {
        Self::init(reader
                        .records()
                        .map(|row| {
                                super::row::Row::new(row.unwrap()
                                            .into_iter()
                                            .map(|cell| super::cell::Cell::new(&cell))
                                            .collect())
                            })
                        .collect())
    }

    
    /// Write the table to the specified writer.
    pub fn to_csv<W: Write>(&self, w: W) -> Result<Writer<W>> {
        self.as_ref().to_csv(w)
    }

    /// Write the table to the specified writer.
    ///
    /// This allows for format customisation.
    pub fn to_csv_writer<W: Write>(&self, writer: Writer<W>) -> Result<Writer<W>> {
        self.as_ref().to_csv_writer(writer)
    }
}