From 8ce1380ea34e7a1a30e8c8f0024fb9917a968738 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Wed, 4 Sep 2024 10:06:28 +0000 Subject: [PATCH] better error handling --- src/main.rs | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1c0eba5..b0cbd9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,35 +39,18 @@ fn init() -> io::Result<()> { if is_inited() { return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); } - match fs::create_dir(STORAGE_FOLDER) { - Ok(_) => { - println!("Create folder for storage"); // TODO: make it only in debug mode - }, - Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Can`t create storage folder")), - } + fs::create_dir(STORAGE_FOLDER)?; + println!("Storage folder created"); - match fs::File::create(STORAGE_PATH) { - Ok(_) => { - println!("Storage db created"); - }, - Err(_) => { - return Err(io::Error::new(io::ErrorKind::Other, "Can`t create mps.db")); // TODO: better error, not just Other - } - } - + fs::File::create(STORAGE_PATH)?; + println!("Storage db created"); Ok(()) } fn add() -> io::Result<()> { - let mut file = match fs::OpenOptions::new() + let mut file = fs::OpenOptions::new() .write(true) - .open(STORAGE_PATH) - { - Ok(file) => file, - Err(_) => { - return Err(io::Error::new(io::ErrorKind::Other, "can`t open database")); // TODO: better error handling - } - }; + .open(STORAGE_PATH)?; writeln!(file, "new item")?; Ok(())