summaryrefslogtreecommitdiffstats
path: root/src/poem/anthology.rs
blob: 94b5f6ba25806ff6c2de088a5e9fb20d443ff8f5 (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
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; 6] = ["alias", "cd", "exit", "export", "source", "unalias"];

/// 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)
}

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),
        _ => unreachable!(),
    }
}