forester_rs/tree/parser/ast/
call.rs

1use crate::tree::parser::ast::arg::Arguments;
2use crate::tree::parser::ast::{Key, TreeType};
3use serde::{Deserialize, Serialize};
4use std::fmt::{Debug, Formatter};
5
6/// A call to a tree
7#[derive(Clone, PartialEq, Deserialize, Serialize)]
8pub enum Call {
9    /// An invocation of a tree like 'root main { invocation()}'
10    Invocation(Key, Arguments),
11    /// An Higher order invocation of a tree like 'root main { ho-invocation(..)}'
12    HoInvocation(Key),
13    /// A lambda call like 'root main { sequence {...} }'
14    Lambda(TreeType, Calls),
15    /// A decorator call like 'root main { decorator(..) child() }'
16    Decorator(TreeType, Arguments, Box<Call>),
17}
18
19impl Debug for Call {
20    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21        match self {
22            Call::Invocation(id, args) => write!(f, "{}({})", id, args),
23            Call::HoInvocation(id) => write!(f, "{}(..)", id),
24            Call::Lambda(tpe, calls) => {
25                let _ = write!(f, "{} :", tpe);
26                let mut elems = f.debug_list();
27                for call in calls.elems.iter() {
28                    elems.entry(call);
29                }
30                let _ = elems.finish();
31                Ok(())
32            }
33            Call::Decorator(tpe, args, call) => {
34                let _ = write!(f, "{}({}) :", tpe, args);
35                let mut elems = f.debug_list();
36                elems.entry(call);
37                let _ = elems.finish();
38                Ok(())
39            }
40        }
41    }
42}
43
44impl Call {
45    pub fn is_lambda(&self) -> bool {
46        matches!(self, Call::Lambda(_, _))
47    }
48    pub fn is_decorator(&self) -> bool {
49        matches!(self, Call::Decorator(_,_, _))
50    }
51
52    pub fn get_ho_invocation(&self) -> Option<Key> {
53        match self {
54            Call::HoInvocation(k) => Some(k.clone()),
55            _ => None,
56        }
57    }
58
59    pub fn key(&self) -> Option<Key> {
60        match self {
61            Call::Invocation(k, _) => Some(k.clone()),
62            Call::HoInvocation(k) => Some(k.clone()),
63            Call::Lambda(_, _) => None,
64            Call::Decorator(_, _, _) => None,
65        }
66    }
67    pub fn arguments(&self) -> Arguments {
68        match self {
69            Call::Invocation(_, args) => args.clone(),
70            Call::HoInvocation(_) => Arguments::default(),
71            Call::Lambda(_, _) => Arguments::default(),
72            Call::Decorator(_, args, _) => args.clone(),
73        }
74    }
75
76    pub fn invocation(id: &str, args: Arguments) -> Self {
77        Call::Invocation(id.to_string(), args)
78    }
79    pub fn ho_invocation(id: &str) -> Self {
80        Call::HoInvocation(id.to_string())
81    }
82    pub fn lambda(tpe: TreeType, calls: Calls) -> Self {
83        Call::Lambda(tpe, calls)
84    }
85    pub fn decorator(tpe: TreeType, args: Arguments, call: Call) -> Self {
86        Call::Decorator(tpe, args, Box::new(call))
87    }
88}
89
90#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
91pub struct Calls {
92    pub elems: Vec<Call>,
93}
94
95impl Calls {
96    pub fn new(elems: Vec<Call>) -> Self {
97        Calls { elems }
98    }
99}