From 55a73cbbf102a4be17ed55bdc8a87f4382f96287 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 --- src/fs/file.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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) + } }