Skip to content

Commit 023862f

Browse files
committed
enable missing optional csv files
1 parent 9f77d38 commit 023862f

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/input.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ pub fn read_csv<'a, T: DeserializeOwned + 'a>(
5656
pub fn read_csv_optional<'a, T: DeserializeOwned + 'a>(
5757
file_path: &'a Path,
5858
) -> Result<impl Iterator<Item = T> + 'a> {
59+
if !file_path.exists() {
60+
return Ok(Vec::new().into_iter());
61+
}
62+
5963
let vec = read_csv_internal(file_path)?;
6064
Ok(vec.into_iter())
6165
}
@@ -347,6 +351,20 @@ mod tests {
347351
.next()
348352
.is_none()
349353
);
354+
355+
// Missing file
356+
let dir = tempdir().unwrap();
357+
let file_path = dir.path().join("a_missing_file.csv");
358+
assert!(!file_path.exists());
359+
assert!(read_csv::<Record>(&file_path).is_err());
360+
// optional csv's should return empty iterator
361+
assert!(
362+
read_csv_optional::<Record>(&file_path)
363+
.unwrap()
364+
.peekable()
365+
.peek()
366+
.is_none()
367+
);
350368
}
351369

352370
#[test]

0 commit comments

Comments
 (0)