Skip to content

Commit 5299842

Browse files
authored
Merge pull request #452 from sagiegurari/0.11.0
Enhancement: Runtime - [Breaking Change] Renamed command args to cont…
2 parents 1111c91 + 12d8963 commit 5299842

File tree

180 files changed

+1111
-1071
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+1111
-1071
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## CHANGELOG
22

3+
### v0.11.0
4+
5+
* Enhancement: Runtime - \[Breaking Change\] Renamed command args to context.
6+
37
### v0.10.0 (2024-10-03)
48

59
* Enhancement: Runtime - \[Breaking Change\] Commands now get new CommandArgs struct instead of multiple fields.

duckscript/src/runner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod runner_test;
99

1010
use crate::expansion::{self, ExpandedValue};
1111
use crate::parser;
12-
use crate::types::command::{CommandArgs, CommandResult, Commands, GoToValue};
12+
use crate::types::command::{CommandInvocationContext, CommandResult, Commands, GoToValue};
1313
use crate::types::env::Env;
1414
use crate::types::error::ScriptError;
1515
use crate::types::instruction::{
@@ -332,8 +332,8 @@ pub fn run_instruction(
332332
&instruction.meta_info,
333333
);
334334

335-
let command_args = CommandArgs {
336-
args: command_arguments,
335+
let command_args = CommandInvocationContext {
336+
arguments: command_arguments,
337337
state,
338338
variables,
339339
output_variable: output_variable.clone(),

duckscript/src/test/mod.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::types::command::{Command, CommandArgs, CommandResult, GoToValue};
1+
use crate::types::command::{Command, CommandInvocationContext, CommandResult, GoToValue};
22
use crate::types::instruction::{
33
Instruction, InstructionType, PreProcessInstruction, ScriptInstruction,
44
};
@@ -15,11 +15,11 @@ impl Command for SetCommand {
1515
Box::new((*self).clone())
1616
}
1717

18-
fn run(&self, arguments: CommandArgs) -> CommandResult {
19-
let output = if arguments.args.is_empty() {
18+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
19+
let output = if context.arguments.is_empty() {
2020
None
2121
} else {
22-
Some(arguments.args[0].clone())
22+
Some(context.arguments[0].clone())
2323
};
2424

2525
CommandResult::Continue(output)
@@ -38,11 +38,11 @@ impl Command for ExitCommand {
3838
Box::new((*self).clone())
3939
}
4040

41-
fn run(&self, arguments: CommandArgs) -> CommandResult {
42-
let output = if arguments.args.is_empty() {
41+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
42+
let output = if context.arguments.is_empty() {
4343
None
4444
} else {
45-
Some(arguments.args[0].clone())
45+
Some(context.arguments[0].clone())
4646
};
4747

4848
CommandResult::Exit(output)
@@ -61,16 +61,16 @@ impl Command for OnErrorCommand {
6161
Box::new((*self).clone())
6262
}
6363

64-
fn run(&self, arguments: CommandArgs) -> CommandResult {
64+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
6565
let mut index = 0;
66-
for argument in arguments.args {
66+
for argument in context.arguments {
6767
index = index + 1;
68-
arguments
68+
context
6969
.variables
7070
.insert(index.to_string(), argument.clone());
7171
}
7272

73-
writeln!(arguments.env.out, "{}", "test").unwrap();
73+
writeln!(context.env.out, "{}", "test").unwrap();
7474

7575
CommandResult::Continue(None)
7676
}
@@ -88,7 +88,7 @@ impl Command for ErrorCommand {
8888
Box::new((*self).clone())
8989
}
9090

91-
fn run(&self, _arguments: CommandArgs) -> CommandResult {
91+
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
9292
CommandResult::Error("test".to_string())
9393
}
9494
}
@@ -105,7 +105,7 @@ impl Command for CrashCommand {
105105
Box::new((*self).clone())
106106
}
107107

108-
fn run(&self, _arguments: CommandArgs) -> CommandResult {
108+
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
109109
CommandResult::Crash("test".to_string())
110110
}
111111
}
@@ -122,11 +122,14 @@ impl Command for GoToLabelCommand {
122122
Box::new((*self).clone())
123123
}
124124

125-
fn run(&self, arguments: CommandArgs) -> CommandResult {
126-
let (output, label) = if arguments.args.is_empty() {
125+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
126+
let (output, label) = if context.arguments.is_empty() {
127127
(None, "target".to_string())
128128
} else {
129-
(Some(arguments.args[0].clone()), arguments.args[0].clone())
129+
(
130+
Some(context.arguments[0].clone()),
131+
context.arguments[0].clone(),
132+
)
130133
};
131134

132135
CommandResult::GoTo(output, GoToValue::Label(label))
@@ -145,13 +148,13 @@ impl Command for GoToLineCommand {
145148
Box::new((*self).clone())
146149
}
147150

148-
fn run(&self, arguments: CommandArgs) -> CommandResult {
149-
let (output, line) = if arguments.args.is_empty() {
151+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
152+
let (output, line) = if context.arguments.is_empty() {
150153
(None, 900)
151154
} else {
152155
(
153-
Some(arguments.args[0].clone()),
154-
arguments.args[0].clone().parse().unwrap(),
156+
Some(context.arguments[0].clone()),
157+
context.arguments[0].clone().parse().unwrap(),
155158
)
156159
};
157160

@@ -175,7 +178,7 @@ impl Command for TestCommand1 {
175178
Box::new((*self).clone())
176179
}
177180

178-
fn run(&self, _arguments: CommandArgs) -> CommandResult {
181+
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
179182
CommandResult::Continue(None)
180183
}
181184
}
@@ -196,7 +199,7 @@ impl Command for TestCommand2 {
196199
Box::new((*self).clone())
197200
}
198201

199-
fn run(&self, _arguments: CommandArgs) -> CommandResult {
202+
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
200203
CommandResult::Continue(None)
201204
}
202205
}
@@ -217,7 +220,7 @@ impl Command for TestCommand3 {
217220
Box::new((*self).clone())
218221
}
219222

220-
fn run(&self, _arguments: CommandArgs) -> CommandResult {
223+
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
221224
CommandResult::Continue(None)
222225
}
223226
}
@@ -238,7 +241,7 @@ impl Command for TestCommand4 {
238241
Box::new((*self).clone())
239242
}
240243

241-
fn run(&self, _arguments: CommandArgs) -> CommandResult {
244+
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
242245
CommandResult::Continue(None)
243246
}
244247
}

duckscript/src/types/command.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use crate::types::instruction::Instruction;
1313
use crate::types::runtime::StateValue;
1414
use std::collections::HashMap;
1515

16-
pub struct CommandArgs<'a> {
16+
/// Holdes entire context (args, variables, state, etc...)
17+
pub struct CommandInvocationContext<'a> {
1718
/// The command arguments
18-
pub args: Vec<String>,
19+
pub arguments: Vec<String>,
1920
/// Internal state which is only used by commands to store/pull data
2021
pub state: &'a mut HashMap<String, StateValue>,
2122
/// All script variables
@@ -75,7 +76,7 @@ pub trait Command {
7576
fn clone_and_box(&self) -> Box<dyn Command>;
7677

7778
/// Run the instruction.
78-
fn run(&self, mut _arguments: CommandArgs) -> CommandResult {
79+
fn run(&self, mut _context: CommandInvocationContext) -> CommandResult {
7980
CommandResult::Crash(format!("Not implemented for command: {}", &self.name()).to_string())
8081
}
8182
}

duckscript/src/types/command_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ fn command_default_run() {
6161
}
6262

6363
let command = InnerCommand {};
64-
let result = command.run(CommandArgs {
65-
args: vec![],
64+
let result = command.run(CommandInvocationContext {
65+
arguments: vec![],
6666
state: &mut HashMap::new(),
6767
variables: &mut HashMap::new(),
6868
output_variable: None,
@@ -93,8 +93,8 @@ fn command_default_run_with_context() {
9393
let mut context = Context::new();
9494
let mut env = Env::default();
9595
let command = InnerCommand {};
96-
let result = command.run(CommandArgs {
97-
args: vec![],
96+
let result = command.run(CommandInvocationContext {
97+
arguments: vec![],
9898
state: &mut HashMap::new(),
9999
variables: &mut HashMap::new(),
100100
output_variable: None,

duckscript_sdk/src/sdk/internal/sdkdocs/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::io;
22
use crate::utils::pckg;
3-
use duckscript::types::command::{Command, CommandArgs, CommandResult};
3+
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};
44

55
#[cfg(test)]
66
#[path = "./mod_test.rs"]
@@ -24,24 +24,24 @@ impl Command for CommandImpl {
2424
Box::new((*self).clone())
2525
}
2626

27-
fn run(&self, arguments: CommandArgs) -> CommandResult {
28-
if arguments.args.is_empty() {
27+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
28+
if context.arguments.is_empty() {
2929
CommandResult::Error("Documentation output directory not provided.".to_string())
3030
} else {
31-
let (file, prefix) = if arguments.args.len() == 1 {
32-
(arguments.args[0].clone(), "".to_string())
31+
let (file, prefix) = if context.arguments.len() == 1 {
32+
(context.arguments[0].clone(), "".to_string())
3333
} else {
34-
(arguments.args[1].clone(), arguments.args[0].clone())
34+
(context.arguments[1].clone(), context.arguments[0].clone())
3535
};
3636

37-
let names = arguments.commands.get_all_command_names();
37+
let names = context.commands.get_all_command_names();
3838
let mut buffer = String::new();
3939

4040
// create ToC
4141
buffer.push_str("# Table of Contents\n");
4242
for name in &names {
4343
if name.starts_with(&prefix) {
44-
match arguments.commands.get(name) {
44+
match context.commands.get(name) {
4545
Some(command) => {
4646
if !command.help().is_empty() {
4747
let aliases = command.aliases();
@@ -68,7 +68,7 @@ impl Command for CommandImpl {
6868
}
6969
};
7070
} else {
71-
if let Err(error) = writeln!(arguments.env.out, "Command: {} skipped.", &name) {
71+
if let Err(error) = writeln!(context.env.out, "Command: {} skipped.", &name) {
7272
return CommandResult::Error(error.to_string());
7373
}
7474
}
@@ -78,7 +78,7 @@ impl Command for CommandImpl {
7878
buffer.push_str("\n");
7979
for name in &names {
8080
if name.starts_with(&prefix) {
81-
let command = match arguments.commands.get(name) {
81+
let command = match context.commands.get(name) {
8282
Some(command) => command,
8383
None => {
8484
return CommandResult::Error(format!("Command: {} not found", name));

duckscript_sdk/src/sdk/std/collections/array/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::pckg;
22
use crate::utils::state::put_handle;
3-
use duckscript::types::command::{Command, CommandArgs, CommandResult};
3+
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};
44
use duckscript::types::runtime::StateValue;
55

66
#[cfg(test)]
@@ -29,14 +29,14 @@ impl Command for CommandImpl {
2929
Box::new((*self).clone())
3030
}
3131

32-
fn run(&self, arguments: CommandArgs) -> CommandResult {
32+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
3333
let mut array = vec![];
3434

35-
for argument in arguments.args {
35+
for argument in context.arguments {
3636
array.push(StateValue::String(argument));
3737
}
3838

39-
let key = put_handle(arguments.state, StateValue::List(array));
39+
let key = put_handle(context.state, StateValue::List(array));
4040

4141
CommandResult::Continue(Some(key))
4242
}

duckscript_sdk/src/sdk/std/collections/array_clear/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::pckg;
22
use crate::utils::state::{get_handles_sub_state, mutate_list};
3-
use duckscript::types::command::{Command, CommandArgs, CommandResult};
3+
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};
44

55
#[cfg(test)]
66
#[path = "./mod_test.rs"]
@@ -28,13 +28,13 @@ impl Command for CommandImpl {
2828
Box::new((*self).clone())
2929
}
3030

31-
fn run(&self, arguments: CommandArgs) -> CommandResult {
32-
if arguments.args.is_empty() {
31+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
32+
if context.arguments.is_empty() {
3333
CommandResult::Error("Array handle not provided.".to_string())
3434
} else {
35-
let state = get_handles_sub_state(arguments.state);
35+
let state = get_handles_sub_state(context.state);
3636

37-
let key = arguments.args[0].clone();
37+
let key = context.arguments[0].clone();
3838

3939
let result = mutate_list(key, state, |list| {
4040
list.clear();

duckscript_sdk/src/sdk/std/collections/array_get/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::pckg;
22
use crate::utils::state::{get_handles_sub_state, get_optional_as_string, mutate_list};
3-
use duckscript::types::command::{Command, CommandArgs, CommandResult};
3+
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};
44

55
#[cfg(test)]
66
#[path = "./mod_test.rs"]
@@ -28,18 +28,19 @@ impl Command for CommandImpl {
2828
Box::new((*self).clone())
2929
}
3030

31-
fn run(&self, arguments: CommandArgs) -> CommandResult {
32-
if arguments.args.len() < 2 {
31+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
32+
if context.arguments.len() < 2 {
3333
CommandResult::Error("Array handle or item index not provided.".to_string())
3434
} else {
35-
let state = get_handles_sub_state(arguments.state);
35+
let state = get_handles_sub_state(context.state);
3636

37-
let key = arguments.args[0].clone();
38-
let index: usize = match arguments.args[1].parse() {
37+
let key = context.arguments[0].clone();
38+
let index: usize = match context.arguments[1].parse() {
3939
Ok(value) => value,
4040
Err(_) => {
4141
return CommandResult::Error(
42-
format!("Non numeric value: {} provided.", &arguments.args[1]).to_string(),
42+
format!("Non numeric value: {} provided.", &context.arguments[1])
43+
.to_string(),
4344
);
4445
}
4546
};

duckscript_sdk/src/sdk/std/collections/array_length/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::pckg;
22
use crate::utils::state::get_handles_sub_state;
3-
use duckscript::types::command::{Command, CommandArgs, CommandResult};
3+
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};
44
use duckscript::types::runtime::StateValue;
55

66
#[cfg(test)]
@@ -33,13 +33,13 @@ impl Command for CommandImpl {
3333
Box::new((*self).clone())
3434
}
3535

36-
fn run(&self, arguments: CommandArgs) -> CommandResult {
37-
if arguments.args.is_empty() {
36+
fn run(&self, context: CommandInvocationContext) -> CommandResult {
37+
if context.arguments.is_empty() {
3838
CommandResult::Error("Array handle not provided.".to_string())
3939
} else {
40-
let state = get_handles_sub_state(arguments.state);
40+
let state = get_handles_sub_state(context.state);
4141

42-
let key = &arguments.args[0];
42+
let key = &context.arguments[0];
4343

4444
match state.get(key) {
4545
Some(state_value) => match state_value {

0 commit comments

Comments
 (0)