Compare commits
2 commits
db28ab982a
...
6f95c04eb6
Author | SHA1 | Date | |
---|---|---|---|
6f95c04eb6 | |||
587d60ee3d |
5 changed files with 110 additions and 3 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,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 {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
use std::{fmt::Display, path::Path};
|
||||
use std::{fmt::Display, path::Path, str::Lines};
|
||||
|
||||
use crate::fs::Result;
|
||||
|
||||
|
@ -15,6 +15,10 @@ impl ReaderReal {
|
|||
pub fn as_str(&self) -> &str {
|
||||
&self.contents
|
||||
}
|
||||
|
||||
pub fn lines(&self) -> Lines<'_> {
|
||||
self.contents.lines()
|
||||
}
|
||||
}
|
||||
impl Display for ReaderReal {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
|
|
75
tests/fs.rs
75
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::*;
|
||||
|
||||
|
@ -55,6 +114,22 @@ mod file {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_file_lines() -> TestResult {
|
||||
let fs = fs::temp().expect("temp fs");
|
||||
|
||||
let path = fs.base().join("foo");
|
||||
let mut file = fs.file(&path);
|
||||
file.write("line 1\nline 2").expect("write");
|
||||
|
||||
let reader = file.reader().expect("reader");
|
||||
let lines = reader.lines().collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(lines, vec!["line 1", "line 2"]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod dir_create {
|
||||
|
|
Loading…
Reference in a new issue