@@ -47,6 +47,14 @@ pub fn panic_action(err: &impl Display, action: ErrAction, path: &Path) -> ! {
4747 panic ! ( "error {} `{}`: {}" , action. as_str( ) , path. display( ) , * err)
4848}
4949
50+ #[ track_caller]
51+ pub fn expect_action < T > ( res : Result < T , impl Display > , action : ErrAction , path : impl AsRef < Path > ) -> T {
52+ match res {
53+ Ok ( x) => x,
54+ Err ( ref e) => panic_action ( e, action, path. as_ref ( ) ) ,
55+ }
56+ }
57+
5058/// Wrapper around `std::fs::File` which panics with a path on failure.
5159pub struct File < ' a > {
5260 pub inner : fs:: File ,
@@ -57,9 +65,9 @@ impl<'a> File<'a> {
5765 #[ track_caller]
5866 pub fn open ( path : & ' a ( impl AsRef < Path > + ?Sized ) , options : & mut OpenOptions ) -> Self {
5967 let path = path. as_ref ( ) ;
60- match options . open ( path ) {
61- Ok ( inner) => Self { inner , path } ,
62- Err ( e ) => panic_action ( & e , ErrAction :: Open , path) ,
68+ Self {
69+ inner : expect_action ( options . open ( path ) , ErrAction :: Open , path) ,
70+ path,
6371 }
6472 }
6573
@@ -86,10 +94,7 @@ impl<'a> File<'a> {
8694 /// Read the entire contents of a file to the given buffer.
8795 #[ track_caller]
8896 pub fn read_append_to_string < ' dst > ( & mut self , dst : & ' dst mut String ) -> & ' dst mut String {
89- match self . inner . read_to_string ( dst) {
90- Ok ( _) => { } ,
91- Err ( e) => panic_action ( & e, ErrAction :: Read , self . path ) ,
92- }
97+ expect_action ( self . inner . read_to_string ( dst) , ErrAction :: Read , self . path ) ;
9398 dst
9499 }
95100
@@ -109,9 +114,7 @@ impl<'a> File<'a> {
109114 } ,
110115 Err ( e) => Err ( e) ,
111116 } ;
112- if let Err ( e) = res {
113- panic_action ( & e, ErrAction :: Write , self . path ) ;
114- }
117+ expect_action ( res, ErrAction :: Write , self . path ) ;
115118 }
116119}
117120
@@ -662,24 +665,22 @@ pub fn try_rename_dir(old_name: &Path, new_name: &Path) -> bool {
662665}
663666
664667pub fn write_file ( path : & Path , contents : & str ) {
665- fs:: write ( path, contents) . unwrap_or_else ( |e| panic_action ( & e , ErrAction :: Write , path) ) ;
668+ expect_action ( fs:: write ( path, contents) , ErrAction :: Write , path) ;
666669}
667670
668671#[ must_use]
669672pub fn run_with_output ( path : & ( impl AsRef < Path > + ?Sized ) , cmd : & mut Command ) -> Vec < u8 > {
670673 fn f ( path : & Path , cmd : & mut Command ) -> Vec < u8 > {
671- match cmd
672- . stdin ( Stdio :: null ( ) )
673- . stdout ( Stdio :: piped ( ) )
674- . stderr ( Stdio :: inherit ( ) )
675- . output ( )
676- {
677- Ok ( x) => match x. status . exit_ok ( ) {
678- Ok ( ( ) ) => x. stdout ,
679- Err ( ref e) => panic_action ( e, ErrAction :: Run , path) ,
680- } ,
681- Err ( ref e) => panic_action ( e, ErrAction :: Run , path) ,
682- }
674+ let output = expect_action (
675+ cmd. stdin ( Stdio :: null ( ) )
676+ . stdout ( Stdio :: piped ( ) )
677+ . stderr ( Stdio :: inherit ( ) )
678+ . output ( ) ,
679+ ErrAction :: Run ,
680+ path,
681+ ) ;
682+ expect_action ( output. status . exit_ok ( ) , ErrAction :: Run , path) ;
683+ output. stdout
683684 }
684685 f ( path. as_ref ( ) , cmd)
685686}
0 commit comments