From 3d734f731442a3781954903d62a61f2b9c3d686d Mon Sep 17 00:00:00 2001 From: Wolfgang Silbermayr Date: Tue, 4 Sep 2018 11:10:00 +0200 Subject: Update csv to version 1 --- Cargo.toml | 2 +- examples/csv.rs | 3 ++- src/lib.rs | 45 ++++++++++++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0af2394..7697053 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,4 +34,4 @@ term = "^0.5" lazy_static = "1" atty = "^0.2" encode_unicode = "^0.3" -csv = { version = "0.15", optional = true } +csv = { version = "1", optional = true } diff --git a/examples/csv.rs b/examples/csv.rs index dd104af..8dbaab1 100644 --- a/examples/csv.rs +++ b/examples/csv.rs @@ -25,7 +25,8 @@ fn main() { table.printstd(); println!(""); - println!("{}", table.to_csv(Vec::new()).unwrap().into_string()); + println!("{}", + String::from_utf8(table.to_csv(Vec::new()).unwrap().into_inner().unwrap()).unwrap()); } #[cfg(not(feature = "csv"))] diff --git a/src/lib.rs b/src/lib.rs index a3adbfe..906f97b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -215,10 +215,10 @@ impl<'a> TableSlice<'a> { mut writer: csv::Writer) -> csv::Result> { for title in self.titles { - writer.write(title.iter().map(|c| c.get_content()))?; + writer.write_record(title.iter().map(|c| c.get_content()))?; } for row in self.rows { - writer.write(row.iter().map(|c| c.get_content()))?; + writer.write_record(row.iter().map(|c| c.get_content()))?; } writer.flush()?; @@ -254,7 +254,10 @@ impl Table { /// 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::Reader::from_string(csv_s).has_headers(false))) + Ok(Table::from_csv( + &mut csv::ReaderBuilder::new() + .has_headers(false) + .from_reader(csv_s.as_bytes()))) } /// Create a table from a CSV file @@ -262,7 +265,10 @@ impl Table { /// 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::Reader::from_file(csv_p)?.has_headers(false))) + Ok(Table::from_csv( + &mut csv::ReaderBuilder::new() + .has_headers(false) + .from_path(csv_p)?)) } /// Create a table from a CSV reader @@ -1056,19 +1062,32 @@ mod tests { #[test] fn to() { - assert_eq!(test_table().to_csv(Vec::new()).unwrap().as_string(), CSV_S); + 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(test_table() - .to_csv(Vec::new()) - .unwrap() - .as_string()) - .unwrap() - .to_string() - .replace("\r\n", "\n"), - test_table().to_string().replace("\r\n", "\n")); + 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] -- cgit v1.2.3