From 47b25c05a6be51b93c909d38a19440d1c04ba2f8 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 19 Dec 2023 16:23:29 +1300 Subject: Large collection of changes --- src/operations/ls.rs | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) (limited to 'src/operations/ls.rs') 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

(path: P) -> Result -where - P: AsRef, -{ +/// Convert a file path into an [Entry]. +pub fn get_entry(path: impl AsRef) -> ReadResult { Entry::from_path(path) } -pub fn get_optional_entry

(path: P) -> Result, EntryReadError> -where - P: AsRef, -{ +/// 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) -> ReadResult> { 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

(path: P) -> Result, EntryReadError> -where - P: AsRef, -{ +/// Get an [Entry] for every file and subdirectory within a directory. +pub fn list_directory(path: impl AsRef) -> ReadResult>{ + 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

(path: P) -> Result, EntryReadError> -where - P: AsRef, -{ +/// Recursively descend into a directory and all sub-directories, returning an +/// [Entry] for each discovered file. +pub fn traverse_directory(path: impl AsRef) -> ReadResult> { let mut file_entries = Vec::new(); for entry in list_directory(path)? { match entry.entry_type { -- cgit v1.2.3-70-g09d2