mirror of
https://github.com/YGGverse/titanit.git
synced 2026-04-02 01:55:34 +00:00
handle errors
This commit is contained in:
parent
08c437920c
commit
04a276116d
2 changed files with 121 additions and 77 deletions
|
|
@ -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)?)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue