Browse Source

better error handling

db-refactoring
Coin de Gamma 4 months ago
parent
commit
8ce1380ea3
  1. 29
      src/main.rs

29
src/main.rs

@ -39,35 +39,18 @@ fn init() -> io::Result<()> {
if is_inited() { if is_inited() {
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted"));
} }
match fs::create_dir(STORAGE_FOLDER) { fs::create_dir(STORAGE_FOLDER)?;
Ok(_) => { println!("Storage folder created");
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")),
}
match fs::File::create(STORAGE_PATH) { fs::File::create(STORAGE_PATH)?;
Ok(_) => { println!("Storage db created");
println!("Storage db created");
},
Err(_) => {
return Err(io::Error::new(io::ErrorKind::Other, "Can`t create mps.db")); // TODO: better error, not just Other
}
}
Ok(()) Ok(())
} }
fn add() -> io::Result<()> { fn add() -> io::Result<()> {
let mut file = match fs::OpenOptions::new() let mut file = fs::OpenOptions::new()
.write(true) .write(true)
.open(STORAGE_PATH) .open(STORAGE_PATH)?;
{
Ok(file) => file,
Err(_) => {
return Err(io::Error::new(io::ErrorKind::Other, "can`t open database")); // TODO: better error handling
}
};
writeln!(file, "new item")?; writeln!(file, "new item")?;
Ok(()) Ok(())

Loading…
Cancel
Save