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};
|
use std::io::{self, Read};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// read file or create it if it doesnt exist
|
|
||||||
match read_or_create_usernames_file() {
|
match read_or_create_usernames_file() {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -11,74 +10,39 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// print users
|
let usernames = get_or_set_usernames().unwrap();
|
||||||
let usernames = get_or_set_usernames();
|
|
||||||
|
|
||||||
println!("printing usernames:\n{usernames:?}");
|
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 usernames_file = File::open("usernames.txt");
|
||||||
let mut usernames = String::new();
|
|
||||||
|
|
||||||
match usernames_file.unwrap().read_to_string(&mut usernames) {
|
let mut usernames_file_copy = File::open("usernames.txt")?;
|
||||||
Ok(_) => (),
|
let mut usernames = String::new();
|
||||||
Err(e) => return format!("could not read from file!\n{e:?}").to_string(),
|
usernames_file_copy.read_to_string(&mut usernames)?;
|
||||||
}
|
|
||||||
|
|
||||||
if !usernames.is_empty() {
|
if !usernames.is_empty() {
|
||||||
return usernames;
|
return Ok(usernames);
|
||||||
} else {
|
} else {
|
||||||
println!("file is empty! adding the admin user");
|
println!("file is empty! adding the admin user");
|
||||||
let add_first_user = fs::write("usernames.txt", "admin");
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let usernames_file = File::open("usernames.txt");
|
// here lies the clunky part
|
||||||
|
File::open("usernames.txt")?;
|
||||||
let mut usernames = String::new();
|
let mut usernames = String::new();
|
||||||
|
usernames_file.unwrap().read_to_string(&mut usernames)?;
|
||||||
match usernames_file.unwrap().read_to_string(&mut usernames) {
|
Ok(usernames)
|
||||||
Ok(_) => (),
|
|
||||||
Err(e) => return format!("could not read from file after adding admin\n{e:?}").to_string(),
|
|
||||||
}
|
|
||||||
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");
|
let file_exists = File::open("usernames.txt");
|
||||||
|
|
||||||
match file_exists {
|
match file_exists {
|
||||||
Ok(filename) => return Ok(filename),
|
Ok(_) => (),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("file does not exist!\n{e:?}\ncreating usernames.txt");
|
println!("file does not exist!\n{e:?}\ncreating usernames.txt");
|
||||||
|
File::create("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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Ok(0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user