Compare commits
No commits in common. "f2315636da4fde73ccb0d37e7f7fd9f84cbc8b62" and "3be53a969dd73684c5bce69a5bbe02ce2b73e17b" have entirely different histories.
f2315636da
...
3be53a969d
5 changed files with 6 additions and 83 deletions
|
@ -5,12 +5,12 @@ Provides injectable Filesystem and Network resources to make code more testable.
|
|||
#### std::fs alternatives
|
||||
|
||||
- [ ] `std::fs::canonicalize` - `path(path).canonicalize()` - Returns the canonical, absolute form of a path with all intermediate components normalized and symbolic links resolved.
|
||||
- [x] `std::fs::copy` - `file(path).copy(target)` - Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
|
||||
- [ ] `std::fs::copy` - `file(path).copy(target)` - Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
|
||||
- [x] `std::fs::create_dir` - `dir(path).create()` - Creates a new, empty directory at the provided path
|
||||
- [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::metadata` - `path(path).metadata()` - Given a path, query the file system to get information about a file, directory, etc.
|
||||
- [x] `std::fs::read` - `file(path).reader().bytes()` - Read the entire contents of a file into a bytes vector.
|
||||
- [ ] `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.
|
||||
- [ ] `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.
|
||||
|
|
|
@ -3,10 +3,10 @@ use crate::fs::{DirItem, DirItemIterator, Result};
|
|||
|
||||
use super::{
|
||||
path::{DirMarker, PathReal},
|
||||
DirHandle, Error, FileMarker, PathMarker,
|
||||
Error, FileMarker, PathMarker,
|
||||
};
|
||||
|
||||
impl<'base, 'path> DirHandle<'base, 'path> {
|
||||
impl<'base, 'path> PathReal<'base, 'path, DirMarker> {
|
||||
/// Creates a new, empty directory at the path
|
||||
///
|
||||
/// Wrapper for [std::fs::create_dir]
|
||||
|
|
|
@ -4,10 +4,10 @@ use crate::fs::Result;
|
|||
use super::{
|
||||
path::{FileMarker, PathReal},
|
||||
reader::Reader,
|
||||
Error, FileHandle,
|
||||
Error,
|
||||
};
|
||||
|
||||
impl<'base, 'path> FileHandle<'base, 'path> {
|
||||
impl<'base, 'path> PathReal<'base, 'path, FileMarker> {
|
||||
/// Returns a [Reader] for the file.
|
||||
///
|
||||
/// ```
|
||||
|
@ -41,25 +41,4 @@ impl<'base, 'path> FileHandle<'base, 'path> {
|
|||
self.check_error()?;
|
||||
std::fs::write(self.as_pathbuf(), contents).map_err(Error::Io)
|
||||
}
|
||||
|
||||
/// Copies the contents of a file to another file.
|
||||
///
|
||||
/// Wrapper for [std::fs::copy]
|
||||
///
|
||||
/// ```
|
||||
/// # fn try_main() -> kxio::fs::Result<()> {
|
||||
/// let fs = kxio::fs::temp()?;
|
||||
/// let src_path = fs.base().join("foo");
|
||||
/// let dest_path = fs.base().join("bar");
|
||||
/// let src = fs.file(&src_path);
|
||||
/// # fs.file(&dest_path).write("new file contents")?;
|
||||
/// let dest = fs.file(&dest_path);
|
||||
/// src.copy(&dest)?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn copy(&self, dest: &PathReal<'base, 'path, FileMarker>) -> Result<u64> {
|
||||
self.check_error()?;
|
||||
std::fs::copy(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,23 +65,6 @@ impl Reader {
|
|||
pub fn lines(&self) -> 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 {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
|
|
39
tests/fs.rs
39
tests/fs.rs
|
@ -385,42 +385,3 @@ mod path_is_file {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
mod copy {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn should_copy_a_file() -> TestResult {
|
||||
let fs = fs::temp().expect("temp fs");
|
||||
let src_path = fs.base().join("foo");
|
||||
let src = fs.file(&src_path);
|
||||
src.write("bar").expect("write");
|
||||
let dst_path = fs.base().join("bar");
|
||||
let dst = fs.file(&dst_path);
|
||||
src.copy(&dst).expect("copy");
|
||||
let src_contents = src.reader().expect("reader").to_string();
|
||||
let dst_contents = dst.reader().expect("reader").to_string();
|
||||
assert_eq!(src_contents, dst_contents);
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue