use std::path::{Path, PathBuf};
use crate::Result;
pub(crate) struct Transient {
path: PathBuf,
what: &'static str,
held: bool,
}
impl Transient {
pub(crate) fn write(path: PathBuf, contents: &[u8], what: &'static str) -> Result<Transient> {
super::write(&path, contents)?;
Ok(Transient { path, what, held: true })
}
pub(crate) fn path(&self) -> &Path {
&self.path
}
pub(crate) fn remove_after<T>(mut self, done: Result<T>) -> Result<T> {
self.held = false;
let removed = super::remove(&self.path);
if let (Err(_), Err(e)) = (&done, &removed) {
tracing::error!(error = %e, "{} was left in the run's state folder", self.what);
}
done.and_then(|value| removed.map(|()| value))
}
}
impl Drop for Transient {
fn drop(&mut self) {
if self.held
&& let Err(e) = super::remove_path(&self.path)
{
tracing::error!(error = %e, path = %self.path.display(), "{}, cut short, was left in the run's state folder", self.what);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_goes_once_used_and_when_dropped_unused() {
let dir = tempfile::tempdir().unwrap();
let used = Transient::write(dir.path().join("a"), b"s3cret", "a test's file").unwrap();
let used_at = used.path().to_path_buf();
assert_eq!(used.remove_after(Ok(7)).unwrap(), 7);
assert!(!used_at.exists());
let cut = Transient::write(dir.path().join("b"), b"s3cret", "a test's file").unwrap();
let cut_at = cut.path().to_path_buf();
drop(cut);
assert!(!cut_at.exists(), "a use cut short removes it too");
}
#[test]
fn a_failed_use_keeps_its_own_error() {
let dir = tempfile::tempdir().unwrap();
let file = Transient::write(dir.path().join("a"), b"s3cret", "a test's file").unwrap();
let at = file.path().to_path_buf();
let failed: Result<()> = Err(crate::error::Error::NoConsole {
machine: "web".into(),
why: "refused".into(),
});
assert_eq!(file.remove_after(failed).unwrap_err().code(), crate::ErrorCode::NoConsole);
assert!(!at.exists());
}
}