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
use crate::types::scope::clear;
use crate::types::scope::set_line_context_name;
use crate::utils::eval;
use crate::utils::state::{get_handles_sub_state, put_handle};
use duckscript::parser;
use duckscript::types::command::{Command, CommandResult, Commands};
use duckscript::types::error::ScriptError;
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
use std::collections::HashMap;
#[derive(Clone)]
pub(crate) struct AliasCommand {
name: String,
aliases: Vec<String>,
help: String,
scope_name: String,
raw_command: String,
arguments_amount: usize,
instructions: Vec<Instruction>,
}
impl AliasCommand {
fn new(
name: String,
aliases: Vec<String>,
help: String,
scope_name: String,
raw_command: String,
arguments_amount: usize,
) -> Result<AliasCommand, ScriptError> {
let instructions = parser::parse_text(&raw_command)?;
let mut scope_name_with_prefix = "scope::".to_string();
scope_name_with_prefix.push_str(&scope_name);
Ok(AliasCommand {
name,
aliases,
help,
scope_name: scope_name_with_prefix,
raw_command,
arguments_amount,
instructions,
})
}
}
impl Command for AliasCommand {
fn name(&self) -> String {
self.name.clone()
}
fn aliases(&self) -> Vec<String> {
self.aliases.clone()
}
fn help(&self) -> String {
format!(
r#"
{}
#### Source:
```sh
{}
```
"#,
&self.help, &self.raw_command
)
.to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn requires_context(&self) -> bool {
true
}
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 {
if arguments.len() < self.arguments_amount {
CommandResult::Error("Invalid arguments provided.".to_string())
} else {
let start_count = variables.len();
let line_context_name = set_line_context_name(&self.scope_name, state);
let mut handle_option = None;
if !arguments.is_empty() {
let mut index = 0;
let mut array = vec![];
for argument in arguments {
index = index + 1;
let mut key = self.scope_name.clone();
key.push_str("::argument::");
key.push_str(&index.to_string());
variables.insert(key, argument.clone());
array.push(StateValue::String(argument.clone()));
}
let handle = put_handle(state, StateValue::List(array));
let mut key = self.scope_name.clone();
key.push_str("::arguments");
variables.insert(key, handle.clone());
handle_option = Some(handle);
}
let (flow_result, flow_output) =
eval::eval_instructions(&self.instructions, commands, state, variables, 0);
match handle_option {
Some(handle) => {
let handle_state = get_handles_sub_state(state);
match handle_state.remove(&handle) {
_ => (),
}
}
None => (),
}
clear(&self.scope_name, variables);
set_line_context_name(&line_context_name, state);
let end_count = variables.len();
if start_count < end_count {
CommandResult::Crash(
format!(
"Memory leak detected, delta variables count: {}",
end_count - start_count
)
.to_string(),
)
} else {
match flow_result {
Some(result) => result,
None => CommandResult::Continue(flow_output),
}
}
}
}
}
pub(crate) fn create_alias_command(
name: String,
aliases: Vec<String>,
help: String,
scope_name: String,
script: String,
arguments_amount: usize,
) -> Result<AliasCommand, ScriptError> {
let raw_command = script.to_string();
let command = AliasCommand::new(
name,
aliases,
help,
scope_name,
raw_command,
arguments_amount,
)?;
Ok(command)
}