From 536e250653e5c140a6d9e60f1cd652b79135e7a6 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Thu, 22 Feb 2024 23:12:24 -0700 Subject: Reorganization and comments Broke out the structs for a poem into their own file: src/recite.rs. Also put the 'prefresh' function into it's own file: src/recite/path.rs. Commented most of the parser code (including structs and helper methods related to parsing (i.e. Verse, Stanza, Meter, Poem)). Renamed any instance of the 'paths' variable to 'path'. --- src/recite/path.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/recite/path.rs (limited to 'src/recite') diff --git a/src/recite/path.rs b/src/recite/path.rs new file mode 100644 index 0000000..28eb45b --- /dev/null +++ b/src/recite/path.rs @@ -0,0 +1,42 @@ +use std::fs; +use std::path::Path; + +/// Refresh the shell's $PATH +/// +/// This function caches all valid paths within within the directories +/// specified. +/// +/// # Arguments +/// * `paths` - A reference to a vector that holds a list to the shell $PATHs +/// +/// # Returns +/// * `bins: Vec` - A new cache of all valid file paths in $PATH +/// +/// # Examples +/// ``` +/// let path = vec!["/bin"]; +/// let path = path.into_iter().map(Path::new).collect(); +/// let mut bins = prefresh(&path); +/// ... +/// // A situation occurs where the $PATH needs to be refreshed +/// bins = prefresh(&path) +/// ``` +pub fn prefresh(path: &Vec<&Path>) -> Vec { + let mut bins: Vec = Vec::new(); + + for p in path { + let files = fs::read_dir(p).expect( + format!( + "dwvsh: error: unable to read the contents of {}", + p.display().to_string() + ) + .as_str(), + ); + + for file in files { + bins.push(file.unwrap().path().display().to_string()); + } + } + + bins +} -- cgit v1.2.3