diff --git a/error-handling/src/main.rs b/error-handling/src/main.rs index db0b71b..c2e1f31 100644 --- a/error-handling/src/main.rs +++ b/error-handling/src/main.rs @@ -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 { 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 { +fn read_or_create_usernames_file() -> Result { 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); }