From 8f410d1ead74b979481f1488a4dcddd33ea829c7 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 25 Aug 2022 21:27:39 +1200 Subject: Initial commit --- src/operations/ls.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/operations/ls.rs (limited to 'src/operations/ls.rs') diff --git a/src/operations/ls.rs b/src/operations/ls.rs new file mode 100644 index 0000000..2d2da5d --- /dev/null +++ b/src/operations/ls.rs @@ -0,0 +1,51 @@ +use crate::{Entry, EntryReadError, EntryType}; +use std::path::Path; + +pub fn get_entry

(path: P) -> Result +where + P: AsRef, +{ + Entry::from_path(path) +} + +pub fn get_optional_entry

(path: P) -> Result, EntryReadError> +where + P: AsRef, +{ + match get_entry(path) { + Ok(e) => Ok(Some(e)), + Err(EntryReadError::NotFound) => Ok(None), + Err(other) => Err(other), + } +} + +pub fn list_directory

(path: P) -> Result, EntryReadError> +where + P: AsRef, +{ + let mut entries = Vec::new(); + for dir_entry in std::fs::read_dir(path)? { + let entry = match Entry::from_path(&dir_entry?.path()) { + Ok(v) => v, + Err(_) => continue, + }; + entries.push(entry); + } + 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, +{ + let mut file_entries = Vec::new(); + for entry in list_directory(path)? { + match entry.entry_type { + EntryType::File => file_entries.push(entry), + EntryType::Directory => file_entries.extend(traverse_directory(&entry.path)?), + } + } + return Ok(file_entries); +} -- cgit v1.2.3-70-g09d2