From 587d60ee3db167c4f279d40ed5b7b4a222ff1d0c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 1 Nov 2024 19:20:41 +0000 Subject: [PATCH] feat(fs): add as_dir/as_file to convert from path --- src/fs/real/dir.rs | 6 ++++- src/fs/real/file.rs | 6 ++++- src/fs/real/path.rs | 20 +++++++++++++++ tests/fs.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/fs/real/dir.rs b/src/fs/real/dir.rs index 4a1349c..eba0c4f 100644 --- a/src/fs/real/dir.rs +++ b/src/fs/real/dir.rs @@ -1,5 +1,5 @@ // -use std::path::Path; +use std::path::{Path, PathBuf}; use crate::fs::{DirItem, DirItemIterator, Error, Result}; @@ -27,6 +27,10 @@ impl<'base, 'path> DirReal<'base, 'path> { Self { path } } + pub fn path(&self) -> PathBuf { + self.path.full_path() + } + pub fn create(&mut self) -> Result<()> { self.path.check_error()?; std::fs::create_dir(self.path.full_path()).map_err(Into::into) diff --git a/src/fs/real/file.rs b/src/fs/real/file.rs index 4aef521..da4a74e 100644 --- a/src/fs/real/file.rs +++ b/src/fs/real/file.rs @@ -1,5 +1,5 @@ // -use std::path::Path; +use std::path::{Path, PathBuf}; use crate::fs::Result; @@ -15,6 +15,10 @@ impl<'base, 'path> FileReal<'base, 'path> { } } + pub fn path(&self) -> PathBuf { + self.path.full_path() + } + pub fn reader(&mut self) -> Result { self.path.check_error()?; ReaderReal::new(&self.path.full_path()) diff --git a/src/fs/real/path.rs b/src/fs/real/path.rs index 94e0438..772172b 100644 --- a/src/fs/real/path.rs +++ b/src/fs/real/path.rs @@ -3,6 +3,8 @@ use std::path::{Path, PathBuf}; use crate::fs::{Error, Result}; +use super::{dir::DirReal, file::FileReal}; + #[derive(Debug)] pub struct PathReal<'base, 'path> { base: &'base Path, @@ -74,6 +76,24 @@ impl<'base, 'path> PathReal<'base, 'path> { self.check_error()?; Ok(self.full_path().is_file()) } + + pub fn as_dir(&mut self) -> Result>> { + self.check_error()?; + if self.full_path().is_dir() { + Ok(Some(DirReal::new(self.base, self.path))) + } else { + Ok(None) + } + } + + pub fn as_file(&mut self) -> Result>> { + self.check_error()?; + if self.full_path().is_file() { + Ok(Some(FileReal::new(self.base, self.path))) + } else { + Ok(None) + } + } } impl From> for PathBuf { fn from(path: PathReal) -> Self { diff --git a/tests/fs.rs b/tests/fs.rs index a2d271a..2c32f7d 100644 --- a/tests/fs.rs +++ b/tests/fs.rs @@ -32,6 +32,65 @@ mod path_of { } } +mod path { + use super::*; + + #[test] + fn path_is_dir_as_dir_some() -> TestResult { + let fs = fs::temp().expect("temp fs"); + + let path = fs.base().join("foo"); + let mut dir = fs.dir(&path); + dir.create().expect("create"); + + let_assert!(Ok(Some(as_dir)) = fs.path(&path).as_dir()); + + assert_eq!(dir.path(), as_dir.path()); + + Ok(()) + } + + #[test] + fn path_is_file_as_dir_none() -> TestResult { + let fs = fs::temp().expect("temp fs"); + + let path = fs.base().join("foo"); + let mut file = fs.file(&path); + file.write("contents").expect("create"); + + let_assert!(Ok(Some(mut as_file)) = fs.path(&path).as_file()); + + assert_eq!(file.path(), as_file.path()); + assert_eq!(as_file.reader().expect("reader").to_string(), "contents"); + + Ok(()) + } + #[test] + fn path_is_dir_as_file_none() -> TestResult { + let fs = fs::temp().expect("temp fs"); + + let path = fs.base().join("foo"); + let mut dir = fs.dir(&path); + dir.create().expect("create"); + + let_assert!(Ok(None) = fs.path(&path).as_file()); + + Ok(()) + } + #[test] + fn path_is_file_as_file_some() -> TestResult { + let fs = fs::temp().expect("temp fs"); + + let path = fs.base().join("foo"); + let mut file = fs.file(&path); + file.write("contents").expect("create"); + + let_assert!(Ok(None) = fs.path(&path).as_dir()); + + Ok(()) + } +} + mod file { use super::*;