@@ -6,31 +6,57 @@ use std::fs::OpenOptions;
66use std:: io:: { self , Write } ;
77
88pub struct StdoutSink {
9- output : Box < dyn Write > ,
9+ output : Option < Box < dyn Write > > ,
10+ path : Option < String > ,
1011 format : AudioFormat ,
1112}
1213
1314impl Open for StdoutSink {
1415 fn open ( path : Option < String > , format : AudioFormat ) -> Self {
1516 info ! ( "Using pipe sink with format: {:?}" , format) ;
16-
17- let output: Box < dyn Write > = match path {
18- Some ( path) => Box :: new ( OpenOptions :: new ( ) . write ( true ) . open ( path) . unwrap ( ) ) ,
19- _ => Box :: new ( io:: stdout ( ) ) ,
20- } ;
21-
22- Self { output, format }
17+ Self {
18+ output : None ,
19+ path,
20+ format,
21+ }
2322 }
2423}
2524
2625impl Sink for StdoutSink {
26+ fn start ( & mut self ) -> io:: Result < ( ) > {
27+ if self . output . is_none ( ) {
28+ let output: Box < dyn Write > = match self . path . as_deref ( ) {
29+ Some ( path) => {
30+ let open_op = OpenOptions :: new ( )
31+ . write ( true )
32+ . open ( path)
33+ . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: Other , e) ) ?;
34+ Box :: new ( open_op)
35+ }
36+ None => Box :: new ( io:: stdout ( ) ) ,
37+ } ;
38+
39+ self . output = Some ( output) ;
40+ }
41+
42+ Ok ( ( ) )
43+ }
44+
2745 sink_as_bytes ! ( ) ;
2846}
2947
3048impl SinkAsBytes for StdoutSink {
3149 fn write_bytes ( & mut self , data : & [ u8 ] ) -> io:: Result < ( ) > {
32- self . output . write_all ( data) ?;
33- self . output . flush ( ) ?;
50+ match self . output . as_deref_mut ( ) {
51+ Some ( output) => {
52+ output. write_all ( data) ?;
53+ output. flush ( ) ?;
54+ }
55+ None => {
56+ return Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Output is None" ) ) ;
57+ }
58+ }
59+
3460 Ok ( ( ) )
3561 }
3662}
0 commit comments