Skip to main content

tome/commands/
builtins.rs

1use super::{execute::execute, help::help};
2use std::collections::HashMap;
3
4pub struct Command {
5    pub func: fn(&str, &str, &[String]) -> Result<String, String>,
6    pub help_text: &'static str,
7}
8
9lazy_static! {
10    pub static ref BUILTIN_COMMANDS: HashMap<String, Command> = {
11        let mut m = HashMap::new();
12        m.insert(
13            "commands".to_owned(),
14            Command {
15                func: help_command as fn(&str, &str, &[String]) -> Result<String, String>,
16                help_text: "print all commands",
17            },
18        );
19        m.insert(
20            "exec".to_owned(),
21            Command {
22                func: exec_command as fn(&str, &str, &[String]) -> Result<String, String>,
23                help_text: "execute a command",
24            },
25        );
26        m.insert(
27            "help".to_owned(),
28            Command {
29                func: help_command as fn(&str, &str, &[String]) -> Result<String, String>,
30                help_text: "print help for the command",
31            },
32        );
33        m.insert(
34            "tome".to_owned(),
35            Command {
36                func: noop_command as fn(&str, &str, &[String]) -> Result<String, String>,
37                help_text: "currently a no-op. reserved namespace for future tome commands",
38            },
39        );
40        m
41    };
42}
43
44fn exec_command(root: &str, shell: &str, args: &[String]) -> Result<String, String> {
45    // strip the first argument since it should be "exec"
46    execute(root, shell, &args[1..])
47}
48
49fn help_command(root: &str, _: &str, args: &[String]) -> Result<String, String> {
50    help(root, args.get(1..).unwrap_or(&[]))
51}
52
53fn noop_command(_: &str, _: &str, _: &[String]) -> Result<String, String> {
54    Ok("".to_owned())
55}