refactor(fs): PathReal owns its own data

This commit is contained in:
Paul Campbell 2024-11-03 20:44:27 +00:00
parent 10a745899d
commit e576e6e579
4 changed files with 38 additions and 42 deletions

View file

@ -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<PathHandle<'base, 'path, PathMarker>> for FileHandle<'base, 'path> {
impl TryFrom<PathHandle<PathMarker>> for FileHandle {
type Error = crate::fs::Error;
fn try_from(
path: PathHandle<'base, 'path, PathMarker>,
) -> std::result::Result<Self, Self::Error> {
fn try_from(path: PathHandle<PathMarker>) -> std::result::Result<Self, Self::Error> {
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<PathHandle<'base, 'path, PathMarker>> for FileHandle<
}
}
}
impl<'base, 'path> TryFrom<PathHandle<'base, 'path, PathMarker>> for DirHandle<'base, 'path> {
impl TryFrom<PathHandle<PathMarker>> for DirHandle {
type Error = crate::fs::Error;
fn try_from(
path: PathHandle<'base, 'path, PathMarker>,
) -> std::result::Result<Self, Self::Error> {
fn try_from(path: PathHandle<PathMarker>) -> std::result::Result<Self, Self::Error> {
match path.as_dir() {
Ok(Some(dir)) => Ok(dir.clone()),
Ok(None) => Err(crate::fs::Error::NotADirectory {

View file

@ -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<u64> {
pub fn copy(&self, dest: &FileHandle) -> Result<u64> {
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)
}

View file

@ -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<T: PathType> {
base: PathBuf,
path: PathBuf,
_phanton: PhantomData<T>,
pub(super) error: Option<Error>,
}
impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
pub(super) fn new(base: &'base Path, path: &'path Path) -> Self {
impl<T: PathType> PathReal<T> {
pub(super) fn new(base: impl Into<PathBuf>, path: impl Into<PathBuf>) -> Self {
let base: PathBuf = base.into();
let path: PathBuf = path.into();
let error = PathReal::<T>::validate(&base, &path);
Self {
base,
path,
_phanton: PhantomData::<T>,
error: PathReal::<T>::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<Option<DirHandle<'base, 'path>>> {
pub fn as_dir(&self) -> Result<Option<DirHandle>> {
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<Option<FileHandle<'base, 'path>>> {
pub fn as_file(&self) -> Result<Option<FileHandle>> {
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<T>) -> 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<PathMarker>) -> 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<PathHandle<'base, 'path, PathMarker>> for PathBuf {
impl From<PathHandle<PathMarker>> for PathBuf {
fn from(path: PathReal<PathMarker>) -> Self {
path.base.join(path.path)
}
}
impl<'base, 'path> From<DirHandle<'base, 'path>> for PathHandle<'base, 'path, PathMarker> {
fn from(dir: DirHandle<'base, 'path>) -> Self {
impl From<DirHandle> for PathHandle<PathMarker> {
fn from(dir: DirHandle) -> Self {
PathReal::new(dir.base, dir.path)
}
}
impl<'base, 'path> From<FileHandle<'base, 'path>> for PathHandle<'base, 'path, PathMarker> {
fn from(file: FileHandle<'base, 'path>) -> Self {
impl From<FileHandle> for PathHandle<PathMarker> {
fn from(file: FileHandle) -> Self {
PathReal::new(file.base, file.path)
}
}

View file

@ -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<DirMarker>;
/// Represents a file path in the filesystem.
pub type FileHandle<'base, 'path> = PathReal<'base, 'path, FileMarker>;
pub type FileHandle = PathReal<FileMarker>;
/// Represents a path in the filesystem.
pub type PathHandle<'base, 'path, T> = PathReal<'base, 'path, T>;
pub type PathHandle<T> = PathReal<T>;
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<PathMarker> {
PathReal::new(&self.base, path)
}
fn validate(&self, path: &Path) -> Result<()> {