feat(fs): add .reader().bytes()

This commit is contained in:
Paul Campbell 2024-11-03 11:36:20 +00:00
parent 7573c52755
commit 425d166517
6 changed files with 47 additions and 12 deletions

View file

@ -10,7 +10,7 @@ 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.

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

@ -2,7 +2,7 @@
use crate::fs::{DirItem, DirItemIterator, Result}; use crate::fs::{DirItem, DirItemIterator, Result};
use super::{ use super::{
path::{DirMarker, PathReal}, DirHandle, Error, FileHandle, FileMarker, PathHandle, PathMarker DirHandle, Error, FileHandle, PathHandle,
}; };
impl<'base, 'path> DirHandle<'base, 'path> { impl<'base, 'path> DirHandle<'base, 'path> {
@ -64,9 +64,7 @@ impl<'base, 'path> DirHandle<'base, 'path> {
impl<'base, 'path> TryFrom<PathHandle<'base, 'path>> for FileHandle<'base, 'path> { impl<'base, 'path> TryFrom<PathHandle<'base, 'path>> for FileHandle<'base, 'path> {
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: PathHandle<'base, 'path>,
) -> 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,9 +77,7 @@ impl<'base, 'path> TryFrom<PathHandle<'base, 'path>> for FileHandle<'base, 'path
impl<'base, 'path> TryFrom<PathHandle<'base, 'path>> for DirHandle<'base, 'path> { impl<'base, 'path> TryFrom<PathHandle<'base, 'path>> for DirHandle<'base, 'path> {
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: PathHandle<'base, 'path>,
) -> 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

@ -2,7 +2,6 @@
use crate::fs::Result; use crate::fs::Result;
use super::{ use super::{
path::{FileMarker, PathReal},
reader::Reader, reader::Reader,
Error, FileHandle, Error, FileHandle,
}; };

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

@ -403,3 +403,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(())
}
}
}