handle errors

This commit is contained in:
yggverse 2025-02-22 00:33:33 +02:00
parent 08c437920c
commit 04a276116d
2 changed files with 121 additions and 77 deletions

View file

@ -1,7 +1,8 @@
use anyhow::{bail, Result};
use anyhow::{anyhow, Error, Result};
use std::{
fs::{create_dir_all, remove_file, rename, File},
path::{PathBuf, MAIN_SEPARATOR},
str::FromStr,
thread,
time::{Duration, SystemTime, UNIX_EPOCH},
};
@ -62,21 +63,26 @@ impl Item {
// Actions
/// Take object processed, commit its changes
pub fn commit(self) -> Result<String> {
/// Commit changes, return permanent Self
pub fn commit(mut self) -> Result<Self, (Self, Error)> {
match self.path.to_str() {
Some(old) => match old.strip_suffix(TMP_SUFFIX) {
Some(new) => {
rename(old, new)?;
Ok(new.to_string())
}
None => bail!("Invalid TMP suffix"), // | panic
Some(new) => match rename(old, new) {
Ok(()) => {
self.path = match PathBuf::from_str(new) {
Ok(path) => path,
Err(e) => return Err((self, anyhow!(e))),
};
Ok(self)
}
Err(e) => Err((self, anyhow!(e))),
},
None => Err((self, anyhow!("Unexpected suffix"))),
},
None => bail!("Invalid Item path"), // | panic
None => Err((self, anyhow!("Unexpected file path"))),
}
}
/// Cleanup object relationships
pub fn delete(self) -> Result<()> {
Ok(remove_file(self.path)?)
}