mod alias; mod cd; mod exit; mod export; mod source; use crate::compose::Environment; use crate::poem::Verse; /// A static list of all the built-in commands static INDEX: [&str; 7] = [ "alias", "cd", "exit", "export", "source", "unalias", "unset", ]; /// Lookup the index of a built-in command /// /// Looks up the index of a built-in command in [INDEX], accounting for aliases. /// /// # Aliases /// * quit -> exit /// * set -> export pub fn lookup(verb: &str) -> Option { let verb = match verb { "quit" => "exit", // Alias 'quit' to 'exit' "set" => "export", // Alias 'set' to 'export' _ => verb, }; INDEX.iter().position(|v| v.to_string() == verb) } /// Run a builtin command, based on its verb /// /// Use [lookup] to check if a verb is in the anthology's index (i.e. is a /// builtin), then call this with the current verse, the index found by /// [lookup], and the shell's global environment state. /// /// # Example /// ``` /// let index = anthology::lookup(verse.verb()); /// if index.is_some() { /// anthology::incant(&verse, index.unwrap(), env); /// } else { /// // Run an external command /// ... /// } /// ``` pub fn incant(verse: &Verse, out: &mut Vec, index: usize, env: &mut Environment) -> i32 { let verb = INDEX[index]; match verb { "alias" => alias::incant(verse, out, &mut env.aliases), "cd" => cd::incant(verse), "exit" => exit::incant(), "export" => export::incant(verse), "source" => source::incant(verse, out, env), "unalias" => alias::unincant(verse, &mut env.aliases), "unset" => export::unincant(verse), _ => unreachable!(), } }