1- use crate :: io:: { OptionalPaths , OutputContext , PairedErrors , RecordWriters , WriteFileZipStdout , open_options:: PairedStruct } ;
1+ use crate :: io:: {
2+ OptionalPaths , OutputContext , PairedErrors , RecordWriters , WriteFileZipStdout , WriterWithContext ,
3+ open_options:: PairedStruct ,
4+ } ;
25use std:: { fs:: File , io:: BufWriter , path:: Path } ;
36
47/// A builder pattern for creating output files in IRMA-core.
@@ -89,16 +92,8 @@ impl<'a> OutputOptions<'a, &'a Path> {
8992 }
9093
9194 /// Interprets the path using [`File`] for writing.
92- #[ allow( dead_code) ]
93- pub fn use_file ( self ) -> OutputOptions < ' a , BufWriter < File > > {
94- let output = self . output . and_then ( |path| {
95- if let Some ( capacity) = self . capacity {
96- File :: create ( path) . map ( |file| BufWriter :: with_capacity ( capacity, file) )
97- } else {
98- File :: create ( path) . map ( BufWriter :: new)
99- }
100- . map_err ( PairedErrors :: Err1 )
101- } ) ;
95+ pub fn use_file ( self ) -> OutputOptions < ' a , File > {
96+ let output = self . output . and_then ( |path| File :: create ( path) . map_err ( PairedErrors :: Err1 ) ) ;
10297
10398 OutputOptions {
10499 context : self . context ,
@@ -184,18 +179,10 @@ impl<'a> OutputOptions<'a, RecordWriters<&'a Path>> {
184179
185180 /// Interprets the path(s) using [`File`] for writing.
186181 #[ allow( dead_code) ]
187- pub fn use_file ( self ) -> OutputOptions < ' a , RecordWriters < BufWriter < File > > > {
182+ pub fn use_file ( self ) -> OutputOptions < ' a , RecordWriters < File > > {
188183 OutputOptions {
189184 context : self . context ,
190- output : self . output . and_then ( |writers| {
191- writers. try_map ( |path| {
192- if let Some ( capacity) = self . capacity {
193- File :: create ( path) . map ( |file| BufWriter :: with_capacity ( capacity, file) )
194- } else {
195- File :: create ( path) . map ( BufWriter :: new)
196- }
197- } )
198- } ) ,
185+ output : self . output . and_then ( |writers| writers. try_map ( File :: create) ) ,
199186 capacity : self . capacity ,
200187 }
201188 }
@@ -252,16 +239,26 @@ impl<'a> OutputOptions<'a, OptionalPaths<'a>> {
252239 }
253240}
254241
255- impl < ' a > OutputOptions < ' a , BufWriter < File > > {
242+ impl < ' a > OutputOptions < ' a , File > {
256243 /// Opens the [`File`] for writing, wrapping it in a [`BufWriter`].
257244 ///
258245 /// ## Errors
259246 ///
260247 /// IO errors when opening the file are propagated. Context is added that
261248 /// includes the path.
262- pub fn open ( self ) -> std:: io:: Result < BufWriter < File > > {
263- match self . output {
264- Ok ( writer) => Ok ( writer) ,
249+ pub fn open ( self ) -> std:: io:: Result < BufWriter < WriterWithContext < File > > > {
250+ let output = self . output . map ( |file| {
251+ let output = OutputContext :: add_writer_context ( file, self . context . output1 ) ;
252+
253+ if let Some ( capacity) = self . capacity {
254+ BufWriter :: with_capacity ( capacity, output)
255+ } else {
256+ BufWriter :: new ( output)
257+ }
258+ } ) ;
259+
260+ match output {
261+ Ok ( output) => Ok ( output) ,
265262 Err ( e) => Err ( self . context . add_context ( e) . into ( ) ) ,
266263 }
267264 }
@@ -282,7 +279,7 @@ impl<'a> OutputOptions<'a, WriteFileZipStdout> {
282279 }
283280}
284281
285- impl < ' a > OutputOptions < ' a , RecordWriters < BufWriter < File > > > {
282+ impl < ' a > OutputOptions < ' a , RecordWriters < File > > {
286283 /// Opens the potentially paired [`File`]s for writing, wrapping each in a
287284 /// [`BufWriter`].
288285 ///
@@ -292,8 +289,18 @@ impl<'a> OutputOptions<'a, RecordWriters<BufWriter<File>>> {
292289 /// includes the path.
293290 #[ allow( dead_code) ]
294291 pub fn open ( self ) -> std:: io:: Result < RecordWriters < BufWriter < File > > > {
295- match self . output {
296- Ok ( writer) => Ok ( writer) ,
292+ let output = self . output . map ( |writers| {
293+ writers. map ( |file| {
294+ if let Some ( capacity) = self . capacity {
295+ BufWriter :: with_capacity ( capacity, file)
296+ } else {
297+ BufWriter :: new ( file)
298+ }
299+ } )
300+ } ) ;
301+
302+ match output {
303+ Ok ( output) => Ok ( output) ,
297304 Err ( e) => Err ( self . context . add_context ( e) . into ( ) ) ,
298305 }
299306 }
0 commit comments