feat(fs): add as_dir/as_file to convert from path
All checks were successful
Rust / build (map[name:nightly]) (push) Successful in 2m5s
Rust / build (map[name:stable]) (push) Successful in 3m47s
Release Please / Release-plz (push) Successful in 1m32s

This commit is contained in:
Paul Campbell 2024-11-01 19:20:41 +00:00
parent 09e1d91a9e
commit 587d60ee3d
4 changed files with 89 additions and 2 deletions

View file

@ -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)

View file

@ -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<ReaderReal> {
self.path.check_error()?;
ReaderReal::new(&self.path.full_path())

View file

@ -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<Option<DirReal<'base, 'path>>> {
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<Option<FileReal<'base, 'path>>> {
self.check_error()?;
if self.full_path().is_file() {
Ok(Some(FileReal::new(self.base, self.path)))
} else {
Ok(None)
}
}
}
impl From<PathReal<'_, '_>> for PathBuf {
fn from(path: PathReal) -> Self {

View file

@ -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::*;