feat(fs): add copy

This commit is contained in:
Paul Campbell 2024-11-03 11:28:00 +00:00
parent a879c6fa15
commit 55a73cbbf1

View 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)
}
}