Error handling methods improved

This commit is contained in:
Kivanc 2025-12-12 14:38:51 +03:00
parent 55c6d574f6
commit 4d461e0381
6 changed files with 160 additions and 191 deletions

View File

@ -2,8 +2,8 @@ use std::fs;
use std::path::Path; use std::path::Path;
const TEST_FILE_NAME: &str = "SampleTextFile.txt"; const TEST_FILE_NAME: &str = "SampleTextFile.txt";
fn main() { fn main() -> Result<(), std::io::Error> {
create_required_files(); create_required_files()?;
let read_str_result = fs::read_to_string(TEST_FILE_NAME); 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); let file_path = Path::new(TEST_FILE_NAME);
if !file_path.exists() { if !file_path.exists() {
let file_write_result = fs::write(file_path, "This is the file content."); 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.");
}
} else { } else {
println!("File already exist."); println!("File already exist.");
} }
Ok(())
} }
fn file_cleanup() { fn file_cleanup() -> Result<(), std::io::Error> {
let file_path = Path::new(TEST_FILE_NAME); let file_path = Path::new(TEST_FILE_NAME);
if file_path.exists() { if file_path.exists() {
let remove_status = fs::remove_file(file_path); fs::remove_file(file_path).inspect(|_| {
if remove_status.is_ok() { println!("Test file {} deleted.", TEST_FILE_NAME);
println!("Successfully removed file."); })?;
} else {
panic!("Error deleting file.");
}
} else { } else {
println!("No cleanup necassary since file not exist."); println!(
"No cleanup necessary since {} not exist.",
file_path.display()
);
} }
Ok(())
} }

View File

@ -5,79 +5,71 @@ use std::path::Path;
const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt";
const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt";
fn main() { fn main() -> Result<(), std::io::Error> {
create_required_files(); create_required_files()?;
let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) { let input_file = fs::File::open(TEST_INPUT_FILE_NAME).inspect_err(|err| {
Ok(f) => f, eprintln!("{} file open error {:?}", TEST_INPUT_FILE_NAME, err);
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),
};
// TODO : How to create a new BufReader using input file // TODO : How to create a new BufReader using input file
let buffered_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; let mut line_number = 1;
for line in buffered_input_file.lines() { for line in buffered_input_file.lines() {
if let Ok(line) = line { let line = line.inspect_err(|err| {
let write_result = eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err);
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()); buffered_file_writer
break; .write(format!("Line {} : {}\n", line_number, line).as_bytes())
} .inspect_err(|err| {
line_number += 1; eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err);
} else { })?;
panic!("Line read error");
} line_number += 1;
} }
println!("{} : lines processed", 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); let file_path = Path::new(TEST_INPUT_FILE_NAME);
if !file_path.exists() { if !file_path.exists() {
let text = "This is the first line of the text. let text = "This is the first line of the text.
This is the second line. This is the second line.
And this is the third and the last line."; And this is the third and the last line.";
let file_write_result = fs::write(file_path, text); fs::write(file_path, text).inspect_err(|err| {
eprintln!("Couldn't create the test file : {}", err);
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());
}
} }
Ok(())
} }
fn file_cleanup() { fn file_cleanup() -> Result<(), std::io::Error> {
let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME];
for file_name in file_names { for file_name in file_names {
let file_path = Path::new(file_name); let file_path = Path::new(file_name);
if file_path.exists() { if file_path.exists() {
let remove_status = fs::remove_file(file_path); fs::remove_file(file_path).inspect(|_| {
if remove_status.is_ok() { println!("Test file {} removed", file_name);
println!("Successfully deleted file {}", file_name); })?;
}else { } else {
eprintln!("Error deleting file {}, err : {:?}", file_name, remove_status.err()); println!("No cleanup necessary since {} not exist.", file_name);
}
}else {
println!("No cleanup necassary since {} not exist.", file_name);
} }
} }
Ok(())
} }

View File

@ -1,8 +1,8 @@
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
fn main() { fn main() -> Result<(), std::io::Error> {
create_required_files(); create_required_files()?;
let mut path_buffer = PathBuf::new(); let mut path_buffer = PathBuf::new();
path_buffer.push("SampleFilesFolder"); 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 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() { if !dir_path.exists() {
let dir_create_result = fs::create_dir(dir_path); fs::create_dir(dir_path).inspect_err(|x| {
if dir_create_result.is_ok() { eprintln!("Could not create directory: {:?}", x);
println!("{:?} created", dir_path); })?;
}
} }
if !file_path.exists() { if !file_path.exists() {
let text = "This is the first line of the text. let text = "This is the first line of the text.
This is the second line. This is the second line.
And this is the third and the last line."; And this is the third and the last line.";
let file_write_result = fs::write(&file_path, text); fs::write(&file_path, text).inspect_err(|err| {
eprintln!("Couldn't create test file: {:?}", err);
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());
}
} }
Ok(())
} }
fn file_cleanup() { fn file_cleanup() -> Result<(), std::io::Error> {
let mut path_buffer = PathBuf::new(); let mut path_buffer = PathBuf::new();
path_buffer.push("SampleFilesFolder"); path_buffer.push("SampleFilesFolder");
path_buffer.push("MultiLineTextFile.txt"); path_buffer.push("MultiLineTextFile.txt");
if path_buffer.exists() { if path_buffer.exists() {
let remove_status = fs::remove_file(&path_buffer); fs::remove_file(&path_buffer).inspect(|_| {
if remove_status.is_ok() { println!("Test file removed");
println!("Test file deleted."); })?;
}else {
panic!("Error deleting file.");
}
} }
path_buffer.pop(); path_buffer.pop();
if path_buffer.exists() { if path_buffer.exists() {
let remove_status = fs::remove_dir(&path_buffer); fs::remove_dir(&path_buffer).inspect(|_| {
if remove_status.is_ok() { println!("Test dir removed");
println!("Test directory deleted."); })?;
}else {
panic!("Error deleting directory.");
}
} }
Ok(())
} }

View File

@ -2,8 +2,8 @@ use std::fs;
use std::path::Path; use std::path::Path;
const TEST_FILE_NAME: &str = "SampleTextFile.txt"; const TEST_FILE_NAME: &str = "SampleTextFile.txt";
fn main() { fn main() -> Result<(), std::io::Error> {
create_required_files(); create_required_files()?;
let read_str_result = fs::read_to_string(TEST_FILE_NAME); 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); let file_path = Path::new(TEST_FILE_NAME);
if !file_path.exists() { if !file_path.exists() {
let file_write_result = fs::write(file_path, "This is the file content."); 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.");
}
} else { } else {
println!("File already exist."); println!("File already exist.");
} }
Ok(())
} }
fn file_cleanup() { fn file_cleanup() -> Result<(), std::io::Error> {
let file_path = Path::new(TEST_FILE_NAME); let file_path = Path::new(TEST_FILE_NAME);
if file_path.exists() { if file_path.exists() {
let remove_status = fs::remove_file(file_path); fs::remove_file(file_path).inspect(|_| {
if remove_status.is_ok() { println!("Test file {} deleted.", TEST_FILE_NAME);
println!("Successfully removed file."); })?;
} else {
panic!("Error deleting file.");
}
} else { } else {
println!("No cleanup necassary since file not exist."); println!(
"No cleanup necessary since {} not exist.",
file_path.display()
);
} }
Ok(())
} }

View File

@ -5,83 +5,70 @@ use std::path::Path;
const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt"; const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt";
const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt"; const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt";
fn main() { fn main() -> Result<(), std::io::Error> {
create_required_files(); create_required_files()?;
let input_file = match fs::File::open(TEST_INPUT_FILE_NAME) { let input_file = fs::File::open(TEST_INPUT_FILE_NAME).inspect_err(|err| {
Ok(f) => f, eprintln!("{} file open error {:?}", TEST_INPUT_FILE_NAME, err);
Err(e) => panic!("Input file open error : {}", e), })?;
};
let buffered_input_file = BufReader::new(input_file); let buffered_input_file = BufReader::new(input_file);
let output_file = match fs::File::create(TEST_OUTPUT_FILE_NAME) { let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME).inspect_err(|err| {
Ok(f) => f, eprintln!("{} file open error {:?}", TEST_OUTPUT_FILE_NAME, err);
Err(e) => panic!("Output file open error : {}", e), })?;
};
let mut buffered_file_writer = BufWriter::new(output_file); let mut buffered_file_writer = BufWriter::new(output_file);
let mut line_number = 1; let mut line_number = 1;
for line in buffered_input_file.lines() { for line in buffered_input_file.lines() {
if let Ok(line) = line { let line = line.inspect_err(|err| {
let write_result = eprintln!("{} line parse error {:?}", TEST_INPUT_FILE_NAME, err);
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()); buffered_file_writer
break; .write(format!("Line {} : {}\n", line_number, line).as_bytes())
} .inspect_err(|err| {
line_number += 1; eprintln!("{} line write error {:?}", TEST_INPUT_FILE_NAME, err);
} else { })?;
panic!("Line read error");
} line_number += 1;
} }
println!("{} : lines processed", 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); let file_path = Path::new(TEST_INPUT_FILE_NAME);
if !file_path.exists() { if !file_path.exists() {
let text = "This is the first line of the text. let text = "This is the first line of the text.
This is the second line. This is the second line.
And this is the third and the last line."; And this is the third and the last line.";
let file_write_result = fs::write(file_path, text); fs::write(file_path, text).inspect_err(|err| {
eprintln!("Couldn't create the test file : {}", err);
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()
);
}
} }
Ok(())
} }
fn file_cleanup() { fn file_cleanup() -> Result<(), std::io::Error> {
let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME]; let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME];
for file_name in file_names { for file_name in file_names {
let file_path = Path::new(file_name); let file_path = Path::new(file_name);
if file_path.exists() { if file_path.exists() {
let remove_status = fs::remove_file(file_path); fs::remove_file(file_path).inspect(|_| {
if remove_status.is_ok() { println!("Test file {} removed", file_name);
println!("Successfully deleted file {}", file_name); })?;
} else {
eprintln!(
"Error deleting file {}, err : {:?}",
file_name,
remove_status.err()
);
}
} else { } else {
println!("No cleanup necassary since {} not exist.", file_name); println!("No cleanup necessary since {} not exist.", file_name);
} }
} }
Ok(())
} }

View File

@ -1,8 +1,8 @@
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
fn main() { fn main() -> Result<(), std::io::Error> {
create_required_files(); create_required_files()?;
let mut path_buffer = PathBuf::new(); let mut path_buffer = PathBuf::new();
path_buffer.push("SampleFilesFolder"); 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 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() { if !dir_path.exists() {
let dir_create_result = fs::create_dir(dir_path); fs::create_dir(dir_path).inspect_err(|x| {
if dir_create_result.is_ok() { eprintln!("Could not create directory: {:?}", x);
println!("{:?} created", dir_path); })?;
}
} }
if !file_path.exists() { if !file_path.exists() {
let text = "This is the first line of the text. let text = "This is the first line of the text.
This is the second line. This is the second line.
And this is the third and the last line."; And this is the third and the last line.";
let file_write_result = fs::write(&file_path, text); fs::write(&file_path, text).inspect_err(|err| {
eprintln!("Couldn't create test file: {:?}", err);
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()
);
}
} }
Ok(())
} }
fn file_cleanup() { fn file_cleanup() -> Result<(), std::io::Error> {
let mut path_buffer = PathBuf::new(); let mut path_buffer = PathBuf::new();
path_buffer.push("SampleFilesFolder"); path_buffer.push("SampleFilesFolder");
path_buffer.push("MultiLineTextFile.txt"); path_buffer.push("MultiLineTextFile.txt");
if path_buffer.exists() { if path_buffer.exists() {
let remove_status = fs::remove_file(&path_buffer); fs::remove_file(&path_buffer).inspect(|_| {
if remove_status.is_ok() { println!("Test file removed");
println!("Test file deleted."); })?;
} else {
panic!("Error deleting file.");
}
} }
path_buffer.pop(); path_buffer.pop();
if path_buffer.exists() { if path_buffer.exists() {
let remove_status = fs::remove_dir(&path_buffer); fs::remove_dir(&path_buffer).inspect(|_| {
if remove_status.is_ok() { println!("Test dir removed");
println!("Test directory deleted."); })?;
} else {
panic!("Error deleting directory.");
}
} }
Ok(())
} }