diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 167 |
1 files changed, 7 insertions, 160 deletions
@@ -11,11 +11,7 @@ extern crate lazy_static; extern crate encode_unicode; use std::io::{self, Write, Error}; -#[cfg(feature = "csv")] -use std::io::Read; use std::fmt; -#[cfg(feature = "csv")] -use std::path::Path; use std::iter::{FromIterator, IntoIterator}; use std::slice::{Iter, IterMut}; use std::ops::{Index, IndexMut}; @@ -24,20 +20,16 @@ use std::mem::transmute; pub use term::{Attr, color}; pub(crate) use term::{Terminal, stdout}; -pub mod cell; -pub mod row; +mod cell; +mod row; pub mod format; mod utils; -/// Reexported types for CSV Read/Write #[cfg(feature = "csv")] -pub mod csv { - extern crate csv; - pub use self::csv::{Reader, Writer, Result, ReaderBuilder}; -} +pub mod csv; -use row::Row; -use cell::Cell; +pub use row::Row; +pub use cell::Cell; use format::{TableFormat, LinePosition, consts}; use utils::StringWriter; @@ -207,30 +199,6 @@ impl<'a> TableSlice<'a> { pub fn printstd(&self) { self.print_tty(false); } - - /// Write the table to the specified writer. - #[cfg(feature = "csv")] - pub fn to_csv<W: Write>(&self, w: W) -> csv::Result<csv::Writer<W>> { - self.to_csv_writer(csv::Writer::from_writer(w)) - } - - /// Write the table to the specified writer. - /// - /// This allows for format customisation. - #[cfg(feature = "csv")] - pub fn to_csv_writer<W: Write>(&self, - mut writer: csv::Writer<W>) - -> csv::Result<csv::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<'a> IntoIterator for &'a TableSlice<'a> { @@ -256,42 +224,6 @@ impl Table { } } - /// Create a table from a CSV string - /// - /// For more customisability use `from_csv()` - #[cfg(feature = "csv")] - pub fn from_csv_string(csv_s: &str) -> csv::Result<Table> { - Ok(Table::from_csv( - &mut csv::ReaderBuilder::new() - .has_headers(false) - .from_reader(csv_s.as_bytes()))) - } - - /// Create a table from a CSV file - /// - /// For more customisability use `from_csv()` - #[cfg(feature = "csv")] - pub fn from_csv_file<P: AsRef<Path>>(csv_p: P) -> csv::Result<Table> { - Ok(Table::from_csv( - &mut csv::ReaderBuilder::new() - .has_headers(false) - .from_path(csv_p)?)) - } - - /// Create a table from a CSV reader - #[cfg(feature = "csv")] - pub fn from_csv<R: Read>(reader: &mut csv::Reader<R>) -> Table { - Table::init(reader - .records() - .map(|row| { - Row::new(row.unwrap() - .into_iter() - .map(|cell| Cell::new(&cell)) - .collect()) - }) - .collect()) - } - /// Change the table format. Eg : Separators pub fn set_format(&mut self, format: TableFormat) { *self.format = format; @@ -429,19 +361,6 @@ impl Table { self.as_ref().printstd(); } - /// Write the table to the specified writer. - #[cfg(feature = "csv")] - pub fn to_csv<W: Write>(&self, w: W) -> csv::Result<csv::Writer<W>> { - self.as_ref().to_csv(w) - } - - /// Write the table to the specified writer. - /// - /// This allows for format customisation. - #[cfg(feature = "csv")] - pub fn to_csv_writer<W: Write>(&self, writer: csv::Writer<W>) -> csv::Result<csv::Writer<W>> { - self.as_ref().to_csv_writer(writer) - } } impl Index<usize> for Table { @@ -656,8 +575,8 @@ macro_rules! ptable { mod tests { use Table; use Slice; - use row::Row; - use cell::Cell; + use Row; + use Cell; use format; use format::consts::{FORMAT_DEFAULT, FORMAT_NO_LINESEP, FORMAT_NO_COLSEP, FORMAT_CLEAN}; @@ -1058,76 +977,4 @@ mod tests { println!("{}", table.to_string().replace("\r\n","\n")); assert_eq!(out, table.to_string().replace("\r\n","\n")); } - - #[cfg(feature = "csv")] - mod csv { - use Table; - use row::Row; - use cell::Cell; - - static CSV_S: &'static str = "ABC,DEFG,HIJKLMN\n\ - foobar,bar,foo\n\ - foobar2,bar2,foo2\n"; - - fn test_table() -> Table { - let mut table = Table::new(); - table - .add_row(Row::new(vec![Cell::new("ABC"), Cell::new("DEFG"), Cell::new("HIJKLMN")])); - table.add_row(Row::new(vec![Cell::new("foobar"), Cell::new("bar"), Cell::new("foo")])); - table.add_row(Row::new(vec![Cell::new("foobar2"), - Cell::new("bar2"), - Cell::new("foo2")])); - table - } - - #[test] - fn from() { - assert_eq!(test_table().to_string().replace("\r\n", "\n"), - Table::from_csv_string(CSV_S) - .unwrap() - .to_string() - .replace("\r\n", "\n")); - } - - #[test] - fn to() { - assert_eq!( - String::from_utf8( - test_table() - .to_csv(Vec::new()) - .unwrap() - .into_inner() - .unwrap() - ).unwrap(), - CSV_S); - } - - #[test] - fn trans() { - assert_eq!( - Table::from_csv_string( - &String::from_utf8( - test_table() - .to_csv(Vec::new()) - .unwrap() - .into_inner() - .unwrap() - ).unwrap() - ).unwrap() - .to_string() - .replace("\r\n", "\n"), - test_table().to_string().replace("\r\n", "\n")); - } - - #[test] - fn extend_table() { - let mut table = Table::new(); - table.add_row(Row::new(vec![Cell::new("ABC"), Cell::new("DEFG"), Cell::new("HIJKLMN")])); - table.extend(vec![vec!["A", "B", "C"]]); - let t2 = table.clone(); - table.extend(t2.rows); - assert_eq!(table.get_row(1).unwrap().get_cell(2).unwrap().get_content(), "C"); - assert_eq!(table.get_row(2).unwrap().get_cell(1).unwrap().get_content(), "DEFG"); - } - } } |