summaryrefslogtreecommitdiff
path: root/src/operations/ls.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2022-08-25 21:27:39 +1200
committerBen Bridle <bridle.benjamin@gmail.com>2022-08-25 21:27:39 +1200
commit8f410d1ead74b979481f1488a4dcddd33ea829c7 (patch)
tree2f22fd930c8d0cdb4de53fef473f7770073e14d5 /src/operations/ls.rs
downloadvagabond-1.0.zip
Initial commitv1.0
Diffstat (limited to 'src/operations/ls.rs')
-rw-r--r--src/operations/ls.rs51
1 files changed, 51 insertions, 0 deletions
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<P>(path: P) -> Result<Entry, EntryReadError>
+where
+ P: AsRef<Path>,
+{
+ Entry::from_path(path)
+}
+
+pub fn get_optional_entry<P>(path: P) -> Result<Option<Entry>, EntryReadError>
+where
+ P: AsRef<Path>,
+{
+ match get_entry(path) {
+ Ok(e) => Ok(Some(e)),
+ Err(EntryReadError::NotFound) => Ok(None),
+ Err(other) => Err(other),
+ }
+}
+
+pub fn list_directory<P>(path: P) -> Result<Vec<Entry>, EntryReadError>
+where
+ P: AsRef<Path>,
+{
+ 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<P>(path: P) -> Result<Vec<Entry>, EntryReadError>
+where
+ P: AsRef<Path>,
+{
+ 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);
+}