-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pub mod json; | ||
pub mod jsonlines; | ||
|
||
#[cfg(feature = "csv")] | ||
pub mod csv; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::io::{Read, Write}; | ||
|
||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
use crate::common::Result; | ||
|
||
pub fn read<T: DeserializeOwned>(reader: impl Read) -> Result<Vec<T>> { | ||
let mut rdr = csv::Reader::from_reader(reader); | ||
let mut records: Vec<T> = Vec::new(); | ||
for result in rdr.deserialize() { | ||
let record: T = result?; | ||
records.push(record); | ||
} | ||
Ok(records) | ||
} | ||
|
||
pub fn write<T: Serialize>(writer: impl Write, records: &[T]) -> Result<()> { | ||
let mut wtr = csv::Writer::from_writer(writer); | ||
for record in records { | ||
wtr.serialize(record)?; | ||
} | ||
wtr.flush()?; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::io::Cursor; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{read, write}; | ||
|
||
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)] | ||
struct Record { | ||
id: u32, | ||
name: String, | ||
} | ||
|
||
#[test] | ||
fn test_read() { | ||
let text = r#" | ||
id,name | ||
1,foo | ||
2,bar"#; | ||
|
||
let records: Vec<Record> = read(Cursor::new(text)).unwrap(); | ||
let expected = vec![ | ||
Record { | ||
id: 1, | ||
name: "foo".to_owned(), | ||
}, | ||
Record { | ||
id: 2, | ||
name: "bar".to_owned(), | ||
}, | ||
]; | ||
assert_eq!(expected, records); | ||
} | ||
|
||
#[test] | ||
fn test_write() { | ||
let records = vec![ | ||
Record { | ||
id: 1, | ||
name: "foo".to_owned(), | ||
}, | ||
Record { | ||
id: 2, | ||
name: "bar".to_owned(), | ||
}, | ||
]; | ||
let mut cursor = Cursor::new(vec![]); | ||
write(&mut cursor, &records).unwrap(); | ||
let data = String::from_utf8(cursor.into_inner()) | ||
.unwrap() | ||
.trim() | ||
.to_string(); | ||
let expected = r#" | ||
id,name | ||
1,foo | ||
2,bar"# | ||
.trim() | ||
.to_owned(); | ||
assert_eq!(expected, data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters