error handling example

- finally get an error handling example running the way it should
- we've used the ugly method (a lot of matches and so on)
- and our code is not beautiful per se
- but it works
- next we improve this with `?`s
This commit is contained in:
2026-01-17 14:59:19 +00:00
parent 6b0ff37562
commit b48bb409d5
5 changed files with 72 additions and 27 deletions

View File

@@ -2,38 +2,83 @@ use std::fs::{self, File};
use std::io::{self, Read}; use std::io::{self, Read};
fn main() { fn main() {
let username = get_username_or_create_file().unwrap(); // read file or create it if it doesnt exist
if username == "" { match read_or_create_usernames_file() {
println!("empty!"); Ok(_) => (),
Err(e) => {
println!("could not read or create file! {e:?}");
return;
}
}
// print users
let usernames = get_or_set_usernames();
println!("printing usernames:\n{usernames:?}");
}
fn get_or_set_usernames() -> String {
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(),
}
if !usernames.is_empty() {
return usernames;
} else { } else {
println!("{:?}", username); 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();
}
}
} }
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 after adding admin\n{e:?}").to_string(),
}
usernames
} }
fn get_username_or_create_file() -> Result<String, io::Error> { fn read_or_create_usernames_file() -> Result<File, io::Error> {
let check_username_file = File::open("username.txt"); let file_exists = File::open("usernames.txt");
let mut username_file = match check_username_file { match file_exists {
Ok(filename) => filename, Ok(filename) => return Ok(filename),
Err(e) => match e.kind() { Err(e) => {
std::io::ErrorKind::NotFound => match File::create("username.txt") { println!("file does not exist!\n{e:?}\ncreating usernames.txt");
Ok(file_created) => {
println!("created file, now writing..."); let create_file = File::create("usernames.txt");
fs::write("{file_created}", "admin");
file_created match create_file {
Ok(filename) => {
println!("created file: {filename:?}");
return Ok(filename);
} }
Err(error_creating_file) => { Err(e) => {
panic!("error creating file! {error_creating_file:?}") return Err(e);
} }
}, }
_ => panic!("cant open file!"), }
},
};
let mut username = String::new();
match username_file.read_to_string(&mut username) {
Ok(_) => Ok(username),
Err(e) => Err(e),
} }
} }

View File

@@ -0,0 +1 @@
john pork

View File

@@ -1 +0,0 @@
admin