You've already forked book-exercises
update error handling example
- replaced `match`s with `?`s where possible - it should not be considered an improvement tho - turns out `?` is best used when you are returning `Result<T,E>`s - we were returning `String`s - so we had to wrap all successful string returns in `Ok()`s - which is just clunky - good learning but its time to move on!
This commit is contained in:
@@ -2,7 +2,6 @@ use std::fs::{self, File};
|
||||
use std::io::{self, Read};
|
||||
|
||||
fn main() {
|
||||
// read file or create it if it doesnt exist
|
||||
match read_or_create_usernames_file() {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
@@ -11,74 +10,39 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
// print users
|
||||
let usernames = get_or_set_usernames();
|
||||
|
||||
let usernames = get_or_set_usernames().unwrap();
|
||||
println!("printing usernames:\n{usernames:?}");
|
||||
}
|
||||
|
||||
fn get_or_set_usernames() -> String {
|
||||
fn get_or_set_usernames() -> Result<String, io::Error> {
|
||||
let usernames_file = File::open("usernames.txt");
|
||||
let mut usernames = String::new();
|
||||
|
||||
match usernames_file.unwrap().read_to_string(&mut usernames) {
|
||||
Ok(_) => (),
|
||||
Err(e) => return format!("could not read from file!\n{e:?}").to_string(),
|
||||
}
|
||||
let mut usernames_file_copy = File::open("usernames.txt")?;
|
||||
let mut usernames = String::new();
|
||||
usernames_file_copy.read_to_string(&mut usernames)?;
|
||||
|
||||
if !usernames.is_empty() {
|
||||
return usernames;
|
||||
return Ok(usernames);
|
||||
} else {
|
||||
println!("file is empty! adding the admin user");
|
||||
let add_first_user = fs::write("usernames.txt", "admin");
|
||||
match add_first_user {
|
||||
Ok(_) => {
|
||||
println!("added admin");
|
||||
}
|
||||
|
||||
Err(e) => {
|
||||
println!(
|
||||
"could not add the admin user
|
||||
error: {e:?}"
|
||||
);
|
||||
return format!(
|
||||
"file is empty and we could not add the admin user
|
||||
{e:?}"
|
||||
)
|
||||
.to_string();
|
||||
}
|
||||
}
|
||||
fs::write("usernames.txt", "admin")?;
|
||||
}
|
||||
|
||||
let usernames_file = File::open("usernames.txt");
|
||||
// here lies the clunky part
|
||||
File::open("usernames.txt")?;
|
||||
let mut usernames = String::new();
|
||||
|
||||
match usernames_file.unwrap().read_to_string(&mut usernames) {
|
||||
Ok(_) => (),
|
||||
Err(e) => return format!("could not read from file after adding admin\n{e:?}").to_string(),
|
||||
}
|
||||
usernames
|
||||
usernames_file.unwrap().read_to_string(&mut usernames)?;
|
||||
Ok(usernames)
|
||||
}
|
||||
|
||||
fn read_or_create_usernames_file() -> Result<File, io::Error> {
|
||||
fn read_or_create_usernames_file() -> Result<u8, io::Error> {
|
||||
let file_exists = File::open("usernames.txt");
|
||||
|
||||
match file_exists {
|
||||
Ok(filename) => return Ok(filename),
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
println!("file does not exist!\n{e:?}\ncreating usernames.txt");
|
||||
|
||||
let create_file = File::create("usernames.txt");
|
||||
|
||||
match create_file {
|
||||
Ok(filename) => {
|
||||
println!("created file: {filename:?}");
|
||||
return Ok(filename);
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
File::create("usernames.txt")?;
|
||||
}
|
||||
}
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user