Compare commits
2 commits
a879c6fa15
...
e629cb2a82
Author | SHA1 | Date | |
---|---|---|---|
e629cb2a82 | |||
08d994cd6b |
3 changed files with 26 additions and 5 deletions
|
@ -5,7 +5,7 @@ Provides injectable Filesystem and Network resources to make code more testable.
|
|||
#### std::fs alternatives
|
||||
|
||||
- [x] `std::fs::canonicalize` - `path(path).canonicalize()` - Returns the canonical, absolute form of a path with all intermediate components normalized and symbolic links resolved.
|
||||
- [ ] `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::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.
|
||||
|
|
|
@ -3,10 +3,10 @@ use crate::fs::{DirItem, DirItemIterator, Result};
|
|||
|
||||
use super::{
|
||||
path::{DirMarker, PathReal},
|
||||
Error, FileMarker, PathMarker,
|
||||
DirHandle, Error, FileMarker, PathMarker,
|
||||
};
|
||||
|
||||
impl<'base, 'path> PathReal<'base, 'path, DirMarker> {
|
||||
impl<'base, 'path> DirHandle<'base, 'path> {
|
||||
/// 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,
|
||||
Error, FileHandle,
|
||||
};
|
||||
|
||||
impl<'base, 'path> PathReal<'base, 'path, FileMarker> {
|
||||
impl<'base, 'path> FileHandle<'base, 'path> {
|
||||
/// Returns a [Reader] for the file.
|
||||
///
|
||||
/// ```
|
||||
|
@ -41,4 +41,25 @@ impl<'base, 'path> PathReal<'base, 'path, FileMarker> {
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue