From 6aeb4521fc2320a00f3392d623d5916c2977d48a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 3 Nov 2024 13:28:18 +0000 Subject: [PATCH] refactor(fs): use type aliases --- src/fs/dir.rs | 21 +++++---------------- src/fs/file.rs | 8 ++------ src/fs/path.rs | 8 +++++--- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/fs/dir.rs b/src/fs/dir.rs index 77c068f..c8d11b3 100644 --- a/src/fs/dir.rs +++ b/src/fs/dir.rs @@ -1,10 +1,7 @@ // use crate::fs::{DirItem, DirItemIterator, Result}; -use super::{ - path::{DirMarker, PathReal}, - DirHandle, Error, FileMarker, PathMarker, -}; +use super::{DirHandle, Error, FileHandle, PathHandle}; impl<'base, 'path> DirHandle<'base, 'path> { /// Creates a new, empty directory at the path @@ -62,14 +59,10 @@ impl<'base, 'path> DirHandle<'base, 'path> { Ok(Box::new(DirItemIterator::new(read_dir))) } } -impl<'base, 'path> TryFrom> - for PathReal<'base, 'path, FileMarker> -{ +impl<'base, 'path> TryFrom> for FileHandle<'base, 'path> { type Error = crate::fs::Error; - fn try_from( - path: PathReal<'base, 'path, PathMarker>, - ) -> std::result::Result { + fn try_from(path: PathHandle<'base, 'path>) -> std::result::Result { match path.as_file() { Ok(Some(dir)) => Ok(dir.clone()), Ok(None) => Err(crate::fs::Error::NotADirectory { @@ -79,14 +72,10 @@ impl<'base, 'path> TryFrom> } } } -impl<'base, 'path> TryFrom> - for PathReal<'base, 'path, DirMarker> -{ +impl<'base, 'path> TryFrom> for DirHandle<'base, 'path> { type Error = crate::fs::Error; - fn try_from( - path: PathReal<'base, 'path, PathMarker>, - ) -> std::result::Result { + fn try_from(path: PathHandle<'base, 'path>) -> 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 9c8b7e4..2618002 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -1,11 +1,7 @@ // use crate::fs::Result; -use super::{ - path::{FileMarker, PathReal}, - reader::Reader, - Error, FileHandle, -}; +use super::{reader::Reader, Error, FileHandle}; impl<'base, 'path> FileHandle<'base, 'path> { /// Returns a [Reader] for the file. @@ -58,7 +54,7 @@ impl<'base, 'path> FileHandle<'base, 'path> { /// # Ok(()) /// # } /// ``` - pub fn copy(&self, dest: &PathReal<'base, 'path, FileMarker>) -> Result { + pub fn copy(&self, dest: &FileHandle<'base, 'path>) -> Result { self.check_error()?; std::fs::copy(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io) } diff --git a/src/fs/path.rs b/src/fs/path.rs index 50169d5..2e4e529 100644 --- a/src/fs/path.rs +++ b/src/fs/path.rs @@ -6,6 +6,8 @@ use std::{ use crate::fs::{Error, Result}; +use super::{DirHandle, FileHandle, PathHandle}; + /// Marker trait for the type of [PathReal]. pub trait PathType {} @@ -170,7 +172,7 @@ 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))) @@ -193,7 +195,7 @@ 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))) @@ -202,7 +204,7 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { } } } -impl From> for PathBuf { +impl<'base, 'path> From> for PathBuf { fn from(path: PathReal) -> Self { path.base.join(path.path) }