kxio 1.1.2

Provides injectable Filesystem and Network resources to make code more testable
Documentation
use std::path::PathBuf;

use derive_more::From;

use crate::fs::like::FileSystemLike;

mod like;
mod real;
mod temp;

#[derive(Debug, From, derive_more::Display)]
pub enum Error {
    Io(std::io::Error),

    #[display("Path access attempted outside of base ({base:?}): {path:?}")]
    PathTraversal {
        base: PathBuf,
        path: PathBuf,
    },
}
impl std::error::Error for Error {}

pub type Result<T> = core::result::Result<T, Error>;

pub const fn new(base: PathBuf) -> FileSystem {
    FileSystem::Real(real::new(base))
}

pub fn temp() -> Result<FileSystem> {
    temp::new().map(FileSystem::Temp)
}

#[derive(Clone, Debug)]
pub enum FileSystem {
    Real(real::RealFileSystem),
    Temp(temp::TempFileSystem),
}
impl std::ops::Deref for FileSystem {
    type Target = dyn FileSystemLike;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Real(fs) => fs,
            Self::Temp(fs) => fs.deref(),
        }
    }
}