Skip to content

Commit 382b016

Browse files
zxvfcsylvestre
authored andcommitted
Extract shared code to utils
1 parent 59e130a commit 382b016

2 files changed

Lines changed: 29 additions & 41 deletions

File tree

src/diff.rs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
// files that was distributed with this source code.
55

66
use crate::params::{parse_params, Format};
7-
use crate::utils::report_failure_to_read_input_file;
7+
use crate::utils;
88
use crate::{context_diff, ed_diff, normal_diff, side_diff, unified_diff};
99
use 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};
1311
use std::iter::Peekable;
1412
use 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 {

src/utils.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
// files that was distributed with this source code.
55

66
use regex::Regex;
7-
use std::{ffi::OsString, io::Write};
7+
use std::io::{self, Error, Read, Write};
8+
use std::{ffi::OsString, fs};
89
use unicode_width::UnicodeWidthStr;
910

1011
/// Replace tabs by spaces in the input line.
@@ -87,15 +88,22 @@ pub fn format_failure_to_read_input_file(
8788
)
8889
}
8990

90-
pub fn report_failure_to_read_input_file(
91-
executable: &OsString,
92-
filepath: &OsString,
93-
error: &std::io::Error,
94-
) {
95-
eprintln!(
96-
"{}",
97-
format_failure_to_read_input_file(executable, filepath, error)
98-
);
91+
pub fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
92+
if filepath == "-" {
93+
let mut content = Vec::new();
94+
io::stdin().read_to_end(&mut content).and(Ok(content))
95+
} else {
96+
fs::read(filepath)
97+
}
98+
}
99+
100+
pub fn read_both_files(
101+
from: &OsString,
102+
to: &OsString,
103+
) -> Result<(Vec<u8>, Vec<u8>), (OsString, Error)> {
104+
let from_content = read_file_contents(from).map_err(|e| (from.clone(), e))?;
105+
let to_content = read_file_contents(to).map_err(|e| (to.clone(), e))?;
106+
Ok((from_content, to_content))
99107
}
100108

101109
#[cfg(test)]

0 commit comments

Comments
 (0)