From b512b0a0d8af92bc074dd635b227e94803f81cd8 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 3 Nov 2024 20:44:27 +0000 Subject: [PATCH] refactor(fs): PathReal owns its own data --- src/fs/dir.rs | 14 +++++--------- src/fs/file.rs | 6 +++--- src/fs/path.rs | 39 +++++++++++++++++++++------------------ src/fs/system.rs | 21 +++++++++------------ 4 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/fs/dir.rs b/src/fs/dir.rs index 27402b9..69d39a0 100644 --- a/src/fs/dir.rs +++ b/src/fs/dir.rs @@ -3,7 +3,7 @@ use crate::fs::{DirItem, DirItemIterator, Result}; use super::{DirHandle, Error, FileHandle, PathHandle, PathMarker}; -impl<'base, 'path> DirHandle<'base, 'path> { +impl DirHandle { /// Creates a new, empty directory at the path /// /// Wrapper for [std::fs::create_dir] @@ -93,12 +93,10 @@ impl<'base, 'path> DirHandle<'base, 'path> { std::fs::remove_dir_all(self.as_pathbuf()).map_err(Error::Io) } } -impl<'base, 'path> TryFrom> for FileHandle<'base, 'path> { +impl TryFrom> for FileHandle { type Error = crate::fs::Error; - fn try_from( - path: PathHandle<'base, 'path, PathMarker>, - ) -> std::result::Result { + fn try_from(path: PathHandle) -> std::result::Result { match path.as_file() { Ok(Some(dir)) => Ok(dir.clone()), Ok(None) => Err(crate::fs::Error::NotADirectory { @@ -108,12 +106,10 @@ impl<'base, 'path> TryFrom> for FileHandle< } } } -impl<'base, 'path> TryFrom> for DirHandle<'base, 'path> { +impl TryFrom> for DirHandle { type Error = crate::fs::Error; - fn try_from( - path: PathHandle<'base, 'path, PathMarker>, - ) -> std::result::Result { + fn try_from(path: PathHandle) -> std::result::Result { match path.as_dir() { Ok(Some(dir)) => Ok(dir.clone()), Ok(None) => Err(crate::fs::Error::NotADirectory { diff --git a/src/fs/file.rs b/src/fs/file.rs index 57b8e92..99ba5ba 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -3,7 +3,7 @@ use crate::fs::Result; use super::{reader::Reader, Error, FileHandle}; -impl<'base, 'path> FileHandle<'base, 'path> { +impl FileHandle { /// Returns a [Reader] for the file. /// /// ``` @@ -54,7 +54,7 @@ impl<'base, 'path> FileHandle<'base, 'path> { /// # Ok(()) /// # } /// ``` - pub fn copy(&self, dest: &FileHandle<'base, 'path>) -> Result { + pub fn copy(&self, dest: &FileHandle) -> Result { self.check_error()?; std::fs::copy(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io) } @@ -94,7 +94,7 @@ impl<'base, 'path> FileHandle<'base, 'path> { /// # Ok(()) /// # } /// ``` - pub fn hard_link(&self, dest: &FileHandle<'base, 'path>) -> Result<()> { + pub fn hard_link(&self, dest: &FileHandle) -> Result<()> { self.check_error()?; std::fs::hard_link(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io) } diff --git a/src/fs/path.rs b/src/fs/path.rs index 101e69a..7558bde 100644 --- a/src/fs/path.rs +++ b/src/fs/path.rs @@ -30,19 +30,22 @@ impl PathType for DirMarker {} /// /// It can be a simple path, or it can be a file or a directory. #[derive(Clone, Debug)] -pub struct PathReal<'base, 'path, T: PathType> { - base: &'base Path, - path: &'path Path, +pub struct PathReal { + base: PathBuf, + path: PathBuf, _phanton: PhantomData, pub(super) error: Option, } -impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { - pub(super) fn new(base: &'base Path, path: &'path Path) -> Self { +impl PathReal { + pub(super) fn new(base: impl Into, path: impl Into) -> Self { + let base: PathBuf = base.into(); + let path: PathBuf = path.into(); + let error = PathReal::::validate(&base, &path); Self { base, path, _phanton: PhantomData::, - error: PathReal::::validate(base, path), + error, } } @@ -59,7 +62,7 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { /// # } /// ``` pub fn as_pathbuf(&self) -> PathBuf { - self.base.join(self.path) + self.base.join(&self.path) } pub(super) fn put(&mut self, error: Error) { @@ -172,10 +175,10 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { /// # Ok(()) /// # } /// ``` - pub fn as_dir(&self) -> Result>> { + pub fn as_dir(&self) -> Result> { self.check_error()?; if self.as_pathbuf().is_dir() { - Ok(Some(PathReal::new(self.base, self.path))) + Ok(Some(PathReal::new(&self.base, &self.path))) } else { Ok(None) } @@ -195,10 +198,10 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { /// # Ok(()) /// # } /// ``` - pub fn as_file(&self) -> Result>> { + pub fn as_file(&self) -> Result> { self.check_error()?; if self.as_pathbuf().is_file() { - Ok(Some(PathReal::new(self.base, self.path))) + Ok(Some(PathReal::new(&self.base, &self.path))) } else { Ok(None) } @@ -221,7 +224,7 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { /// # Ok(()) /// # } /// ``` - pub fn rename(&self, dest: &PathHandle<'base, 'path, T>) -> Result<()> { + pub fn rename(&self, dest: &PathHandle) -> Result<()> { self.check_error()?; std::fs::rename(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io) } @@ -261,7 +264,7 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { /// # Ok(()) /// # } /// ``` - pub fn soft_link(&self, link: &PathReal<'_, '_, PathMarker>) -> Result<()> { + pub fn soft_link(&self, link: &PathReal) -> Result<()> { self.check_error()?; std::os::unix::fs::symlink(self.as_pathbuf(), link.as_pathbuf()).map_err(Error::Io) } @@ -342,18 +345,18 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { std::fs::set_permissions(self.as_pathbuf(), perms).map_err(Error::Io) } } -impl<'base, 'path> From> for PathBuf { +impl From> for PathBuf { fn from(path: PathReal) -> Self { path.base.join(path.path) } } -impl<'base, 'path> From> for PathHandle<'base, 'path, PathMarker> { - fn from(dir: DirHandle<'base, 'path>) -> Self { +impl From for PathHandle { + fn from(dir: DirHandle) -> Self { PathReal::new(dir.base, dir.path) } } -impl<'base, 'path> From> for PathHandle<'base, 'path, PathMarker> { - fn from(file: FileHandle<'base, 'path>) -> Self { +impl From for PathHandle { + fn from(file: FileHandle) -> Self { PathReal::new(file.base, file.path) } } diff --git a/src/fs/system.rs b/src/fs/system.rs index 217cced..8598668 100644 --- a/src/fs/system.rs +++ b/src/fs/system.rs @@ -15,13 +15,13 @@ pub struct FileSystem { } /// Represents a directory path in the filesystem. -pub type DirHandle<'base, 'path> = PathReal<'base, 'path, DirMarker>; +pub type DirHandle = PathReal; /// Represents a file path in the filesystem. -pub type FileHandle<'base, 'path> = PathReal<'base, 'path, FileMarker>; +pub type FileHandle = PathReal; /// Represents a path in the filesystem. -pub type PathHandle<'base, 'path, T> = PathReal<'base, 'path, T>; +pub type PathHandle = PathReal; impl FileSystem { pub const fn new(base: PathBuf) -> Self { @@ -55,8 +55,8 @@ impl FileSystem { /// # Ok(()) /// # } /// ``` - pub fn dir<'base, 'path>(&'base self, path: &'path Path) -> DirHandle<'base, 'path> { - let mut dir = PathReal::new(self.base(), path); + pub fn dir(&self, path: &Path) -> DirHandle { + let mut dir = PathReal::new(&self.base, path); if dir.error.is_none() { if let Ok(exists) = dir.exists() { @@ -90,8 +90,8 @@ impl FileSystem { /// # Ok(()) /// # } /// ``` - pub fn file<'base, 'path>(&'base self, path: &'path Path) -> FileHandle<'base, 'path> { - let mut file = PathReal::new(self.base(), path); + pub fn file(&self, path: &Path) -> FileHandle { + let mut file = PathReal::new(&self.base, path); if file.error.is_none() { if let Ok(exists) = file.exists() { @@ -124,11 +124,8 @@ impl FileSystem { /// # Ok(()) /// # } /// ``` - pub fn path<'base, 'path>( - &'base self, - path: &'path Path, - ) -> PathHandle<'base, 'path, PathMarker> { - PathReal::new(self.base(), path) + pub fn path(&self, path: &Path) -> PathHandle { + PathReal::new(&self.base, path) } fn validate(&self, path: &Path) -> Result<()> {