From 48e525c0a76811e9bbd43094ed76518e3690f85b Mon Sep 17 00:00:00 2001 From: Aurash Karimi Date: Wed, 29 Oct 2025 14:12:39 +0000 Subject: [PATCH] add trimming to csv reader in main csv reader function add test for csv files with whitespace --- src/input.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/input.rs b/src/input.rs index 1886c55e3..45c35e23c 100644 --- a/src/input.rs +++ b/src/input.rs @@ -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> { - 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()) @@ -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 = 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::(&file_path).is_err());