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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#[cfg(test)]
#[path = "./command_test.rs"]
mod command_test;
use crate::types::error::{ErrorInfo, ScriptError};
use crate::types::instruction::Instruction;
use crate::types::runtime::StateValue;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub enum GoToValue {
Label(String),
Line(usize),
}
#[derive(Debug, Clone)]
pub enum CommandResult {
Continue(Option<String>),
GoTo(Option<String>, GoToValue),
Error(String),
Crash(String),
Exit(Option<String>),
}
pub trait Command {
fn name(&self) -> String;
fn aliases(&self) -> Vec<String> {
vec![]
}
fn help(&self) -> String {
format!("No documentation found for command: {}", self.name())
}
fn clone_and_box(&self) -> Box<dyn Command>;
fn requires_context(&self) -> bool {
false
}
fn run(&self, _arguments: Vec<String>) -> CommandResult {
CommandResult::Crash(format!("Not implemented for command: {}", &self.name()).to_string())
}
fn run_with_context(
&self,
_arguments: Vec<String>,
_state: &mut HashMap<String, StateValue>,
_variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
_instructions: &Vec<Instruction>,
_commands: &mut Commands,
_line: usize,
) -> CommandResult {
CommandResult::Crash(format!("Not implemented for command: {}", &self.name()).to_string())
}
}
pub type CommandBox = Box<dyn Command>;
impl Clone for Box<dyn Command> {
fn clone(&self) -> Box<dyn Command> {
self.clone_and_box()
}
}
#[derive(Clone)]
pub struct Commands {
pub commands: HashMap<String, CommandBox>,
pub aliases: HashMap<String, String>,
}
impl Commands {
pub fn new() -> Commands {
Commands {
commands: HashMap::new(),
aliases: HashMap::new(),
}
}
pub fn set(&mut self, command: CommandBox) -> Result<(), ScriptError> {
let name = command.name();
let aliases = command.aliases();
if self.commands.contains_key(&name) {
return Err(ScriptError {
info: ErrorInfo::Initialization(format!("Command: {} already defined.", &name)),
});
}
for alias in &aliases {
if self.aliases.contains_key(alias) {
return Err(ScriptError {
info: ErrorInfo::Initialization(format!(
"Alias: {} for command: {} already defined.",
&alias, &name
)),
});
}
}
self.commands.insert(name.clone(), command);
self.aliases.remove(&name);
for alias in &aliases {
self.aliases.insert(alias.to_string(), name.clone());
}
Ok(())
}
pub fn get(&self, name: &str) -> Option<&CommandBox> {
let command_name = match self.aliases.get(name) {
Some(ref value) => value,
None => name,
};
match self.commands.get(command_name) {
Some(ref value) => Some(value),
None => None,
}
}
pub fn exists(&self, name: &str) -> bool {
let command = self.get(name);
command.is_some()
}
pub fn get_for_use(&mut self, name: &str) -> Option<CommandBox> {
let command_name = match self.aliases.get(name) {
Some(ref value) => value,
None => name,
};
match self.commands.get(command_name) {
Some(value) => Some(value.clone()),
None => None,
}
}
pub fn get_all_command_names(&self) -> Vec<String> {
let mut names = vec![];
for key in self.commands.keys() {
names.push(key.to_string());
}
names.sort();
names
}
pub fn remove(&mut self, name: &str) -> bool {
let command_name = match self.aliases.get(name) {
Some(ref value) => value,
None => name,
};
match self.commands.remove(command_name) {
Some(command) => {
let aliases = command.aliases();
for alias in &aliases {
self.aliases.remove(alias);
}
true
}
None => false,
}
}
}