From fedd4c31b0d1c6d036b1105a74b6e6a1f135f2b4 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Fri, 14 Jun 2024 20:29:32 -0400 Subject: Add back spellcheck This commit reworks spellcheck() so it is more verbose about what it returns. It also re-introduces the use of spellcheck() in Poem::recite(). Spellcheck now returns a value in the enum, Spelling: FullPath -> Indicates a full path was specified as the verb OnPath -> Indicates the verb is on the $PATH BuiltIn -> Indicates the verb is a built-in command None (Option) -> The verb does not exist This commit also removes some defunct (commented-out) code. --- src/poem/elements/verse.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/poem/elements') diff --git a/src/poem/elements/verse.rs b/src/poem/elements/verse.rs index 42d7c6a..b4f41ba 100644 --- a/src/poem/elements/verse.rs +++ b/src/poem/elements/verse.rs @@ -1,6 +1,7 @@ use super::rune::Rune; use super::stanza::Stanza; use super::word::Word; +use crate::poem::anthology; use crate::poem::anthology::Anthology; use crate::poem::Poem; mod logic; @@ -32,6 +33,13 @@ pub struct Verse { pub meter: Rune, } +#[derive(Debug, Clone)] +pub enum Spelling { + FullPath, + OnPath(usize), + BuiltIn(usize), +} + pub trait Forkable { fn fork(&mut self, env: &mut Environment) -> Result; } @@ -216,7 +224,7 @@ impl Verse { /// stanza_success.spellcheck(bins) // -> Some(i) /// stanza_fail.spellcheck(bins) // -> None /// ``` - pub fn spellcheck(&self, bins: &Vec) -> Option { + pub fn spellcheck(&self, bins: &Vec) -> Option { // An empty verb (i.e. the empty string) cannot be a program, so // return false // Thanks to the parsing in Poem::read, however, it's @@ -225,6 +233,13 @@ impl Verse { return None; } + // Check for built-ins + let i = anthology::lookup(self.verb().as_str()); + match i { + Some(i) => return Some(Spelling::BuiltIn(i)), + None => {} + }; + // Only search the $PATH if a full or relative path was not given, or // if the path given does not exist if !Path::new(self.verb().as_str()).exists() { @@ -232,7 +247,7 @@ impl Verse { // Searches in $PATH order for (i, path) in bins.iter().enumerate() { if path.split('/').last().unwrap() == self.verb() { - return Some(i); + return Some(Spelling::OnPath(i)); } } @@ -241,7 +256,7 @@ impl Verse { } // Return the length of bins if the full path or relative path exists - Some(bins.len()) + Some(Spelling::FullPath) } /// Run a command @@ -256,7 +271,7 @@ impl Verse { out: &mut Vec, pids: &mut Arc>>, env: &mut Environment, - anthology: Option, + spell: Option, ) -> Result { // Read files into 'out' if Rune::Read is present in the verse's IO if self.io.contains(&Rune::Read) { @@ -274,12 +289,12 @@ impl Verse { } // Build and run the command - match anthology { - Some(_) => { - let mut command = Anthology::new(self.verb()); + match spell { + Some(Spelling::BuiltIn(i)) => { + let mut command = Anthology::new(i); incant!(self.verb(), command, out, pids, env, self) } - None => { + _ => { let mut command = Command::new(self.verb()); incant!(self.verb(), command, out, pids, env, self) } -- cgit v1.2.3