summaryrefslogtreecommitdiffstats
path: root/src/poem/anthology.rs
blob: 9a0d2e45e8e3408cd5559861179dc41f6f0a6bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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.
pub fn lookup(verb: &str) -> Option<usize> {
    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, index: usize, env: &mut Environment) -> i32 {
    let verb = INDEX[index];
    match verb {
        "alias" => alias::incant(verse, &mut env.aliases),
        "cd" => cd::incant(verse),
        "exit" => exit::incant(),
        "export" => export::incant(verse),
        "source" => source::incant(verse, env),
        "unalias" => alias::unincant(verse, &mut env.aliases),
        "unset" => export::unincant(verse),
        _ => unreachable!(),
    }
}