|
2 | 2 | extern crate unicode_width;
|
3 | 3 | extern crate term;
|
4 | 4 | extern crate atty;
|
| 5 | +#[cfg(feature = "csv")] |
5 | 6 | extern crate csv;
|
6 | 7 | #[macro_use] extern crate lazy_static;
|
7 | 8 | extern crate encode_unicode;
|
8 | 9 |
|
9 |
| -use std::io::{self, Read, Write, Error}; |
| 10 | +use std::io::{self, Write, Error}; |
| 11 | +#[cfg(feature = "csv")] |
| 12 | +use std::io::Read; |
10 | 13 | use std::fmt;
|
| 14 | +#[cfg(feature = "csv")] |
11 | 15 | use std::path::Path;
|
12 | 16 | use std::iter::{FromIterator, IntoIterator};
|
13 | 17 | use std::slice::{Iter, IterMut};
|
@@ -182,13 +186,15 @@ impl <'a> TableSlice<'a> {
|
182 | 186 | }
|
183 | 187 |
|
184 | 188 | /// Write the table to the specified writer.
|
| 189 | + #[cfg(feature = "csv")] |
185 | 190 | pub fn to_csv<W: Write>(&self, w: W) -> csv::Result<csv::Writer<W>> {
|
186 | 191 | self.to_csv_writer(csv::Writer::from_writer(w))
|
187 | 192 | }
|
188 | 193 |
|
189 | 194 | /// Write the table to the specified writer.
|
190 | 195 | ///
|
191 | 196 | /// This allows for format customisation.
|
| 197 | + #[cfg(feature = "csv")] |
192 | 198 | pub fn to_csv_writer<W: Write>(&self, mut writer: csv::Writer<W>) -> csv::Result<csv::Writer<W>> {
|
193 | 199 | for title in self.titles {
|
194 | 200 | try!(writer.write(title.iter().map(|c| c.get_content())));
|
@@ -228,18 +234,21 @@ impl Table {
|
228 | 234 | /// Create a table from a CSV string
|
229 | 235 | ///
|
230 | 236 | /// For more customisability use `from_csv()`
|
| 237 | + #[cfg(feature = "csv")] |
231 | 238 | pub fn from_csv_string(csv_s: &str) -> csv::Result<Table> {
|
232 | 239 | Ok(Table::from_csv(&mut csv::Reader::from_string(csv_s).has_headers(false)))
|
233 | 240 | }
|
234 | 241 |
|
235 | 242 | /// Create a table from a CSV file
|
236 | 243 | ///
|
237 | 244 | /// For more customisability use `from_csv()`
|
| 245 | + #[cfg(feature = "csv")] |
238 | 246 | pub fn from_csv_file<P: AsRef<Path>>(csv_p: P) -> csv::Result<Table> {
|
239 | 247 | Ok(Table::from_csv(&mut try!(csv::Reader::from_file(csv_p)).has_headers(false)))
|
240 | 248 | }
|
241 | 249 |
|
242 | 250 | /// Create a table from a CSV reader
|
| 251 | + #[cfg(feature = "csv")] |
243 | 252 | pub fn from_csv<R: Read>(reader: &mut csv::Reader<R>) -> Table {
|
244 | 253 | Table::init(reader.records().map(|row| Row::new(row.unwrap().into_iter().map(|cell| Cell::new(&cell)).collect())).collect())
|
245 | 254 | }
|
@@ -371,13 +380,15 @@ impl Table {
|
371 | 380 | }
|
372 | 381 |
|
373 | 382 | /// Write the table to the specified writer.
|
| 383 | + #[cfg(feature = "csv")] |
374 | 384 | pub fn to_csv<W: Write>(&self, w: W) -> csv::Result<csv::Writer<W>> {
|
375 | 385 | self.as_ref().to_csv(w)
|
376 | 386 | }
|
377 | 387 |
|
378 | 388 | /// Write the table to the specified writer.
|
379 | 389 | ///
|
380 | 390 | /// This allows for format customisation.
|
| 391 | + #[cfg(feature = "csv")] |
381 | 392 | pub fn to_csv_writer<W: Write>(&self, writer: csv::Writer<W>) -> csv::Result<csv::Writer<W>> {
|
382 | 393 | self.as_ref().to_csv_writer(writer)
|
383 | 394 | }
|
@@ -739,6 +750,7 @@ mod tests {
|
739 | 750 | assert_eq!(out, table.to_string().replace("\r\n", "\n"));
|
740 | 751 | }
|
741 | 752 |
|
| 753 | + #[cfg(feature = "csv")] |
742 | 754 | mod csv {
|
743 | 755 | use Table;
|
744 | 756 | use row::Row;
|
|
0 commit comments