feat(fs): add .copy(dest)
All checks were successful
Rust / build (map[name:stable]) (push) Successful in 2m4s
Rust / build (map[name:nightly]) (push) Successful in 3m45s
Release Please / Release-plz (push) Successful in 1m35s

This commit is contained in:
Paul Campbell 2024-11-03 11:28:00 +00:00
parent b593a8f67e
commit 2f236b752c
3 changed files with 40 additions and 1 deletions

View file

@ -5,7 +5,7 @@ Provides injectable Filesystem and Network resources to make code more testable.
#### std::fs alternatives #### 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. - [ ] `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` - `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. - [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.

View file

@ -41,4 +41,25 @@ impl<'base, 'path> FileHandle<'base, 'path> {
self.check_error()?; self.check_error()?;
std::fs::write(self.as_pathbuf(), contents).map_err(Error::Io) 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)
}
} }

View file

@ -385,3 +385,21 @@ mod path_is_file {
Ok(()) 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(())
}
}