feat(fs); add as_dir/as_file to convert from path
This commit is contained in:
parent
3a625f5b5b
commit
2a8e4393ae
4 changed files with 88 additions and 2 deletions
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
|
|||
|
||||
use crate::fs::{Error, Result};
|
||||
|
||||
use super::{dir::DirReal, file::FileReal};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PathReal<'base, 'path> {
|
||||
|
@ -75,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 {
|
||||
|
|
59
tests/fs.rs
59
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::*;
|
||||
|
||||
|
|
Loading…
Reference in a new issue