Skip to content
This repository was archived by the owner on Apr 16, 2023. It is now read-only.

Commit cb67e16

Browse files
committed
* Recover from IO errors
* Replace and operator to or operator
1 parent b27b5e5 commit cb67e16

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/filereader.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ impl FileReader {
2323
}
2424

2525
/// Read the lines of a file
26-
///
26+
///
2727
/// # Arguments
28-
///
28+
///
2929
/// * `path` - The path of the file that should be read
3030
///
3131
/// # Example
@@ -36,17 +36,25 @@ impl FileReader {
3636
///
3737
/// # Returns
3838
///
39-
/// A `Vec` that contains all the lines in the specified file
40-
pub fn read_lines(&self, path: &str) -> Vec<String> {
41-
let file = File::open(path).unwrap();
39+
/// The `Vec` that contains all the lines in the specified file or an `Error`
40+
pub fn read_lines(&self, path: &str) -> Result<Vec<String>, std::io::Error> {
41+
let file = File::open(path);
42+
let file = match file {
43+
Ok(d) => d,
44+
Err(e) => return Err(e),
45+
};
46+
4247
let reader = BufReader::new(file);
4348

4449
let mut lines = vec![];
4550
for (_index, line) in reader.lines().enumerate() {
46-
let line = line.unwrap();
51+
let line = match line {
52+
Ok(d) => d,
53+
Err(e) => return Err(e),
54+
};
4755
lines.push(line);
4856
}
4957

50-
lines
58+
Ok(lines)
5159
}
5260
}

src/view.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Sandbox for ApplicationContext {
7272
self.second_file = path.into_os_string().into_string().unwrap();
7373
}
7474
Message::ComparePressed => {
75-
if self.first_file.is_empty() && self.second_file.is_empty() {
75+
if self.first_file.is_empty() || self.second_file.is_empty() {
7676
MessageDialog::new()
7777
.set_type(MessageType::Warning)
7878
.set_title("text-diff")
@@ -87,6 +87,32 @@ impl Sandbox for ApplicationContext {
8787
let lines_first_file = file_reader.read_lines(&self.first_file);
8888
let lines_second_file = file_reader.read_lines(&self.second_file);
8989

90+
let lines_first_file = match lines_first_file {
91+
Ok(d) => d,
92+
Err(e) => {
93+
MessageDialog::new()
94+
.set_type(MessageType::Error)
95+
.set_title("text-diff")
96+
.set_text(&format!("Error while reading file!\n{}", e))
97+
.show_alert()
98+
.unwrap();
99+
return;
100+
}
101+
};
102+
103+
let lines_second_file = match lines_second_file {
104+
Ok(d) => d,
105+
Err(e) => {
106+
MessageDialog::new()
107+
.set_type(MessageType::Error)
108+
.set_title("text-diff")
109+
.set_text(&format!("Error while reading file!\n{}", e))
110+
.show_alert()
111+
.unwrap();
112+
return;
113+
}
114+
};
115+
90116
let mut diff = vec![];
91117
for f in &lines_first_file {
92118
let mut included = false;

0 commit comments

Comments
 (0)