refactor(fs): PathReal owns its own data
This commit is contained in:
parent
10a745899d
commit
e576e6e579
4 changed files with 38 additions and 42 deletions
|
@ -3,7 +3,7 @@ use crate::fs::{DirItem, DirItemIterator, Result};
|
||||||
|
|
||||||
use super::{DirHandle, Error, FileHandle, PathHandle, PathMarker};
|
use super::{DirHandle, Error, FileHandle, PathHandle, PathMarker};
|
||||||
|
|
||||||
impl<'base, 'path> DirHandle<'base, 'path> {
|
impl DirHandle {
|
||||||
/// Creates a new, empty directory at the path
|
/// Creates a new, empty directory at the path
|
||||||
///
|
///
|
||||||
/// Wrapper for [std::fs::create_dir]
|
/// 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)
|
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;
|
type Error = crate::fs::Error;
|
||||||
|
|
||||||
fn try_from(
|
fn try_from(path: PathHandle<PathMarker>) -> std::result::Result<Self, Self::Error> {
|
||||||
path: PathHandle<'base, 'path, PathMarker>,
|
|
||||||
) -> std::result::Result<Self, Self::Error> {
|
|
||||||
match path.as_file() {
|
match path.as_file() {
|
||||||
Ok(Some(dir)) => Ok(dir.clone()),
|
Ok(Some(dir)) => Ok(dir.clone()),
|
||||||
Ok(None) => Err(crate::fs::Error::NotADirectory {
|
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;
|
type Error = crate::fs::Error;
|
||||||
|
|
||||||
fn try_from(
|
fn try_from(path: PathHandle<PathMarker>) -> std::result::Result<Self, Self::Error> {
|
||||||
path: PathHandle<'base, 'path, PathMarker>,
|
|
||||||
) -> std::result::Result<Self, Self::Error> {
|
|
||||||
match path.as_dir() {
|
match path.as_dir() {
|
||||||
Ok(Some(dir)) => Ok(dir.clone()),
|
Ok(Some(dir)) => Ok(dir.clone()),
|
||||||
Ok(None) => Err(crate::fs::Error::NotADirectory {
|
Ok(None) => Err(crate::fs::Error::NotADirectory {
|
||||||
|
|
|
@ -3,7 +3,7 @@ use crate::fs::Result;
|
||||||
|
|
||||||
use super::{reader::Reader, Error, FileHandle};
|
use super::{reader::Reader, Error, FileHandle};
|
||||||
|
|
||||||
impl<'base, 'path> FileHandle<'base, 'path> {
|
impl FileHandle {
|
||||||
/// Returns a [Reader] for the file.
|
/// Returns a [Reader] for the file.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -54,7 +54,7 @@ impl<'base, 'path> FileHandle<'base, 'path> {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn copy(&self, dest: &FileHandle<'base, 'path>) -> Result<u64> {
|
pub fn copy(&self, dest: &FileHandle) -> Result<u64> {
|
||||||
self.check_error()?;
|
self.check_error()?;
|
||||||
std::fs::copy(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io)
|
std::fs::copy(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io)
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ impl<'base, 'path> FileHandle<'base, 'path> {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn hard_link(&self, dest: &FileHandle<'base, 'path>) -> Result<()> {
|
pub fn hard_link(&self, dest: &FileHandle) -> Result<()> {
|
||||||
self.check_error()?;
|
self.check_error()?;
|
||||||
std::fs::hard_link(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io)
|
std::fs::hard_link(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io)
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,19 +30,22 @@ impl PathType for DirMarker {}
|
||||||
///
|
///
|
||||||
/// It can be a simple path, or it can be a file or a directory.
|
/// It can be a simple path, or it can be a file or a directory.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct PathReal<'base, 'path, T: PathType> {
|
pub struct PathReal<T: PathType> {
|
||||||
base: &'base Path,
|
base: PathBuf,
|
||||||
path: &'path Path,
|
path: PathBuf,
|
||||||
_phanton: PhantomData<T>,
|
_phanton: PhantomData<T>,
|
||||||
pub(super) error: Option<Error>,
|
pub(super) error: Option<Error>,
|
||||||
}
|
}
|
||||||
impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
|
impl<T: PathType> PathReal<T> {
|
||||||
pub(super) fn new(base: &'base Path, path: &'path Path) -> Self {
|
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 {
|
Self {
|
||||||
base,
|
base,
|
||||||
path,
|
path,
|
||||||
_phanton: PhantomData::<T>,
|
_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 {
|
pub fn as_pathbuf(&self) -> PathBuf {
|
||||||
self.base.join(self.path)
|
self.base.join(&self.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn put(&mut self, error: Error) {
|
pub(super) fn put(&mut self, error: Error) {
|
||||||
|
@ -172,10 +175,10 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn as_dir(&self) -> Result<Option<DirHandle<'base, 'path>>> {
|
pub fn as_dir(&self) -> Result<Option<DirHandle>> {
|
||||||
self.check_error()?;
|
self.check_error()?;
|
||||||
if self.as_pathbuf().is_dir() {
|
if self.as_pathbuf().is_dir() {
|
||||||
Ok(Some(PathReal::new(self.base, self.path)))
|
Ok(Some(PathReal::new(&self.base, &self.path)))
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
@ -195,10 +198,10 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn as_file(&self) -> Result<Option<FileHandle<'base, 'path>>> {
|
pub fn as_file(&self) -> Result<Option<FileHandle>> {
|
||||||
self.check_error()?;
|
self.check_error()?;
|
||||||
if self.as_pathbuf().is_file() {
|
if self.as_pathbuf().is_file() {
|
||||||
Ok(Some(PathReal::new(self.base, self.path)))
|
Ok(Some(PathReal::new(&self.base, &self.path)))
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
@ -221,7 +224,7 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn rename(&self, dest: &PathHandle<'base, 'path, T>) -> Result<()> {
|
pub fn rename(&self, dest: &PathHandle<T>) -> Result<()> {
|
||||||
self.check_error()?;
|
self.check_error()?;
|
||||||
std::fs::rename(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io)
|
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(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn soft_link(&self, link: &PathReal<'_, '_, PathMarker>) -> Result<()> {
|
pub fn soft_link(&self, link: &PathReal<PathMarker>) -> Result<()> {
|
||||||
self.check_error()?;
|
self.check_error()?;
|
||||||
std::os::unix::fs::symlink(self.as_pathbuf(), link.as_pathbuf()).map_err(Error::Io)
|
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)
|
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 {
|
fn from(path: PathReal<PathMarker>) -> Self {
|
||||||
path.base.join(path.path)
|
path.base.join(path.path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<'base, 'path> From<DirHandle<'base, 'path>> for PathHandle<'base, 'path, PathMarker> {
|
impl From<DirHandle> for PathHandle<PathMarker> {
|
||||||
fn from(dir: DirHandle<'base, 'path>) -> Self {
|
fn from(dir: DirHandle) -> Self {
|
||||||
PathReal::new(dir.base, dir.path)
|
PathReal::new(dir.base, dir.path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<'base, 'path> From<FileHandle<'base, 'path>> for PathHandle<'base, 'path, PathMarker> {
|
impl From<FileHandle> for PathHandle<PathMarker> {
|
||||||
fn from(file: FileHandle<'base, 'path>) -> Self {
|
fn from(file: FileHandle) -> Self {
|
||||||
PathReal::new(file.base, file.path)
|
PathReal::new(file.base, file.path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,13 +15,13 @@ pub struct FileSystem {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a directory path in the 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.
|
/// 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.
|
/// Represents a path in the filesystem.
|
||||||
pub type PathHandle<'base, 'path, T> = PathReal<'base, 'path, T>;
|
pub type PathHandle<T> = PathReal<T>;
|
||||||
|
|
||||||
impl FileSystem {
|
impl FileSystem {
|
||||||
pub const fn new(base: PathBuf) -> Self {
|
pub const fn new(base: PathBuf) -> Self {
|
||||||
|
@ -55,8 +55,8 @@ impl FileSystem {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn dir<'base, 'path>(&'base self, path: &'path Path) -> DirHandle<'base, 'path> {
|
pub fn dir(&self, path: &Path) -> DirHandle {
|
||||||
let mut dir = PathReal::new(self.base(), path);
|
let mut dir = PathReal::new(&self.base, path);
|
||||||
|
|
||||||
if dir.error.is_none() {
|
if dir.error.is_none() {
|
||||||
if let Ok(exists) = dir.exists() {
|
if let Ok(exists) = dir.exists() {
|
||||||
|
@ -90,8 +90,8 @@ impl FileSystem {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn file<'base, 'path>(&'base self, path: &'path Path) -> FileHandle<'base, 'path> {
|
pub fn file(&self, path: &Path) -> FileHandle {
|
||||||
let mut file = PathReal::new(self.base(), path);
|
let mut file = PathReal::new(&self.base, path);
|
||||||
|
|
||||||
if file.error.is_none() {
|
if file.error.is_none() {
|
||||||
if let Ok(exists) = file.exists() {
|
if let Ok(exists) = file.exists() {
|
||||||
|
@ -124,11 +124,8 @@ impl FileSystem {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn path<'base, 'path>(
|
pub fn path(&self, path: &Path) -> PathHandle<PathMarker> {
|
||||||
&'base self,
|
PathReal::new(&self.base, path)
|
||||||
path: &'path Path,
|
|
||||||
) -> PathHandle<'base, 'path, PathMarker> {
|
|
||||||
PathReal::new(self.base(), path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate(&self, path: &Path) -> Result<()> {
|
fn validate(&self, path: &Path) -> Result<()> {
|
||||||
|
|
Loading…
Reference in a new issue