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() {
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")),
}
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::create_dir(STORAGE_FOLDER)?;
println!("Storage folder created");
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(())

Loading…
Cancel
Save