File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -56,6 +56,10 @@ pub fn read_csv<'a, T: DeserializeOwned + 'a>(
5656pub 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]
You can’t perform that action at this time.
0 commit comments