11use crate :: io:: {
2- IterWithContext , IterWithErrorContext , PairedWriters , RecordReaders , RecordWriters , WriterWithContext ,
3- WriterWithErrorContext ,
2+ IterWithContext , IterWithErrorContext , PairedWriters , ReaderWithContext , ReaderWithErrorContext , RecordReaders ,
3+ RecordWriters , WriterWithContext , WriterWithErrorContext ,
4+ } ;
5+ use std:: {
6+ error:: Error ,
7+ fmt:: Display ,
8+ io:: { Read , Write } ,
9+ path:: Path ,
410} ;
5- use std:: { error:: Error , fmt:: Display , io:: Write , path:: Path } ;
611use zoe:: data:: err:: { ErrorWithContext , WithErrorContext } ;
712
813/// An enum to represent the possible reader types for [`InputOptions`], for the
@@ -231,7 +236,7 @@ impl InputContext<'_> {
231236 }
232237
233238 /// Wrap the fallible iterators contained in a [`RecordReaders`] so that
234- /// failed reads have context added.
239+ /// items that are errors have context added.
235240 pub fn add_paired_iter_context < I > ( & self , iters : RecordReaders < I > ) -> RecordReaders < IterWithContext < I > >
236241 where
237242 I : IterWithErrorContext , {
@@ -242,6 +247,31 @@ impl InputContext<'_> {
242247 . map ( |iter| InputContext :: add_iter_context ( iter, self . reader2 , self . input2 ) ) ,
243248 }
244249 }
250+
251+ /// Adds context to a reader, so that failed reads have context.
252+ ///
253+ /// `reader` and `input` should be corresponding fields in an
254+ /// [`InputContext`] struct. The context will include the path if available
255+ /// and the record type.
256+ pub fn add_reader_context < R > ( reader : R , input : InputType ) -> ReaderWithContext < R >
257+ where
258+ R : Read , {
259+ match input {
260+ InputType :: File ( path) => reader. reader_with_file_context ( "Failed to read from file" , path) ,
261+ InputType :: Stdin => reader. reader_with_context ( "Failed to read from stdin" ) ,
262+ }
263+ }
264+
265+ /// Wrap the readers contained in a [`RecordReaders`] so that failed reads
266+ /// have context added.
267+ pub fn add_paired_reader_context < R > ( & self , readers : RecordReaders < R > ) -> RecordReaders < ReaderWithContext < R > >
268+ where
269+ R : Read , {
270+ RecordReaders {
271+ reader1 : InputContext :: add_reader_context ( readers. reader1 , self . input1 ) ,
272+ reader2 : readers. reader2 . map ( |r| InputContext :: add_reader_context ( r, self . input2 ) ) ,
273+ }
274+ }
245275}
246276
247277/// The complete context for [`OutputOptions`] necessary for displaying any
0 commit comments