Compare commits

...

3 commits

Author SHA1 Message Date
ba3a388705 feat(fs): add .dir(path).remove()
All checks were successful
Rust / build (map[name:nightly]) (push) Successful in 1m50s
Rust / build (map[name:stable]) (push) Successful in 4m25s
Release Please / Release-plz (push) Successful in 1m42s
2024-11-03 14:05:45 +00:00
16e57b8ca9 feat(fs): add .reader().bytes()
All checks were successful
Rust / build (map[name:stable]) (push) Successful in 2m4s
Rust / build (map[name:nightly]) (push) Successful in 4m13s
Release Please / Release-plz (push) Successful in 1m27s
2024-11-03 14:03:16 +00:00
6aeb4521fc refactor(fs): use type aliases
All checks were successful
Rust / build (map[name:nightly]) (push) Successful in 2m2s
Rust / build (map[name:stable]) (push) Successful in 3m54s
Release Please / Release-plz (push) Successful in 41s
2024-11-03 14:01:53 +00:00
7 changed files with 88 additions and 30 deletions

View file

@ -10,11 +10,11 @@ Provides injectable Filesystem and Network resources to make code more testable.
- [x] `std::fs::create_dir_all` - `dir(path).create_all()` - Recursively create a directory and all of its parent components if they are missing. - [x] `std::fs::create_dir_all` - `dir(path).create_all()` - Recursively create a directory and all of its parent components if they are missing.
- [ ] `std::fs::hard_link` - `link(path)create()` - Creates a new hard link on the filesystem. - [ ] `std::fs::hard_link` - `link(path)create()` - Creates a new hard link on the filesystem.
- [ ] `std::fs::metadata` - `path(path).metadata()` - Given a path, query the file system to get information about a file, directory, etc. - [ ] `std::fs::metadata` - `path(path).metadata()` - Given a path, query the file system to get information about a file, directory, etc.
- [ ] `std::fs::read` - `file(path).reader().bytes()` - Read the entire contents of a file into a bytes vector. - [x] `std::fs::read` - `file(path).reader().bytes()` - Read the entire contents of a file into a bytes vector.
- [x] `std::fs::read_dir` - `dir(path).read()` - Returns an iterator over the entries within a directory. - [x] `std::fs::read_dir` - `dir(path).read()` - Returns an iterator over the entries within a directory.
- [ ] `std::fs::read_link` - `link(path).read()` - Reads a symbolic link, returning the file that the link points to. - [ ] `std::fs::read_link` - `link(path).read()` - Reads a symbolic link, returning the file that the link points to.
- [x] `std::fs::read_to_string` - `file(path).reader().to_string()` - Read the entire contents of a file into a string. - [x] `std::fs::read_to_string` - `file(path).reader().to_string()` - Read the entire contents of a file into a string.
- [ ] `std::fs::remove_dir` - `dir(path).remove()` - Removes an empty directory. - [x] `std::fs::remove_dir` - `dir(path).remove()` - Removes an empty directory.
- [ ] `std::fs::remove_dir_all` - `dir(path).remove_all()` - Removes a directory at this path, after removing all its contents. Use carefully! - [ ] `std::fs::remove_dir_all` - `dir(path).remove_all()` - Removes a directory at this path, after removing all its contents. Use carefully!
- [ ] `std::fs::remove_file` - `file(path).remove()` - Removes a file from the filesystem. - [ ] `std::fs::remove_file` - `file(path).remove()` - Removes a file from the filesystem.
- [ ] `std::fs::rename` - `path(path).rename()` - Rename a file or directory to a new name, replacing the original file if to already exists. - [ ] `std::fs::rename` - `path(path).rename()` - Rename a file or directory to a new name, replacing the original file if to already exists.

View file

@ -1,9 +1,11 @@
build: build:
#!/usr/bin/env bash
set -e
cargo fmt cargo fmt
cargo fmt --check cargo fmt --check
cargo hack --feature-powerset clippy cargo hack clippy
cargo hack --feature-powerset build cargo hack build
cargo hack --feature-powerset test cargo hack test
cargo doc cargo doc
install-hooks: install-hooks:

View file

@ -1,10 +1,7 @@
// //
use crate::fs::{DirItem, DirItemIterator, Result}; use crate::fs::{DirItem, DirItemIterator, Result};
use super::{ use super::{DirHandle, Error, FileHandle, PathHandle};
path::{DirMarker, PathReal},
DirHandle, Error, FileMarker, PathMarker,
};
impl<'base, 'path> DirHandle<'base, 'path> { impl<'base, 'path> DirHandle<'base, 'path> {
/// Creates a new, empty directory at the path /// Creates a new, empty directory at the path
@ -61,15 +58,28 @@ impl<'base, 'path> DirHandle<'base, 'path> {
let read_dir = std::fs::read_dir(self.as_pathbuf()).map_err(Error::Io)?; let read_dir = std::fs::read_dir(self.as_pathbuf()).map_err(Error::Io)?;
Ok(Box::new(DirItemIterator::new(read_dir))) Ok(Box::new(DirItemIterator::new(read_dir)))
} }
/// Removes an empty directory.
///
/// Wrapper for [std::fs::remove_dir]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo").join("bar");
/// let dir = fs.dir(&path);
/// dir.remove()?;
/// # Ok(())
/// }
pub fn remove(&self) -> Result<()> {
self.check_error()?;
std::fs::remove_dir(self.as_pathbuf()).map_err(Error::Io)
}
} }
impl<'base, 'path> TryFrom<PathReal<'base, 'path, PathMarker>> impl<'base, 'path> TryFrom<PathHandle<'base, 'path>> for FileHandle<'base, 'path> {
for PathReal<'base, 'path, FileMarker>
{
type Error = crate::fs::Error; type Error = crate::fs::Error;
fn try_from( fn try_from(path: PathHandle<'base, 'path>) -> std::result::Result<Self, Self::Error> {
path: PathReal<'base, 'path, PathMarker>,
) -> std::result::Result<Self, Self::Error> {
match path.as_file() { match path.as_file() {
Ok(Some(dir)) => Ok(dir.clone()), Ok(Some(dir)) => Ok(dir.clone()),
Ok(None) => Err(crate::fs::Error::NotADirectory { Ok(None) => Err(crate::fs::Error::NotADirectory {
@ -79,14 +89,10 @@ impl<'base, 'path> TryFrom<PathReal<'base, 'path, PathMarker>>
} }
} }
} }
impl<'base, 'path> TryFrom<PathReal<'base, 'path, PathMarker>> impl<'base, 'path> TryFrom<PathHandle<'base, 'path>> for DirHandle<'base, 'path> {
for PathReal<'base, 'path, DirMarker>
{
type Error = crate::fs::Error; type Error = crate::fs::Error;
fn try_from( fn try_from(path: PathHandle<'base, 'path>) -> std::result::Result<Self, Self::Error> {
path: PathReal<'base, 'path, PathMarker>,
) -> std::result::Result<Self, Self::Error> {
match path.as_dir() { match path.as_dir() {
Ok(Some(dir)) => Ok(dir.clone()), Ok(Some(dir)) => Ok(dir.clone()),
Ok(None) => Err(crate::fs::Error::NotADirectory { Ok(None) => Err(crate::fs::Error::NotADirectory {

View file

@ -1,11 +1,7 @@
// //
use crate::fs::Result; use crate::fs::Result;
use super::{ use super::{reader::Reader, Error, FileHandle};
path::{FileMarker, PathReal},
reader::Reader,
Error, FileHandle,
};
impl<'base, 'path> FileHandle<'base, 'path> { impl<'base, 'path> FileHandle<'base, 'path> {
/// Returns a [Reader] for the file. /// Returns a [Reader] for the file.
@ -58,7 +54,7 @@ impl<'base, 'path> FileHandle<'base, 'path> {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn copy(&self, dest: &PathReal<'base, 'path, FileMarker>) -> Result<u64> { pub fn copy(&self, dest: &FileHandle<'base, 'path>) -> Result<u64> {
self.check_error()?; self.check_error()?;
std::fs::copy(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io) std::fs::copy(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io)
} }

View file

@ -6,6 +6,8 @@ use std::{
use crate::fs::{Error, Result}; use crate::fs::{Error, Result};
use super::{DirHandle, FileHandle, PathHandle};
/// Marker trait for the type of [PathReal]. /// Marker trait for the type of [PathReal].
pub trait PathType {} pub trait PathType {}
@ -170,7 +172,7 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn as_dir(&self) -> Result<Option<PathReal<'base, 'path, DirMarker>>> { pub fn as_dir(&self) -> Result<Option<DirHandle<'base, 'path>>> {
self.check_error()?; self.check_error()?;
if self.as_pathbuf().is_dir() { if self.as_pathbuf().is_dir() {
Ok(Some(PathReal::new(self.base, self.path))) Ok(Some(PathReal::new(self.base, self.path)))
@ -193,7 +195,7 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn as_file(&self) -> Result<Option<PathReal<'base, 'path, FileMarker>>> { pub fn as_file(&self) -> Result<Option<FileHandle<'base, 'path>>> {
self.check_error()?; self.check_error()?;
if self.as_pathbuf().is_file() { if self.as_pathbuf().is_file() {
Ok(Some(PathReal::new(self.base, self.path))) Ok(Some(PathReal::new(self.base, self.path)))
@ -202,7 +204,7 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
} }
} }
} }
impl From<PathReal<'_, '_, PathMarker>> for PathBuf { impl<'base, 'path> From<PathHandle<'base, 'path>> for PathBuf {
fn from(path: PathReal<PathMarker>) -> Self { fn from(path: PathReal<PathMarker>) -> Self {
path.base.join(path.path) path.base.join(path.path)
} }

View file

@ -65,6 +65,23 @@ impl Reader {
pub fn lines(&self) -> Lines<'_> { pub fn lines(&self) -> Lines<'_> {
self.contents.lines() self.contents.lines()
} }
/// Returns the contents of the file as bytes.
///
/// ```
/// # use kxio::fs::Result;
/// # fn main() -> Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo");
/// let file = fs.file(&path);
/// # file.write("new file contents")?;
/// let bytes = file.reader()?.bytes();
/// # Ok(())
/// # }
/// ```
pub fn bytes(&self) -> &[u8] {
self.contents.as_bytes()
}
} }
impl Display for Reader { impl Display for Reader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

View file

@ -250,6 +250,20 @@ mod dir_dir_read {
Ok(()) Ok(())
} }
} }
mod dir_remove {
use super::*;
#[test]
fn should_remove_a_dir() -> TestResult {
let fs = fs::temp().expect("temp fs");
let path = fs.base().join("foo");
fs.dir(&path).create().expect("create");
fs.dir(&path).remove().expect("remove");
let exists = fs.path(&path).exists().expect("exists");
assert!(!exists);
Ok(())
}
}
mod path_exists { mod path_exists {
use super::*; use super::*;
@ -403,3 +417,24 @@ mod copy {
Ok(()) Ok(())
} }
} }
mod reader {
use super::*;
mod bytes {
use super::*;
#[test]
fn should_return_bytes() -> TestResult {
let fs = fs::temp().expect("temp fs");
let path = fs.base().join("foo");
let file = fs.file(&path);
file.write("bar").expect("write");
let reader = file.reader().expect("reader");
let bytes = reader.bytes();
assert_eq!(bytes.len(), 3);
assert_eq!(bytes[0], b'b');
assert_eq!(bytes[1], b'a');
assert_eq!(bytes[2], b'r');
Ok(())
}
}
}