From 4d461e03812e97cea0604eb4b750a958379d1acc Mon Sep 17 00:00:00 2001 From: Kivanc Date: Fri, 12 Dec 2025 14:38:51 +0300 Subject: [PATCH] Error handling methods improved --- exercises/24_file_io/file_io1.rs | 36 +++++++------- exercises/24_file_io/file_io2.rs | 80 ++++++++++++++------------------ exercises/24_file_io/file_io3.rs | 56 ++++++++++------------ solutions/24_file_io/file_io1.rs | 36 +++++++------- solutions/24_file_io/file_io2.rs | 79 +++++++++++++------------------ solutions/24_file_io/file_io3.rs | 64 ++++++++++++------------- 6 files changed, 160 insertions(+), 191 deletions(-) diff --git a/exercises/24_file_io/file_io1.rs b/exercises/24_file_io/file_io1.rs index bdd838e3..54647dc4 100644 --- a/exercises/24_file_io/file_io1.rs +++ b/exercises/24_file_io/file_io1.rs @@ -2,8 +2,8 @@ use std::fs; use std::path::Path; const TEST_FILE_NAME: &str = "SampleTextFile.txt"; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; let read_str_result = fs::read_to_string(TEST_FILE_NAME); @@ -17,35 +17,35 @@ fn main() { } } - file_cleanup(); + file_cleanup()?; + Ok(()) } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { - let file_write_result = fs::write(file_path, "This is the file content."); - if file_write_result.is_ok() { - println!("Successfully wrote to file."); - } else { - panic!("Error writing to file."); - } + fs::write(file_path, "This is the file content.")?; } else { println!("File already exist."); } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if file_path.exists() { - let remove_status = fs::remove_file(file_path); - if remove_status.is_ok() { - println!("Successfully removed file."); - } else { - panic!("Error deleting file."); - } + fs::remove_file(file_path).inspect(|_| { + println!("Test file {} deleted.", TEST_FILE_NAME); + })?; } else { - println!("No cleanup necassary since file not exist."); + println!( + "No cleanup necessary since {} not exist.", + file_path.display() + ); } + + Ok(()) } diff --git a/exercises/24_file_io/file_io2.rs b/exercises/24_file_io/file_io2.rs index 66579294..f19d2a47 100644 --- a/exercises/24_file_io/file_io2.rs +++ b/exercises/24_file_io/file_io2.rs @@ -5,79 +5,71 @@ use std::path::Path; const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; - let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) { - Ok(f) => f, - Err(e) => panic!("Input file open error : {}", e), - }; - - let buffered_input_file = BufReader::new(input_file); - - let output_file = match fs::File::create(TEST_OUTPUT_FILE_NAME){ - Ok(f) => f, - Err(e) => panic!("Output file open error : {}", e), - }; + let input_file = fs::File::open(TEST_INPUT_FILE_NAME).inspect_err(|err| { + eprintln!("{} file open error {:?}", TEST_INPUT_FILE_NAME, err); + })?; // TODO : How to create a new BufReader using input file let buffered_input_file =; + let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME).inspect_err(|err| { + eprintln!("{} file open error {:?}", TEST_OUTPUT_FILE_NAME, err); + })?; + + let mut buffered_file_writer = BufWriter::new(output_file); + let mut line_number = 1; for line in buffered_input_file.lines() { - if let Ok(line) = line { - let write_result = - buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); - if write_result.is_err() { - eprintln!("Line write error: {}", write_result.unwrap_err()); - break; - } - line_number += 1; - } else { - panic!("Line read error"); - } + let line = line.inspect_err(|err| { + eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err); + })?; + + buffered_file_writer + .write(format!("Line {} : {}\n", line_number, line).as_bytes()) + .inspect_err(|err| { + eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err); + })?; + + line_number += 1; } println!("{} : lines processed", line_number - 1); - file_cleanup(); + file_cleanup() } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_INPUT_FILE_NAME); if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - let file_write_result = fs::write(file_path, text); - - if file_write_result.is_ok() { - println!("Multi line file created successfully!"); - }else { - eprintln!("Error creating file : {} , error : {:?}", file_path.display(), file_write_result.err()); - } + fs::write(file_path, text).inspect_err(|err| { + eprintln!("Couldn't create the test file : {}", err); + })?; } + + Ok(()) } -fn file_cleanup() { - +fn file_cleanup() -> Result<(), std::io::Error> { let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; for file_name in file_names { let file_path = Path::new(file_name); if file_path.exists() { - let remove_status = fs::remove_file(file_path); - if remove_status.is_ok() { - println!("Successfully deleted file {}", file_name); - }else { - eprintln!("Error deleting file {}, err : {:?}", file_name, remove_status.err()); - } - }else { - println!("No cleanup necassary since {} not exist.", file_name); + fs::remove_file(file_path).inspect(|_| { + println!("Test file {} removed", file_name); + })?; + } else { + println!("No cleanup necessary since {} not exist.", file_name); } } + Ok(()) } - diff --git a/exercises/24_file_io/file_io3.rs b/exercises/24_file_io/file_io3.rs index 94e7e5d4..6ac3be9b 100644 --- a/exercises/24_file_io/file_io3.rs +++ b/exercises/24_file_io/file_io3.rs @@ -1,8 +1,8 @@ use std::fs; use std::path::PathBuf; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; let mut path_buffer = PathBuf::new(); path_buffer.push("SampleFilesFolder"); @@ -25,59 +25,53 @@ fn main() { } } - - file_cleanup(); + file_cleanup() } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); - let dir_path = file_path.parent().unwrap(); + let dir_path = match file_path.parent(){ + Some(parent) => parent, + None => return Err(std::io::Error::new(std::io::ErrorKind::Other, "Could not get parent path")), + }; if !dir_path.exists() { - let dir_create_result = fs::create_dir(dir_path); - if dir_create_result.is_ok() { - println!("{:?} created", dir_path); - } + fs::create_dir(dir_path).inspect_err(|x| { + eprintln!("Could not create directory: {:?}", x); + })?; } if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - let file_write_result = fs::write(&file_path, text); - - if file_write_result.is_ok() { - println!("Multi line file created successfully!"); - }else { - eprintln!("Error creating file : {} , error : {:?}", file_path.display(), file_write_result.err()); - } + fs::write(&file_path, text).inspect_err(|err| { + eprintln!("Couldn't create test file: {:?}", err); + })?; } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let mut path_buffer = PathBuf::new(); path_buffer.push("SampleFilesFolder"); path_buffer.push("MultiLineTextFile.txt"); if path_buffer.exists() { - let remove_status = fs::remove_file(&path_buffer); - if remove_status.is_ok() { - println!("Test file deleted."); - }else { - panic!("Error deleting file."); - } + fs::remove_file(&path_buffer).inspect(|_| { + println!("Test file removed"); + })?; } - path_buffer.pop(); if path_buffer.exists() { - let remove_status = fs::remove_dir(&path_buffer); - if remove_status.is_ok() { - println!("Test directory deleted."); - }else { - panic!("Error deleting directory."); - } + fs::remove_dir(&path_buffer).inspect(|_| { + println!("Test dir removed"); + })?; } + + Ok(()) } diff --git a/solutions/24_file_io/file_io1.rs b/solutions/24_file_io/file_io1.rs index b690b65c..9331eeea 100644 --- a/solutions/24_file_io/file_io1.rs +++ b/solutions/24_file_io/file_io1.rs @@ -2,8 +2,8 @@ use std::fs; use std::path::Path; const TEST_FILE_NAME: &str = "SampleTextFile.txt"; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; let read_str_result = fs::read_to_string(TEST_FILE_NAME); @@ -16,35 +16,35 @@ fn main() { } } - file_cleanup(); + file_cleanup()?; + Ok(()) } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if !file_path.exists() { - let file_write_result = fs::write(file_path, "This is the file content."); - if file_write_result.is_ok() { - println!("Successfully wrote to file."); - } else { - panic!("Error writing to file."); - } + fs::write(file_path, "This is the file content.")?; } else { println!("File already exist."); } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_FILE_NAME); if file_path.exists() { - let remove_status = fs::remove_file(file_path); - if remove_status.is_ok() { - println!("Successfully removed file."); - } else { - panic!("Error deleting file."); - } + fs::remove_file(file_path).inspect(|_| { + println!("Test file {} deleted.", TEST_FILE_NAME); + })?; } else { - println!("No cleanup necassary since file not exist."); + println!( + "No cleanup necessary since {} not exist.", + file_path.display() + ); } + + Ok(()) } diff --git a/solutions/24_file_io/file_io2.rs b/solutions/24_file_io/file_io2.rs index b8363874..bfcaff1e 100644 --- a/solutions/24_file_io/file_io2.rs +++ b/solutions/24_file_io/file_io2.rs @@ -5,83 +5,70 @@ use std::path::Path; const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; - let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) { - Ok(f) => f, - Err(e) => panic!("Input file open error : {}", e), - }; + let input_file = fs::File::open(TEST_INPUT_FILE_NAME).inspect_err(|err| { + eprintln!("{} file open error {:?}", TEST_INPUT_FILE_NAME, err); + })?; let buffered_input_file = BufReader::new(input_file); - let output_file = match fs::File::create(TEST_OUTPUT_FILE_NAME) { - Ok(f) => f, - Err(e) => panic!("Output file open error : {}", e), - }; + let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME).inspect_err(|err| { + eprintln!("{} file open error {:?}", TEST_OUTPUT_FILE_NAME, err); + })?; let mut buffered_file_writer = BufWriter::new(output_file); let mut line_number = 1; for line in buffered_input_file.lines() { - if let Ok(line) = line { - let write_result = - buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes()); - if write_result.is_err() { - eprintln!("Line write error: {}", write_result.unwrap_err()); - break; - } - line_number += 1; - } else { - panic!("Line read error"); - } + let line = line.inspect_err(|err| { + eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err); + })?; + + buffered_file_writer + .write(format!("Line {} : {}\n", line_number, line).as_bytes()) + .inspect_err(|err| { + eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err); + })?; + + line_number += 1; } println!("{} : lines processed", line_number - 1); - file_cleanup(); + file_cleanup() } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = Path::new(TEST_INPUT_FILE_NAME); if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - let file_write_result = fs::write(file_path, text); - - if file_write_result.is_ok() { - println!("Multi line file created successfully!"); - } else { - eprintln!( - "Error creating file : {} , error : {:?}", - file_path.display(), - file_write_result.err() - ); - } + fs::write(file_path, text).inspect_err(|err| { + eprintln!("Couldn't create the test file : {}", err); + })?; } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; for file_name in file_names { let file_path = Path::new(file_name); if file_path.exists() { - let remove_status = fs::remove_file(file_path); - if remove_status.is_ok() { - println!("Successfully deleted file {}", file_name); - } else { - eprintln!( - "Error deleting file {}, err : {:?}", - file_name, - remove_status.err() - ); - } + fs::remove_file(file_path).inspect(|_| { + println!("Test file {} removed", file_name); + })?; } else { - println!("No cleanup necassary since {} not exist.", file_name); + println!("No cleanup necessary since {} not exist.", file_name); } } + + Ok(()) } diff --git a/solutions/24_file_io/file_io3.rs b/solutions/24_file_io/file_io3.rs index dc1b4e9b..ac65ca97 100644 --- a/solutions/24_file_io/file_io3.rs +++ b/solutions/24_file_io/file_io3.rs @@ -1,8 +1,8 @@ use std::fs; use std::path::PathBuf; -fn main() { - create_required_files(); +fn main() -> Result<(), std::io::Error> { + create_required_files()?; let mut path_buffer = PathBuf::new(); path_buffer.push("SampleFilesFolder"); @@ -24,62 +24,58 @@ fn main() { } } - file_cleanup(); + file_cleanup() } -fn create_required_files() { +fn create_required_files() -> Result<(), std::io::Error> { let file_path = PathBuf::from("SampleFilesFolder/MultiLineTextFile.txt"); - let dir_path = file_path.parent().unwrap(); + let dir_path = match file_path.parent() { + Some(parent) => parent, + None => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Could not get parent path", + )); + } + }; if !dir_path.exists() { - let dir_create_result = fs::create_dir(dir_path); - if dir_create_result.is_ok() { - println!("{:?} created", dir_path); - } + fs::create_dir(dir_path).inspect_err(|x| { + eprintln!("Could not create directory: {:?}", x); + })?; } if !file_path.exists() { let text = "This is the first line of the text. This is the second line. And this is the third and the last line."; - let file_write_result = fs::write(&file_path, text); - - if file_write_result.is_ok() { - println!("Multi line file created successfully!"); - } else { - eprintln!( - "Error creating file : {} , error : {:?}", - file_path.display(), - file_write_result.err() - ); - } + fs::write(&file_path, text).inspect_err(|err| { + eprintln!("Couldn't create test file: {:?}", err); + })?; } + + Ok(()) } -fn file_cleanup() { +fn file_cleanup() -> Result<(), std::io::Error> { let mut path_buffer = PathBuf::new(); path_buffer.push("SampleFilesFolder"); path_buffer.push("MultiLineTextFile.txt"); if path_buffer.exists() { - let remove_status = fs::remove_file(&path_buffer); - if remove_status.is_ok() { - println!("Test file deleted."); - } else { - panic!("Error deleting file."); - } + fs::remove_file(&path_buffer).inspect(|_| { + println!("Test file removed"); + })?; } - path_buffer.pop(); if path_buffer.exists() { - let remove_status = fs::remove_dir(&path_buffer); - if remove_status.is_ok() { - println!("Test directory deleted."); - } else { - panic!("Error deleting directory."); - } + fs::remove_dir(&path_buffer).inspect(|_| { + println!("Test dir removed"); + })?; } + + Ok(()) }