From 122c7141f857b9711c2dcaf96af926c9ba2e2aa5 Mon Sep 17 00:00:00 2001 From: Pierre-Henri Symoneaux Date: Fri, 21 Sep 2018 14:45:17 +0200 Subject: Move all CSV stuff into dedicated module --- src/lib.rs | 153 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 75 insertions(+), 78 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f3ef9bf..323003d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; @@ -29,11 +25,85 @@ pub mod row; pub mod format; mod utils; -/// Reexported types for CSV Read/Write +/// CSV impl and reexported types #[cfg(feature = "csv")] pub mod csv { 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(&self, w: W) -> Result> { + 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(&self, + mut writer: Writer) + -> Result> { + 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 { + 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>(csv_p: P) -> Result { + 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(reader: &mut Reader) -> 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(&self, w: W) -> Result> { + self.as_ref().to_csv(w) + } + + /// Write the table to the specified writer. + /// + /// This allows for format customisation. + pub fn to_csv_writer(&self, writer: Writer) -> Result> { + self.as_ref().to_csv_writer(writer) + } + } } use row::Row; @@ -207,30 +277,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(&self, w: W) -> csv::Result> { - 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(&self, - mut writer: csv::Writer) - -> csv::Result> { - 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 +302,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 { - 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>(csv_p: P) -> csv::Result
{ - 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(reader: &mut csv::Reader) -> 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 +439,6 @@ impl Table { self.as_ref().printstd(); } - /// Write the table to the specified writer. - #[cfg(feature = "csv")] - pub fn to_csv(&self, w: W) -> csv::Result> { - 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(&self, writer: csv::Writer) -> csv::Result> { - self.as_ref().to_csv_writer(writer) - } } impl Index for Table { -- cgit v1.2.3 From a1440b5dbb6db5ff6dcdd5a6e4eb27fd5d44ab87 Mon Sep 17 00:00:00 2001 From: Pierre-Henri Symoneaux Date: Fri, 21 Sep 2018 14:54:29 +0200 Subject: Moved CSV module into dedicated file --- src/csv.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 80 +------------------------------------------------------------- 2 files changed, 80 insertions(+), 79 deletions(-) create mode 100644 src/csv.rs diff --git a/src/csv.rs b/src/csv.rs new file mode 100644 index 0000000..cb8ec72 --- /dev/null +++ b/src/csv.rs @@ -0,0 +1,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(&self, w: W) -> Result> { + 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(&self, + mut writer: Writer) + -> Result> { + 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 { + 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>(csv_p: P) -> Result { + 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(reader: &mut Reader) -> 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(&self, w: W) -> Result> { + self.as_ref().to_csv(w) + } + + /// Write the table to the specified writer. + /// + /// This allows for format customisation. + pub fn to_csv_writer(&self, writer: Writer) -> Result> { + self.as_ref().to_csv_writer(writer) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 323003d..4bf3521 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,86 +25,8 @@ pub mod row; pub mod format; mod utils; -/// CSV impl and reexported types #[cfg(feature = "csv")] -pub mod csv { - 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(&self, w: W) -> Result> { - 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(&self, - mut writer: Writer) - -> Result> { - 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 { - 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>(csv_p: P) -> Result { - 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(reader: &mut Reader) -> 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(&self, w: W) -> Result> { - self.as_ref().to_csv(w) - } - - /// Write the table to the specified writer. - /// - /// This allows for format customisation. - pub fn to_csv_writer(&self, writer: Writer) -> Result> { - self.as_ref().to_csv_writer(writer) - } - } -} +pub mod csv; use row::Row; use cell::Cell; -- cgit v1.2.3 From dee6df4f336bc82247031f5576bb8d78eb773190 Mon Sep 17 00:00:00 2001 From: Pierre-Henri Symoneaux Date: Fri, 21 Sep 2018 15:32:59 +0200 Subject: cell and row mods become private. Row and Cell are reexported in crate's root --- README.md | 4 +-- examples/basic.rs | 4 +-- examples/span.rs | 2 +- examples/style.rs | 4 +-- src/cell.rs | 6 ++-- src/csv.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 84 ++++--------------------------------------------------- src/main.rs | 4 +-- src/row.rs | 12 ++++---- 9 files changed, 94 insertions(+), 101 deletions(-) diff --git a/README.md b/README.md index af660a4..28404a2 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,7 @@ Start using it like this: ```rust #[macro_use] extern crate prettytable; -use prettytable::Table; -use prettytable::row::Row; -use prettytable::cell::Cell; +use prettytable::{Table, Row, Cell}; fn main() { // Create the table diff --git a/examples/basic.rs b/examples/basic.rs index fe397d8..1e5cf35 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,8 +1,6 @@ #[macro_use] extern crate prettytable; -use prettytable::Table; -use prettytable::row::Row; -use prettytable::cell::Cell; +use prettytable::{Table, Row, Cell}; /* Following main function will print : diff --git a/examples/span.rs b/examples/span.rs index 61e1815..10f1ef7 100644 --- a/examples/span.rs +++ b/examples/span.rs @@ -1,6 +1,6 @@ #[macro_use] extern crate prettytable; -use prettytable::{row::Row, cell::Cell, format::Alignment}; +use prettytable::{Row, Cell, format::Alignment}; fn main() { diff --git a/examples/style.rs b/examples/style.rs index b7cef57..1d93e07 100644 --- a/examples/style.rs +++ b/examples/style.rs @@ -1,8 +1,6 @@ #[macro_use] extern crate prettytable; -use prettytable::Table; -use prettytable::row::Row; -use prettytable::cell::Cell; +use prettytable::{Table, Row, Cell}; use prettytable::{Attr, color}; diff --git a/src/cell.rs b/src/cell.rs index 1a3461d..bc25cec 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -308,10 +308,10 @@ impl Default for Cell { #[macro_export] macro_rules! cell { () => { - $crate::cell::Cell::default() + $crate::Cell::default() }; ($value:expr) => { - $crate::cell::Cell::new(&$value.to_string()) + $crate::Cell::new(&$value.to_string()) }; ($style:ident -> $value:expr) => { cell!($value).style_spec(stringify!($style)) @@ -320,7 +320,7 @@ macro_rules! cell { #[cfg(test)] mod tests { - use cell::Cell; + use Cell; use format::Alignment; use term::{color, Attr}; use utils::StringWriter; diff --git a/src/csv.rs b/src/csv.rs index cb8ec72..55f10e2 100644 --- a/src/csv.rs +++ b/src/csv.rs @@ -56,9 +56,9 @@ impl super::Table { Self::init(reader .records() .map(|row| { - super::row::Row::new(row.unwrap() + super::Row::new(row.unwrap() .into_iter() - .map(|cell| super::cell::Cell::new(&cell)) + .map(|cell| super::Cell::new(&cell)) .collect()) }) .collect()) @@ -76,4 +76,75 @@ impl super::Table { pub fn to_csv_writer(&self, writer: Writer) -> Result> { self.as_ref().to_csv_writer(writer) } +} + + +#[cfg(test)] +mod tests { + use {Table, Row, 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"); + } } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 4bf3521..e539b5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,16 +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; #[cfg(feature = "csv")] 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; @@ -575,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}; @@ -977,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"); - } - } } diff --git a/src/main.rs b/src/main.rs index c653413..af9b745 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,8 @@ #[macro_use] extern crate prettytable; use prettytable::Table; -use prettytable::row::Row; -use prettytable::cell::Cell; +use prettytable::Row; +use prettytable::Cell; use prettytable::format::*; use prettytable::{Attr, color}; diff --git a/src/row.rs b/src/row.rs index 75ce89a..775a3d8 100644 --- a/src/row.rs +++ b/src/row.rs @@ -8,7 +8,7 @@ use std::ops::{Index, IndexMut}; use super::Terminal; use super::utils::NEWLINE; -use super::cell::Cell; +use super::Cell; use super::format::{TableFormat, ColumnPosition}; /// Represent a table row made of cells @@ -312,16 +312,16 @@ macro_rules! row { (($($out:tt)*); $style:ident -> $value:expr) => (vec![$($out)* cell!($style -> $value)]); (($($out:tt)*); $style:ident -> $value:expr, $($n: tt)*) => (row!(($($out)* cell!($style -> $value),); $($n)*)); - ($($content:expr), *) => ($crate::row::Row::new(vec![$(cell!($content)), *])); // This line may not be needed starting from Rust 1.20 - ($style:ident => $($content:expr), *) => ($crate::row::Row::new(vec![$(cell!($style -> $content)), *])); - ($style:ident => $($content:expr,) *) => ($crate::row::Row::new(vec![$(cell!($style -> $content)), *])); - ($($content:tt)*) => ($crate::row::Row::new(row!((); $($content)*))); + ($($content:expr), *) => ($crate::Row::new(vec![$(cell!($content)), *])); // This line may not be needed starting from Rust 1.20 + ($style:ident => $($content:expr), *) => ($crate::Row::new(vec![$(cell!($style -> $content)), *])); + ($style:ident => $($content:expr,) *) => ($crate::Row::new(vec![$(cell!($style -> $content)), *])); + ($($content:tt)*) => ($crate::Row::new(row!((); $($content)*))); } #[cfg(test)] mod tests { use super::*; - use cell::Cell; + use Cell; #[test] fn row_default_empty() { -- cgit v1.2.3