Skip to content

Commit

Permalink
Merge pull request #84 from silwol/csv-1
Browse files Browse the repository at this point in the history
Update csv to version 1
  • Loading branch information
phsym authored Sep 16, 2018
2 parents ece1d8e + 3d734f7 commit 550ab35
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
3 changes: 2 additions & 1 deletion examples/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
45 changes: 32 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ impl<'a> TableSlice<'a> {
mut writer: csv::Writer<W>)
-> csv::Result<csv::Writer<W>> {
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()?;
Expand Down Expand Up @@ -254,15 +254,21 @@ impl Table {
/// 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::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
///
/// 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::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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 550ab35

Please sign in to comment.