Skip to content

Commit 4d461e0

Browse files
committed
Error handling methods improved
1 parent 55c6d57 commit 4d461e0

File tree

6 files changed

+160
-191
lines changed

6 files changed

+160
-191
lines changed

exercises/24_file_io/file_io1.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::fs;
22
use std::path::Path;
33

44
const TEST_FILE_NAME: &str = "SampleTextFile.txt";
5-
fn main() {
6-
create_required_files();
5+
fn main() -> Result<(), std::io::Error> {
6+
create_required_files()?;
77

88
let read_str_result = fs::read_to_string(TEST_FILE_NAME);
99

@@ -17,35 +17,35 @@ fn main() {
1717
}
1818
}
1919

20-
file_cleanup();
20+
file_cleanup()?;
21+
Ok(())
2122
}
2223

23-
fn create_required_files() {
24+
fn create_required_files() -> Result<(), std::io::Error> {
2425
let file_path = Path::new(TEST_FILE_NAME);
2526

2627
if !file_path.exists() {
27-
let file_write_result = fs::write(file_path, "This is the file content.");
28-
if file_write_result.is_ok() {
29-
println!("Successfully wrote to file.");
30-
} else {
31-
panic!("Error writing to file.");
32-
}
28+
fs::write(file_path, "This is the file content.")?;
3329
} else {
3430
println!("File already exist.");
3531
}
32+
33+
Ok(())
3634
}
3735

38-
fn file_cleanup() {
36+
fn file_cleanup() -> Result<(), std::io::Error> {
3937
let file_path = Path::new(TEST_FILE_NAME);
4038

4139
if file_path.exists() {
42-
let remove_status = fs::remove_file(file_path);
43-
if remove_status.is_ok() {
44-
println!("Successfully removed file.");
45-
} else {
46-
panic!("Error deleting file.");
47-
}
40+
fs::remove_file(file_path).inspect(|_| {
41+
println!("Test file {} deleted.", TEST_FILE_NAME);
42+
})?;
4843
} else {
49-
println!("No cleanup necassary since file not exist.");
44+
println!(
45+
"No cleanup necessary since {} not exist.",
46+
file_path.display()
47+
);
5048
}
49+
50+
Ok(())
5151
}

exercises/24_file_io/file_io2.rs

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,71 @@ use std::path::Path;
55
const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt";
66
const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt";
77

8-
fn main() {
9-
create_required_files();
8+
fn main() -> Result<(), std::io::Error> {
9+
create_required_files()?;
1010

11-
let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) {
12-
Ok(f) => f,
13-
Err(e) => panic!("Input file open error : {}", e),
14-
};
15-
16-
let buffered_input_file = BufReader::new(input_file);
17-
18-
let output_file = match fs::File::create(TEST_OUTPUT_FILE_NAME){
19-
Ok(f) => f,
20-
Err(e) => panic!("Output file open error : {}", e),
21-
};
11+
let input_file = fs::File::open(TEST_INPUT_FILE_NAME).inspect_err(|err| {
12+
eprintln!("{} file open error {:?}", TEST_INPUT_FILE_NAME, err);
13+
})?;
2214

2315
// TODO : How to create a new BufReader using input file
2416
let buffered_input_file =;
2517

18+
let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME).inspect_err(|err| {
19+
eprintln!("{} file open error {:?}", TEST_OUTPUT_FILE_NAME, err);
20+
})?;
21+
22+
let mut buffered_file_writer = BufWriter::new(output_file);
23+
2624
let mut line_number = 1;
2725

2826
for line in buffered_input_file.lines() {
29-
if let Ok(line) = line {
30-
let write_result =
31-
buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
32-
if write_result.is_err() {
33-
eprintln!("Line write error: {}", write_result.unwrap_err());
34-
break;
35-
}
36-
line_number += 1;
37-
} else {
38-
panic!("Line read error");
39-
}
27+
let line = line.inspect_err(|err| {
28+
eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err);
29+
})?;
30+
31+
buffered_file_writer
32+
.write(format!("Line {} : {}\n", line_number, line).as_bytes())
33+
.inspect_err(|err| {
34+
eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err);
35+
})?;
36+
37+
line_number += 1;
4038
}
4139

4240
println!("{} : lines processed", line_number - 1);
43-
file_cleanup();
41+
file_cleanup()
4442
}
4543

46-
fn create_required_files() {
44+
fn create_required_files() -> Result<(), std::io::Error> {
4745
let file_path = Path::new(TEST_INPUT_FILE_NAME);
4846

4947
if !file_path.exists() {
5048
let text = "This is the first line of the text.
5149
This is the second line.
5250
And this is the third and the last line.";
53-
let file_write_result = fs::write(file_path, text);
54-
55-
if file_write_result.is_ok() {
56-
println!("Multi line file created successfully!");
57-
}else {
58-
eprintln!("Error creating file : {} , error : {:?}", file_path.display(), file_write_result.err());
59-
}
51+
fs::write(file_path, text).inspect_err(|err| {
52+
eprintln!("Couldn't create the test file : {}", err);
53+
})?;
6054
}
61-
}
6255

63-
fn file_cleanup() {
56+
Ok(())
57+
}
6458

59+
fn file_cleanup() -> Result<(), std::io::Error> {
6560
let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME];
6661

6762
for file_name in file_names {
6863
let file_path = Path::new(file_name);
6964

7065
if file_path.exists() {
71-
let remove_status = fs::remove_file(file_path);
72-
if remove_status.is_ok() {
73-
println!("Successfully deleted file {}", file_name);
74-
}else {
75-
eprintln!("Error deleting file {}, err : {:?}", file_name, remove_status.err());
76-
}
77-
}else {
78-
println!("No cleanup necassary since {} not exist.", file_name);
66+
fs::remove_file(file_path).inspect(|_| {
67+
println!("Test file {} removed", file_name);
68+
})?;
69+
} else {
70+
println!("No cleanup necessary since {} not exist.", file_name);
7971
}
8072
}
8173

74+
Ok(())
8275
}
83-

exercises/24_file_io/file_io3.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::fs;
22
use std::path::PathBuf;
33

4-
fn main() {
5-
create_required_files();
4+
fn main() -> Result<(), std::io::Error> {
5+
create_required_files()?;
66
let mut path_buffer = PathBuf::new();
77

88
path_buffer.push("SampleFilesFolder");
@@ -25,59 +25,53 @@ fn main() {
2525
}
2626
}
2727

28-
29-
file_cleanup();
28+
file_cleanup()
3029
}
3130

32-
fn create_required_files() {
31+
fn create_required_files() -> Result<(), std::io::Error> {
3332
let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt");
3433

35-
let dir_path = file_path.parent().unwrap();
34+
let dir_path = match file_path.parent(){
35+
Some(parent) => parent,
36+
None => return Err(std::io::Error::new(std::io::ErrorKind::Other, "Could not get parent path")),
37+
};
3638

3739
if !dir_path.exists() {
38-
let dir_create_result = fs::create_dir(dir_path);
39-
if dir_create_result.is_ok() {
40-
println!("{:?} created", dir_path);
41-
}
40+
fs::create_dir(dir_path).inspect_err(|x| {
41+
eprintln!("Could not create directory: {:?}", x);
42+
})?;
4243
}
4344

4445
if !file_path.exists() {
4546
let text = "This is the first line of the text.
4647
This is the second line.
4748
And this is the third and the last line.";
48-
let file_write_result = fs::write(&file_path, text);
49-
50-
if file_write_result.is_ok() {
51-
println!("Multi line file created successfully!");
52-
}else {
53-
eprintln!("Error creating file : {} , error : {:?}", file_path.display(), file_write_result.err());
54-
}
49+
fs::write(&file_path, text).inspect_err(|err| {
50+
eprintln!("Couldn't create test file: {:?}", err);
51+
})?;
5552
}
53+
54+
Ok(())
5655
}
5756

58-
fn file_cleanup() {
57+
fn file_cleanup() -> Result<(), std::io::Error> {
5958
let mut path_buffer = PathBuf::new();
6059

6160
path_buffer.push("SampleFilesFolder");
6261
path_buffer.push("MultiLineTextFile.txt");
6362

6463
if path_buffer.exists() {
65-
let remove_status = fs::remove_file(&path_buffer);
66-
if remove_status.is_ok() {
67-
println!("Test file deleted.");
68-
}else {
69-
panic!("Error deleting file.");
70-
}
64+
fs::remove_file(&path_buffer).inspect(|_| {
65+
println!("Test file removed");
66+
})?;
7167
}
72-
7368
path_buffer.pop();
7469

7570
if path_buffer.exists() {
76-
let remove_status = fs::remove_dir(&path_buffer);
77-
if remove_status.is_ok() {
78-
println!("Test directory deleted.");
79-
}else {
80-
panic!("Error deleting directory.");
81-
}
71+
fs::remove_dir(&path_buffer).inspect(|_| {
72+
println!("Test dir removed");
73+
})?;
8274
}
75+
76+
Ok(())
8377
}

solutions/24_file_io/file_io1.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::fs;
22
use std::path::Path;
33

44
const TEST_FILE_NAME: &str = "SampleTextFile.txt";
5-
fn main() {
6-
create_required_files();
5+
fn main() -> Result<(), std::io::Error> {
6+
create_required_files()?;
77

88
let read_str_result = fs::read_to_string(TEST_FILE_NAME);
99

@@ -16,35 +16,35 @@ fn main() {
1616
}
1717
}
1818

19-
file_cleanup();
19+
file_cleanup()?;
20+
Ok(())
2021
}
2122

22-
fn create_required_files() {
23+
fn create_required_files() -> Result<(), std::io::Error> {
2324
let file_path = Path::new(TEST_FILE_NAME);
2425

2526
if !file_path.exists() {
26-
let file_write_result = fs::write(file_path, "This is the file content.");
27-
if file_write_result.is_ok() {
28-
println!("Successfully wrote to file.");
29-
} else {
30-
panic!("Error writing to file.");
31-
}
27+
fs::write(file_path, "This is the file content.")?;
3228
} else {
3329
println!("File already exist.");
3430
}
31+
32+
Ok(())
3533
}
3634

37-
fn file_cleanup() {
35+
fn file_cleanup() -> Result<(), std::io::Error> {
3836
let file_path = Path::new(TEST_FILE_NAME);
3937

4038
if file_path.exists() {
41-
let remove_status = fs::remove_file(file_path);
42-
if remove_status.is_ok() {
43-
println!("Successfully removed file.");
44-
} else {
45-
panic!("Error deleting file.");
46-
}
39+
fs::remove_file(file_path).inspect(|_| {
40+
println!("Test file {} deleted.", TEST_FILE_NAME);
41+
})?;
4742
} else {
48-
println!("No cleanup necassary since file not exist.");
43+
println!(
44+
"No cleanup necessary since {} not exist.",
45+
file_path.display()
46+
);
4947
}
48+
49+
Ok(())
5050
}

0 commit comments

Comments
 (0)