diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2023-12-19 16:23:29 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2023-12-19 16:23:29 +1300 |
commit | 47b25c05a6be51b93c909d38a19440d1c04ba2f8 (patch) | |
tree | 448c23c6d58a8407de45a4730cf972bd83713aa0 /src/operations | |
parent | 8f410d1ead74b979481f1488a4dcddd33ea829c7 (diff) | |
download | vagabond-47b25c05a6be51b93c909d38a19440d1c04ba2f8.zip |
Large collection of changesv1.0.0
Diffstat (limited to 'src/operations')
-rw-r--r-- | src/operations/cp.rs | 65 | ||||
-rw-r--r-- | src/operations/ls.rs | 41 | ||||
-rw-r--r-- | src/operations/mkdir.rs | 21 | ||||
-rw-r--r-- | src/operations/rm.rs | 30 |
4 files changed, 68 insertions, 89 deletions
diff --git a/src/operations/cp.rs b/src/operations/cp.rs index be43826..27be3ce 100644 --- a/src/operations/cp.rs +++ b/src/operations/cp.rs @@ -1,62 +1,55 @@ -use crate::make_parent_directory; -use crate::{get_entry, get_optional_entry, list_directory}; -use crate::{EntryType, EntryWriteError}; -use std::path::Path; +use crate::*; -pub fn copy<P, Q>(source_path: P, target_path: Q) -> Result<(), EntryWriteError> -where - P: AsRef<Path>, - Q: AsRef<Path>, -{ +#[must_use] +pub fn copy(source_path: impl AsRef<Path>, destination_path: impl AsRef<Path>) -> WriteResult<()> { let source = get_entry(&source_path)?; - let target = get_optional_entry(&target_path)?; - let target_type = target.and_then(|e| Some(e.entry_type)); + let destination = get_optional_entry(&destination_path)?; + let destination_type = destination.and_then(|e| Some(e.entry_type)); - match (source.entry_type, target_type) { + match (source.entry_type, destination_type) { (EntryType::File, Some(EntryType::File)) => { - copy_file(source_path, target_path)?; + copy_file(source_path, destination_path)?; } (EntryType::File, Some(EntryType::Directory)) => { - let target_path = target_path.as_ref().join(source.name); - copy_file(source_path, target_path)?; + let destination_path = destination_path.as_ref().join(source.name); + copy_file(source_path, destination_path)?; } (EntryType::File, None) => { - make_parent_directory(&target_path)?; - copy_file(source_path, target_path)?; + make_parent_directory(&destination_path)?; + copy_file(&source_path, &destination_path)?; } (EntryType::Directory, Some(EntryType::File)) => { - std::fs::remove_file(&target_path)?; - copy_directory(&source_path, &target_path)?; + // The file is replaced by a copy of the source directory + remove_file(&destination_path)?; + copy_directory(&source_path, &destination_path)?; } (EntryType::Directory, Some(EntryType::Directory)) => { - let target_path = target_path.as_ref().join(source.name); - copy_directory(source_path, target_path)?; + // The destination is filled with the contents of the source directory + for entry in list_directory(source_path)? { + let destination_path = destination_path.as_ref().join(&entry.name); + copy_file(entry.path, destination_path)?; + } } (EntryType::Directory, None) => { - make_parent_directory(&target_path)?; - copy_directory(source_path, target_path)?; + // The destination is created as a copy of the source directory + copy_directory(&source_path, &destination_path)?; } } Ok(()) } -fn copy_file<P, Q>(source_path: P, target_path: Q) -> Result<(), EntryWriteError> -where - P: AsRef<Path>, - Q: AsRef<Path>, -{ - std::fs::copy(source_path, target_path)?; +#[must_use] +fn copy_file(source_path: impl AsRef<Path>, destination_path: impl AsRef<Path>) -> WriteResult<()> { + let copy_result = std::fs::copy(&source_path, &destination_path); + io_result_to_write_result(copy_result, &destination_path.as_ref())?; Ok(()) } -fn copy_directory<P, Q>(source_path: P, target_path: Q) -> Result<(), EntryWriteError> -where - P: AsRef<Path>, - Q: AsRef<Path>, -{ +#[must_use] +fn copy_directory(source_path: impl AsRef<Path>, destination_path: impl AsRef<Path>) -> WriteResult<()> { for entry in list_directory(&source_path)? { - let target_path = target_path.as_ref().join(entry.name); - copy(entry.path, &target_path)?; + let destination_path = destination_path.as_ref().join(entry.name); + copy(entry.path, &destination_path)?; } Ok(()) } diff --git a/src/operations/ls.rs b/src/operations/ls.rs index 2d2da5d..9dd3258 100644 --- a/src/operations/ls.rs +++ b/src/operations/ls.rs @@ -1,31 +1,29 @@ -use crate::{Entry, EntryReadError, EntryType}; -use std::path::Path; +use crate::*; -pub fn get_entry<P>(path: P) -> Result<Entry, EntryReadError> -where - P: AsRef<Path>, -{ +/// Convert a file path into an [Entry]. +pub fn get_entry(path: impl AsRef<Path>) -> ReadResult<Entry> { Entry::from_path(path) } -pub fn get_optional_entry<P>(path: P) -> Result<Option<Entry>, EntryReadError> -where - P: AsRef<Path>, -{ +/// Convert a file path that might not be valid into an [Entry]. This will return +/// as [Option::None] instead of [EntryReadError::NotFound] if the file doesn't exist. +pub fn get_optional_entry(path: impl AsRef<Path>) -> ReadResult<Option<Entry>> { match get_entry(path) { Ok(e) => Ok(Some(e)), - Err(EntryReadError::NotFound) => Ok(None), + Err(EntryReadError { error_kind: EntryErrorKind::NotFound, .. }) => Ok(None), Err(other) => Err(other), } } -pub fn list_directory<P>(path: P) -> Result<Vec<Entry>, EntryReadError> -where - P: AsRef<Path>, -{ +/// Get an [Entry] for every file and subdirectory within a directory. +pub fn list_directory(path: impl AsRef<Path>) -> ReadResult<Vec<Entry>>{ + let path = path.as_ref(); + macro_rules! raise { + ($err:expr) => {io_result_to_read_result($err, path)?}; } + let mut entries = Vec::new(); - for dir_entry in std::fs::read_dir(path)? { - let entry = match Entry::from_path(&dir_entry?.path()) { + for dir_entry in raise!(std::fs::read_dir(path)) { + let entry = match Entry::from_path(&raise!(dir_entry).path()) { Ok(v) => v, Err(_) => continue, }; @@ -34,12 +32,9 @@ where return Ok(entries); } -/// Recursively descend into a directory and all sub-directories, -/// returning an [`Entry`](struct.Entry.html) for each discovered file. -pub fn traverse_directory<P>(path: P) -> Result<Vec<Entry>, EntryReadError> -where - P: AsRef<Path>, -{ +/// Recursively descend into a directory and all sub-directories, returning an +/// [Entry] for each discovered file. +pub fn traverse_directory(path: impl AsRef<Path>) -> ReadResult<Vec<Entry>> { let mut file_entries = Vec::new(); for entry in list_directory(path)? { match entry.entry_type { diff --git a/src/operations/mkdir.rs b/src/operations/mkdir.rs index 011b8cf..9b2abd2 100644 --- a/src/operations/mkdir.rs +++ b/src/operations/mkdir.rs @@ -1,18 +1,15 @@ -use crate::EntryWriteError; -use std::path::Path; +use crate::*; -pub fn make_directory<P>(path: P) -> Result<(), EntryWriteError> -where - P: AsRef<Path>, -{ - std::fs::DirBuilder::new().recursive(true).create(path)?; - Ok(()) +/// Create a new directory and all parent directories. +#[must_use] +pub fn make_directory(path: impl AsRef<Path>) -> WriteResult<()> { + let make_result = std::fs::DirBuilder::new().recursive(true).create(&path); + Ok(io_result_to_write_result(make_result, &path.as_ref())?) } -pub fn make_parent_directory<P>(path: P) -> Result<(), EntryWriteError> -where - P: AsRef<Path>, -{ +/// Create the parent directory of a path. +#[must_use] +pub fn make_parent_directory(path: impl AsRef<Path>) -> WriteResult<()> { match path.as_ref().parent() { Some(parent) => make_directory(parent), None => Ok(()), diff --git a/src/operations/rm.rs b/src/operations/rm.rs index 846a094..bf206a5 100644 --- a/src/operations/rm.rs +++ b/src/operations/rm.rs @@ -1,23 +1,17 @@ -use crate::EntryWriteError; -use crate::{get_entry, EntryType}; -use std::path::Path; +use crate::*; -pub fn remove<P>(path: P) -> Result<(), EntryWriteError> -where - P: AsRef<Path>, -{ +#[must_use] +pub fn remove(path: impl AsRef<Path>) -> WriteResult<()> { let entry = get_entry(&path)?; - match entry.entry_type { - EntryType::File => std::fs::remove_file(&path)?, - EntryType::Directory => std::fs::remove_dir_all(&path)?, - } - Ok(()) + let remove_result = match entry.entry_type { + EntryType::File => std::fs::remove_file(&path), + EntryType::Directory => std::fs::remove_dir_all(&path), + }; + Ok(io_result_to_write_result(remove_result, path.as_ref())?) } -pub fn remove_file<P>(path: P) -> Result<(), EntryWriteError> -where - P: AsRef<Path>, -{ - std::fs::remove_file(path)?; - Ok(()) +#[must_use] +pub fn remove_file(path: impl AsRef<Path>) -> WriteResult<()> { + let remove_result = std::fs::remove_file(&path); + Ok(io_result_to_write_result(remove_result, &path.as_ref())?) } |