summaryrefslogtreecommitdiffstats
path: root/src/poem/anthology.rs
blob: 2a2f62a2f16c459085c833f97bb993c565d18c95 (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
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)
}

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