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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
pub mod arg;
pub mod call;
pub mod invocation;
pub mod message;
use crate::runtime::rtree::rnode::Name;
use crate::runtime::RuntimeError;
use crate::tree::parser::ast::invocation::Invocation;
use crate::tree::project::{AliasName, TreeName};
use crate::tree::TreeError;
use arg::ArgumentsType::{Named, Unnamed};
use arg::{Arguments, Params};
use call::{Call, Calls};
use itertools::Itertools;
use message::Message;
use parsit::step::Step;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{format, write, Display, Formatter, Write};
use std::str::FromStr;
use strum::ParseError;
use strum_macros::Display;
use strum_macros::EnumString;

pub type Key = String;

#[derive(Display, Debug, Clone, Copy, Eq, PartialEq, EnumString, Deserialize, Serialize)]
#[strum(serialize_all = "snake_case")]
pub enum TreeType {
    Root,
    Parallel,
    Sequence,
    MSequence,
    RSequence,
    Fallback,
    RFallback,
    // decorators
    Inverter,
    ForceSuccess,
    ForceFail,
    Repeat,
    Retry,
    Timeout,
    Delay,
    // actions
    Impl,
    Cond,
}

impl TreeType {
    pub fn is_decorator(&self) -> bool {
        match self {
            TreeType::Inverter
            | TreeType::ForceSuccess
            | TreeType::ForceFail
            | TreeType::Repeat
            | TreeType::Retry
            | TreeType::Delay
            | TreeType::Timeout => true,
            _ => false,
        }
    }
    pub fn is_action(&self) -> bool {
        match self {
            TreeType::Impl | TreeType::Cond => true,
            _ => false,
        }
    }
}

pub fn validate_lambda<'a, 'b>(
    tpe: &'a TreeType,
    args: &'a Arguments,
    calls: &'a Calls,
) -> Result<(), &'b str> {
    match tpe {
        TreeType::Impl | TreeType::Cond => {
            Err("the types impl or cond should have declaration and get called by name")
        }

        _ if tpe.is_decorator() => {
            if calls.elems.len() != 1 {
                Err("any decorator should have only one child")
            } else {
                Ok(())
            }
        }

        _ => {
            if args.args.is_empty() {
                Ok(())
            } else {
                Err("any lambda invocation should not have arguments")
            }
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Tree {
    pub tpe: TreeType,
    pub name: Key,
    pub params: Params,
    pub calls: Calls,
}

impl Tree {
    pub fn root(name: &str, calls: Vec<Call>) -> Self {
        Tree::new(
            TreeType::Root,
            name.to_string(),
            Params::default(),
            Calls::new(calls),
        )
    }
}

impl Tree {
    pub fn is_root(&self) -> bool {
        match self.tpe {
            TreeType::Root => true,
            _ => false,
        }
    }
    pub fn new(tpe: TreeType, name: Key, params: Params, calls: Calls) -> Self {
        Self {
            tpe,
            name,
            params,
            calls,
        }
    }
    pub fn to_inv(&self) -> Invocation {
        self.into()
    }
    pub fn to_inv_args(&self, args: Arguments) -> Invocation {
        Invocation::new(self, args)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Import(pub String, pub Vec<ImportName>);

#[derive(Clone, Debug, PartialEq, Hash, Eq)]
pub enum ImportName {
    Id(String),
    Alias(TreeName, AliasName),
    WholeFile,
}

impl ImportName {
    pub fn id(v: &str) -> Self {
        ImportName::Id(v.to_string())
    }
    pub fn alias(v: &str, alias: &str) -> Self {
        ImportName::Alias(v.to_string(), alias.to_string())
    }
}

impl Import {
    pub fn f_name(&self) -> &str {
        match self {
            Import(n, _) => n,
        }
    }
    pub fn file(f: &str) -> Self {
        Import(f.to_string(), vec![ImportName::WholeFile])
    }
    pub fn names(f: &str, names: Vec<&str>) -> Self {
        Import(
            f.to_string(),
            names.into_iter().map(|v| ImportName::id(v)).collect(),
        )
    }
    pub fn names_mixed(f: &str, names: Vec<ImportName>) -> Self {
        Import(f.to_string(), names)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum FileEntity {
    Tree(Tree),
    Import(Import),
}

#[derive(Clone, Debug, PartialEq)]
pub struct AstFile(pub Vec<FileEntity>);

impl<'a> AstFile {
    pub fn new(field0: Vec<FileEntity>) -> Self {
        Self(field0)
    }
}