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/ls.rs | |
parent | 8f410d1ead74b979481f1488a4dcddd33ea829c7 (diff) | |
download | vagabond-06b4917ac4712c02a7904521875ee01b102d8c2a.zip |
Large collection of changesv1.0.0
Diffstat (limited to 'src/operations/ls.rs')
-rw-r--r-- | src/operations/ls.rs | 41 |
1 files changed, 18 insertions, 23 deletions
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 { |