From 5a7718698373d07a29fffcb792acdb81aa7712d7 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Sat, 23 Mar 2024 02:45:54 -0600 Subject: read() and recite() overhaul Rebuilt the LR parser (i.e. read()) from the ground up. This required that some changes be made to recite(), in order to accomodate the new data structures. These data structures were each split out into their own file, in order to make working with each component a bit easier. In addition to reworking the parts of the parser already present, some new features were also added, such as: - Support for strings (' and ") - Support for environment variables ($) - Support for interpreting tild as $HOME (~) - Support for sub-reading and sub-reciting (`) --- src/path.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/path.rs (limited to 'src/path.rs') diff --git a/src/path.rs b/src/path.rs new file mode 100644 index 0000000..28eb45b --- /dev/null +++ b/src/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