kxio/src/fs/mod.rs

45 lines
898 B
Rust
Raw Normal View History

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,
},
}
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)
}
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(),
}
}
}