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
57
58
59
60
61
use crate::tree::parser::ast::arg::Arguments;
use crate::tree::parser::ast::{Key, TreeType};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum Call {
    Invocation(Key, Arguments),
    HoInvocation(Key),
    Lambda(TreeType, Calls),
    Decorator(TreeType, Arguments, Box<Call>),
}

impl Call {
    pub fn get_ho_invocation(&self) -> Option<Key> {
        match self {
            Call::HoInvocation(k) => Some(k.clone()),
            _ => None,
        }
    }

    pub fn key(&self) -> Option<Key> {
        match self {
            Call::Invocation(k, _) => Some(k.clone()),
            Call::HoInvocation(k) => Some(k.clone()),
            Call::Lambda(_, _) => None,
            Call::Decorator(_, _, _) => None,
        }
    }
    pub fn arguments(&self) -> Arguments {
        match self {
            Call::Invocation(_, args) => args.clone(),
            Call::HoInvocation(_) => Arguments::default(),
            Call::Lambda(_, _) => Arguments::default(),
            Call::Decorator(_, args, _) => args.clone(),
        }
    }

    pub fn invocation(id: &str, args: Arguments) -> Self {
        Call::Invocation(id.to_string(), args)
    }
    pub fn ho_invocation(id: &str) -> Self {
        Call::HoInvocation(id.to_string())
    }
    pub fn lambda(tpe: TreeType, calls: Calls) -> Self {
        Call::Lambda(tpe, calls)
    }
    pub fn decorator(tpe: TreeType, args: Arguments, call: Call) -> Self {
        Call::Decorator(tpe, args, Box::new(call))
    }
}

#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct Calls {
    pub elems: Vec<Call>,
}

impl Calls {
    pub fn new(elems: Vec<Call>) -> Self {
        Calls { elems }
    }
}