Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ pub fn read_csv_optional<'a, T: DeserializeOwned + 'a>(
}

fn read_csv_internal<'a, T: DeserializeOwned + 'a>(file_path: &'a Path) -> Result<Vec<T>> {
let vec = csv::Reader::from_path(file_path)
let vec = csv::ReaderBuilder::new()
.trim(csv::Trim::All)
.from_path(file_path)
.with_context(|| input_err_msg(file_path))?
.into_deserialize()
.process_results(|iter| iter.collect_vec())
Expand Down Expand Up @@ -345,6 +347,24 @@ mod tests {
]
);

// File with leading/trailing whitespace
let dir = tempdir().unwrap();
let file_path = create_csv_file(dir.path(), "id , value\t\n hello\t ,1\n world ,2\n");
let records: Vec<Record> = read_csv(&file_path).unwrap().collect();
assert_eq!(
records,
&[
Record {
id: "hello".into(),
value: 1,
},
Record {
id: "world".into(),
value: 2,
}
]
);

// File with no data (only column headers)
let file_path = create_csv_file(dir.path(), "id,value\n");
assert!(read_csv::<Record>(&file_path).is_err());
Expand Down