44// files that was distributed with this source code.
55
66use crate :: params:: { parse_params, Format } ;
7- use crate :: utils:: report_failure_to_read_input_file ;
7+ use crate :: utils;
88use crate :: { context_diff, ed_diff, normal_diff, side_diff, unified_diff} ;
99use std:: env:: ArgsOs ;
10- use std:: ffi:: OsString ;
11- use std:: fs;
12- use std:: io:: { self , stdout, Read , Write } ;
10+ use std:: io:: { self , stdout, Write } ;
1311use std:: iter:: Peekable ;
1412use std:: process:: { exit, ExitCode } ;
1513
@@ -23,6 +21,7 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
2321 eprintln ! ( "{error}" ) ;
2422 exit ( 2 ) ;
2523 } ) ;
24+
2625 // if from and to are the same file, no need to perform any comparison
2726 let maybe_report_identical_files = || {
2827 if params. report_identical_files {
@@ -40,35 +39,16 @@ pub fn main(opts: Peekable<ArgsOs>) -> ExitCode {
4039 return ExitCode :: SUCCESS ;
4140 }
4241
43- // read files
44- fn read_file_contents ( filepath : & OsString ) -> io:: Result < Vec < u8 > > {
45- if filepath == "-" {
46- let mut content = Vec :: new ( ) ;
47- io:: stdin ( ) . read_to_end ( & mut content) . and ( Ok ( content) )
48- } else {
49- fs:: read ( filepath)
50- }
51- }
52- let mut io_error = false ;
53- let from_content = match read_file_contents ( & params. from ) {
54- Ok ( from_content) => from_content,
55- Err ( e) => {
56- report_failure_to_read_input_file ( & params. executable , & params. from , & e) ;
57- io_error = true ;
58- vec ! [ ]
59- }
60- } ;
61- let to_content = match read_file_contents ( & params. to ) {
62- Ok ( to_content) => to_content,
63- Err ( e) => {
64- report_failure_to_read_input_file ( & params. executable , & params. to , & e) ;
65- io_error = true ;
66- vec ! [ ]
42+ let ( from_content, to_content) = match utils:: read_both_files ( & params. from , & params. to ) {
43+ Ok ( contents) => contents,
44+ Err ( ( filepath, error) ) => {
45+ eprintln ! (
46+ "{}" ,
47+ utils:: format_failure_to_read_input_file( & params. executable, & filepath, & error)
48+ ) ;
49+ return ExitCode :: from ( 2 ) ;
6750 }
6851 } ;
69- if io_error {
70- return ExitCode :: from ( 2 ) ;
71- }
7252
7353 // run diff
7454 let result: Vec < u8 > = match params. format {
0 commit comments