Compare commits

...

2 commits

Author SHA1 Message Date
6f95c04eb6 feat(fs): add lines to reader
All checks were successful
Rust / build (map[name:nightly]) (push) Successful in 2m8s
Rust / build (map[name:stable]) (push) Successful in 3m54s
Release Please / Release-plz (push) Successful in 1m33s
2024-11-01 21:49:22 +00:00
587d60ee3d 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
2024-11-01 21:49:22 +00:00
5 changed files with 110 additions and 3 deletions

View file

@ -1,5 +1,5 @@
// //
use std::path::Path; use std::path::{Path, PathBuf};
use crate::fs::{DirItem, DirItemIterator, Error, Result}; use crate::fs::{DirItem, DirItemIterator, Error, Result};
@ -27,6 +27,10 @@ impl<'base, 'path> DirReal<'base, 'path> {
Self { path } Self { path }
} }
pub fn path(&self) -> PathBuf {
self.path.full_path()
}
pub fn create(&mut self) -> Result<()> { pub fn create(&mut self) -> Result<()> {
self.path.check_error()?; self.path.check_error()?;
std::fs::create_dir(self.path.full_path()).map_err(Into::into) 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; 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> { pub fn reader(&mut self) -> Result<ReaderReal> {
self.path.check_error()?; self.path.check_error()?;
ReaderReal::new(&self.path.full_path()) ReaderReal::new(&self.path.full_path())

View file

@ -3,6 +3,8 @@ use std::path::{Path, PathBuf};
use crate::fs::{Error, Result}; use crate::fs::{Error, Result};
use super::{dir::DirReal, file::FileReal};
#[derive(Debug)] #[derive(Debug)]
pub struct PathReal<'base, 'path> { pub struct PathReal<'base, 'path> {
base: &'base Path, base: &'base Path,
@ -74,6 +76,24 @@ impl<'base, 'path> PathReal<'base, 'path> {
self.check_error()?; self.check_error()?;
Ok(self.full_path().is_file()) 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 { impl From<PathReal<'_, '_>> for PathBuf {
fn from(path: PathReal) -> Self { fn from(path: PathReal) -> Self {

View file

@ -1,5 +1,5 @@
// //
use std::{fmt::Display, path::Path}; use std::{fmt::Display, path::Path, str::Lines};
use crate::fs::Result; use crate::fs::Result;
@ -15,6 +15,10 @@ impl ReaderReal {
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
&self.contents &self.contents
} }
pub fn lines(&self) -> Lines<'_> {
self.contents.lines()
}
} }
impl Display for ReaderReal { impl Display for ReaderReal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

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 { mod file {
use super::*; use super::*;
@ -55,6 +114,22 @@ mod file {
Ok(()) 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 { mod dir_create {