From c51366497937ca17e2cd92288f4953d3ce3d8c54 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 27 Apr 2024 18:11:51 +0100 Subject: [PATCH] refactor(fs): rename internal structs trait *Env => *Like struct *Env => * (remove suffix) --- src/filesystem.rs | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/filesystem.rs b/src/filesystem.rs index 96dc633..0f9cd80 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -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) -> Self { let cwd = cwd.unwrap_or_default(); - Self::Real(RealFileSystemEnv::new(cwd)) + Self::Real(RealFileSystem::new(cwd)) } pub fn new_temp() -> std::io::Result { - 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>, + + // Handle to the temporary directory + // When this handle is dropped the directory is deleted + _temp_dir: Arc>, } -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 { 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)?;