summaryrefslogtreecommitdiffstats
path: root/src/poem/anthology.rs
blob: 7d97ab700110c9bfb3d1fedf8f5b130290022b69 (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
pub mod cd;
pub mod exit;
pub mod export;
pub mod source;
use crate::compose::Environment;
use crate::poem::Verse;

/// A static list of all the built-in commands
static INDEX: [&str; 4] = ["cd", "exit", "export", "source"];

/// 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 {
        "cd" => cd::incant(verse),
        "exit" => exit::incant(),
        "export" => export::incant(verse),
        "source" => source::incant(verse, env),
        _ => unreachable!(),
    }
}