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
55
56
use std::{fmt::Debug, rc::Rc};

use anyhow::{anyhow, Result};

use crate::environment::Environment;
use crate::resolver::ResolverAction;

use super::{ident::IdentData, Value};

pub type CommandRes = Option<Vec<ResolverAction>>;

#[derive(Clone)]
pub struct Command {
    pub name: String,
    // An arity of 0 allows for an arbitrary amount of arguments
    pub arity: usize,
    pub func: Rc<dyn Fn(&Environment, &[Value]) -> Result<CommandRes>>,
}

impl Debug for Command {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(format!("Command<{}>", self.name).as_str())
    }
}

impl Command {
    pub fn run(&self, env: &Environment, args: &Vec<Value>) -> Result<CommandRes> {
        if self.arity>0 && args.len() != self.arity {
            return Err(anyhow!(
                "{} requires {} arguments, got {}",
                self.name,
                self.arity,
                args.len()
            ));
        }
        (self.func)(env, args)
    }
}

#[derive(Debug)]
pub struct CommandCall {
    pub command: String,
    pub args: Vec<Value>,
}

impl CommandCall {
    pub fn run(&self, env: &Environment) -> Result<CommandRes> {
        let r = env.get(&self.command);
        let res = r.ok_or_else(|| anyhow!("{} is not defined", self.command))?;
        if let IdentData::Command(cmd) = res {
            cmd.run(env, &self.args)
        } else {
            Err(anyhow!("{} is not a command", self.command))
        }
    }
}