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