kxio 3.2.0

Provides injectable Filesystem and Network resources to make code more testable
Documentation
use std::sync::{Arc, Mutex};

use tempfile::TempDir;

use super::{Error, FileSystem};

#[derive(Clone, Debug)]
pub struct TempFileSystem {
    real: FileSystem,
    _temp_dir: Arc<Mutex<TempDir>>,
}
impl TempFileSystem {
    #[tracing::instrument]
    pub fn new() -> super::Result<Self> {
        let temp_dir = tempfile::tempdir().map_err(Error::Io)?;
        let base = temp_dir.path().to_path_buf();
        let temp_dir = Arc::new(Mutex::new(temp_dir));
        let real = super::new(base);

        Ok(Self {
            real,
            _temp_dir: temp_dir,
        })
    }

    /// Create a clone of the wrapped [FileSystem].
    pub fn as_real(&self) -> FileSystem {
        self.real.clone()
    }
}
impl std::ops::Deref for TempFileSystem {
    type Target = FileSystem;

    fn deref(&self) -> &Self::Target {
        &self.real
    }
}
impl Drop for TempFileSystem {
    #[cfg_attr(test, mutants::skip)]
    #[tracing::instrument]
    fn drop(&mut self) {
        tracing::debug!("drop");
    }
}