mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
Error handling methods improved
This commit is contained in:
parent
55c6d574f6
commit
4d461e0381
@ -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.");
|
||||
fs::remove_file(file_path).inspect(|_| {
|
||||
println!("Test file {} deleted.", TEST_FILE_NAME);
|
||||
})?;
|
||||
} else {
|
||||
panic!("Error deleting file.");
|
||||
}
|
||||
} else {
|
||||
println!("No cleanup necassary since file not exist.");
|
||||
println!(
|
||||
"No cleanup necessary since {} not exist.",
|
||||
file_path.display()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
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;
|
||||
} else {
|
||||
panic!("Line read error");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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];
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
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();
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
@ -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.");
|
||||
fs::remove_file(file_path).inspect(|_| {
|
||||
println!("Test file {} deleted.", TEST_FILE_NAME);
|
||||
})?;
|
||||
} else {
|
||||
panic!("Error deleting file.");
|
||||
}
|
||||
} else {
|
||||
println!("No cleanup necassary since file not exist.");
|
||||
println!(
|
||||
"No cleanup necessary since {} not exist.",
|
||||
file_path.display()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
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;
|
||||
} else {
|
||||
panic!("Line read error");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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];
|
||||
|
||||
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);
|
||||
fs::remove_file(file_path).inspect(|_| {
|
||||
println!("Test file {} removed", file_name);
|
||||
})?;
|
||||
} else {
|
||||
eprintln!(
|
||||
"Error deleting file {}, err : {:?}",
|
||||
file_name,
|
||||
remove_status.err()
|
||||
);
|
||||
}
|
||||
} else {
|
||||
println!("No cleanup necassary since {} not exist.", file_name);
|
||||
println!("No cleanup necessary since {} not exist.", file_name);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -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);
|
||||
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();
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user