refactor(fs): rename internal structs

trait *Env => *Like
struct *Env => * (remove suffix)
This commit is contained in:
Paul Campbell 2024-04-27 18:11:51 +01:00
parent 299d429214
commit c513664979

View file

@ -1,5 +1,3 @@
#![allow(unused)]
use std::{
ops::Deref,
path::PathBuf,
@ -11,20 +9,20 @@ use tracing::{debug, info};
#[derive(Clone, Debug)]
pub enum FileSystem {
Real(RealFileSystemEnv),
Temp(TempFileSystemEnv),
Real(RealFileSystem),
Temp(TempFileSystem),
}
impl FileSystem {
pub fn new_real(cwd: Option<PathBuf>) -> Self {
let cwd = cwd.unwrap_or_default();
Self::Real(RealFileSystemEnv::new(cwd))
Self::Real(RealFileSystem::new(cwd))
}
pub fn new_temp() -> std::io::Result<Self> {
TempFileSystemEnv::new().map(Self::Temp)
TempFileSystem::new().map(Self::Temp)
}
}
impl Deref for FileSystem {
type Target = dyn FileSystemEnv;
type Target = dyn FileSystemLike;
fn deref(&self) -> &Self::Target {
match self {
@ -34,7 +32,7 @@ impl Deref for FileSystem {
}
}
pub trait FileSystemEnv: Sync + Send + std::fmt::Debug {
pub trait FileSystemLike: Sync + Send + std::fmt::Debug {
fn cwd(&self) -> &PathBuf;
fn in_cwd(&self, name: &str) -> PathBuf {
@ -72,41 +70,47 @@ pub trait FileSystemEnv: Sync + Send + std::fmt::Debug {
}
#[derive(Clone, Debug, Default)]
pub struct RealFileSystemEnv {
pub struct RealFileSystem {
cwd: PathBuf,
}
#[derive(Clone, Debug)]
pub struct TempFileSystemEnv {
pub struct TempFileSystem {
cwd: PathBuf,
temp_dir: Arc<Mutex<TempDir>>,
// Handle to the temporary directory
// When this handle is dropped the directory is deleted
_temp_dir: Arc<Mutex<TempDir>>,
}
impl FileSystemEnv for TempFileSystemEnv {
impl FileSystemLike for TempFileSystem {
fn cwd(&self) -> &PathBuf {
&self.cwd
}
}
impl FileSystemEnv for RealFileSystemEnv {
impl FileSystemLike for RealFileSystem {
fn cwd(&self) -> &PathBuf {
&self.cwd
}
}
impl RealFileSystemEnv {
impl RealFileSystem {
const fn new(cwd: PathBuf) -> Self {
Self { cwd }
}
}
impl TempFileSystemEnv {
impl TempFileSystem {
fn new() -> std::io::Result<Self> {
let temp_dir = tempdir()?;
info!("temp dir: {:?}", temp_dir.path());
let cwd = temp_dir.path().to_path_buf();
let temp_dir = Arc::new(Mutex::new(temp_dir));
Ok(Self { cwd, temp_dir })
Ok(Self {
cwd,
_temp_dir: temp_dir,
})
}
}
@ -120,13 +124,13 @@ mod tests {
#[test_log::test]
fn test_cwd() {
let cwd = PathBuf::from("/tmp");
let env = RealFileSystemEnv::new(cwd.clone());
let env = RealFileSystem::new(cwd.clone());
assert_eq!(env.cwd(), &cwd);
}
#[test_log::test]
fn test_create_on_temp_fs() -> std::io::Result<()> {
let env = TempFileSystemEnv::new()?;
let env = TempFileSystem::new()?;
assert!(env.cwd().exists());
Ok(())
}
@ -134,13 +138,13 @@ mod tests {
#[test_log::test]
fn test_create_on_real_fs() {
let cwd = PathBuf::from("/tmp");
let env = RealFileSystemEnv::new(cwd.clone());
let env = RealFileSystem::new(cwd.clone());
assert_eq!(env.cwd(), &cwd);
}
#[test_log::test]
fn test_write_and_read_file() -> std::io::Result<()> {
let env = TempFileSystemEnv::new()?;
let env = TempFileSystem::new()?;
let file_name = "test.txt";
let content = "Hello, World!";
let path = env.write_file(file_name, content)?;