From 08d994cd6b4dbf98bae6d2861efbc937d69d8590 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 3 Nov 2024 11:28:00 +0000 Subject: [PATCH] feat(fs): add copy --- README.md | 2 +- src/fs/file.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 52102a5..88b0a5c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/fs/file.rs b/src/fs/file.rs index 5b7c7b4..346b77a 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -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 { + self.check_error()?; + std::fs::copy(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io) + } }