Skip to content

Commit

Permalink
Merge pull request #452 from sagiegurari/0.11.0
Browse files Browse the repository at this point in the history
Enhancement: Runtime - [Breaking Change] Renamed command args to cont…
  • Loading branch information
sagiegurari authored Oct 4, 2024
2 parents 1111c91 + 12d8963 commit 5299842
Show file tree
Hide file tree
Showing 180 changed files with 1,111 additions and 1,071 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### v0.11.0

* Enhancement: Runtime - \[Breaking Change\] Renamed command args to context.

### v0.10.0 (2024-10-03)

* Enhancement: Runtime - \[Breaking Change\] Commands now get new CommandArgs struct instead of multiple fields.
Expand Down
6 changes: 3 additions & 3 deletions duckscript/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod runner_test;

use crate::expansion::{self, ExpandedValue};
use crate::parser;
use crate::types::command::{CommandArgs, CommandResult, Commands, GoToValue};
use crate::types::command::{CommandInvocationContext, CommandResult, Commands, GoToValue};
use crate::types::env::Env;
use crate::types::error::ScriptError;
use crate::types::instruction::{
Expand Down Expand Up @@ -332,8 +332,8 @@ pub fn run_instruction(
&instruction.meta_info,
);

let command_args = CommandArgs {
args: command_arguments,
let command_args = CommandInvocationContext {
arguments: command_arguments,
state,
variables,
output_variable: output_variable.clone(),
Expand Down
51 changes: 27 additions & 24 deletions duckscript/src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::command::{Command, CommandArgs, CommandResult, GoToValue};
use crate::types::command::{Command, CommandInvocationContext, CommandResult, GoToValue};
use crate::types::instruction::{
Instruction, InstructionType, PreProcessInstruction, ScriptInstruction,
};
Expand All @@ -15,11 +15,11 @@ impl Command for SetCommand {
Box::new((*self).clone())
}

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

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

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

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

fn run(&self, arguments: CommandArgs) -> CommandResult {
fn run(&self, context: CommandInvocationContext) -> CommandResult {
let mut index = 0;
for argument in arguments.args {
for argument in context.arguments {
index = index + 1;
arguments
context
.variables
.insert(index.to_string(), argument.clone());
}

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

CommandResult::Continue(None)
}
Expand All @@ -88,7 +88,7 @@ impl Command for ErrorCommand {
Box::new((*self).clone())
}

fn run(&self, _arguments: CommandArgs) -> CommandResult {
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
CommandResult::Error("test".to_string())
}
}
Expand All @@ -105,7 +105,7 @@ impl Command for CrashCommand {
Box::new((*self).clone())
}

fn run(&self, _arguments: CommandArgs) -> CommandResult {
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
CommandResult::Crash("test".to_string())
}
}
Expand All @@ -122,11 +122,14 @@ impl Command for GoToLabelCommand {
Box::new((*self).clone())
}

fn run(&self, arguments: CommandArgs) -> CommandResult {
let (output, label) = if arguments.args.is_empty() {
fn run(&self, context: CommandInvocationContext) -> CommandResult {
let (output, label) = if context.arguments.is_empty() {
(None, "target".to_string())
} else {
(Some(arguments.args[0].clone()), arguments.args[0].clone())
(
Some(context.arguments[0].clone()),
context.arguments[0].clone(),
)
};

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

fn run(&self, arguments: CommandArgs) -> CommandResult {
let (output, line) = if arguments.args.is_empty() {
fn run(&self, context: CommandInvocationContext) -> CommandResult {
let (output, line) = if context.arguments.is_empty() {
(None, 900)
} else {
(
Some(arguments.args[0].clone()),
arguments.args[0].clone().parse().unwrap(),
Some(context.arguments[0].clone()),
context.arguments[0].clone().parse().unwrap(),
)
};

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

fn run(&self, _arguments: CommandArgs) -> CommandResult {
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
CommandResult::Continue(None)
}
}
Expand All @@ -196,7 +199,7 @@ impl Command for TestCommand2 {
Box::new((*self).clone())
}

fn run(&self, _arguments: CommandArgs) -> CommandResult {
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
CommandResult::Continue(None)
}
}
Expand All @@ -217,7 +220,7 @@ impl Command for TestCommand3 {
Box::new((*self).clone())
}

fn run(&self, _arguments: CommandArgs) -> CommandResult {
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
CommandResult::Continue(None)
}
}
Expand All @@ -238,7 +241,7 @@ impl Command for TestCommand4 {
Box::new((*self).clone())
}

fn run(&self, _arguments: CommandArgs) -> CommandResult {
fn run(&self, _context: CommandInvocationContext) -> CommandResult {
CommandResult::Continue(None)
}
}
Expand Down
7 changes: 4 additions & 3 deletions duckscript/src/types/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use crate::types::instruction::Instruction;
use crate::types::runtime::StateValue;
use std::collections::HashMap;

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

/// Run the instruction.
fn run(&self, mut _arguments: CommandArgs) -> CommandResult {
fn run(&self, mut _context: CommandInvocationContext) -> CommandResult {
CommandResult::Crash(format!("Not implemented for command: {}", &self.name()).to_string())
}
}
Expand Down
8 changes: 4 additions & 4 deletions duckscript/src/types/command_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ fn command_default_run() {
}

let command = InnerCommand {};
let result = command.run(CommandArgs {
args: vec![],
let result = command.run(CommandInvocationContext {
arguments: vec![],
state: &mut HashMap::new(),
variables: &mut HashMap::new(),
output_variable: None,
Expand Down Expand Up @@ -93,8 +93,8 @@ fn command_default_run_with_context() {
let mut context = Context::new();
let mut env = Env::default();
let command = InnerCommand {};
let result = command.run(CommandArgs {
args: vec![],
let result = command.run(CommandInvocationContext {
arguments: vec![],
state: &mut HashMap::new(),
variables: &mut HashMap::new(),
output_variable: None,
Expand Down
20 changes: 10 additions & 10 deletions duckscript_sdk/src/sdk/internal/sdkdocs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::io;
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandArgs, CommandResult};
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};

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

fn run(&self, arguments: CommandArgs) -> CommandResult {
if arguments.args.is_empty() {
fn run(&self, context: CommandInvocationContext) -> CommandResult {
if context.arguments.is_empty() {
CommandResult::Error("Documentation output directory not provided.".to_string())
} else {
let (file, prefix) = if arguments.args.len() == 1 {
(arguments.args[0].clone(), "".to_string())
let (file, prefix) = if context.arguments.len() == 1 {
(context.arguments[0].clone(), "".to_string())
} else {
(arguments.args[1].clone(), arguments.args[0].clone())
(context.arguments[1].clone(), context.arguments[0].clone())
};

let names = arguments.commands.get_all_command_names();
let names = context.commands.get_all_command_names();
let mut buffer = String::new();

// create ToC
buffer.push_str("# Table of Contents\n");
for name in &names {
if name.starts_with(&prefix) {
match arguments.commands.get(name) {
match context.commands.get(name) {
Some(command) => {
if !command.help().is_empty() {
let aliases = command.aliases();
Expand All @@ -68,7 +68,7 @@ impl Command for CommandImpl {
}
};
} else {
if let Err(error) = writeln!(arguments.env.out, "Command: {} skipped.", &name) {
if let Err(error) = writeln!(context.env.out, "Command: {} skipped.", &name) {
return CommandResult::Error(error.to_string());
}
}
Expand All @@ -78,7 +78,7 @@ impl Command for CommandImpl {
buffer.push_str("\n");
for name in &names {
if name.starts_with(&prefix) {
let command = match arguments.commands.get(name) {
let command = match context.commands.get(name) {
Some(command) => command,
None => {
return CommandResult::Error(format!("Command: {} not found", name));
Expand Down
8 changes: 4 additions & 4 deletions duckscript_sdk/src/sdk/std/collections/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::pckg;
use crate::utils::state::put_handle;
use duckscript::types::command::{Command, CommandArgs, CommandResult};
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};
use duckscript::types::runtime::StateValue;

#[cfg(test)]
Expand Down Expand Up @@ -29,14 +29,14 @@ impl Command for CommandImpl {
Box::new((*self).clone())
}

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

for argument in arguments.args {
for argument in context.arguments {
array.push(StateValue::String(argument));
}

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

CommandResult::Continue(Some(key))
}
Expand Down
10 changes: 5 additions & 5 deletions duckscript_sdk/src/sdk/std/collections/array_clear/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::pckg;
use crate::utils::state::{get_handles_sub_state, mutate_list};
use duckscript::types::command::{Command, CommandArgs, CommandResult};
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};

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

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

let key = arguments.args[0].clone();
let key = context.arguments[0].clone();

let result = mutate_list(key, state, |list| {
list.clear();
Expand Down
15 changes: 8 additions & 7 deletions duckscript_sdk/src/sdk/std/collections/array_get/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::pckg;
use crate::utils::state::{get_handles_sub_state, get_optional_as_string, mutate_list};
use duckscript::types::command::{Command, CommandArgs, CommandResult};
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};

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

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

let key = arguments.args[0].clone();
let index: usize = match arguments.args[1].parse() {
let key = context.arguments[0].clone();
let index: usize = match context.arguments[1].parse() {
Ok(value) => value,
Err(_) => {
return CommandResult::Error(
format!("Non numeric value: {} provided.", &arguments.args[1]).to_string(),
format!("Non numeric value: {} provided.", &context.arguments[1])
.to_string(),
);
}
};
Expand Down
10 changes: 5 additions & 5 deletions duckscript_sdk/src/sdk/std/collections/array_length/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::pckg;
use crate::utils::state::get_handles_sub_state;
use duckscript::types::command::{Command, CommandArgs, CommandResult};
use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};
use duckscript::types::runtime::StateValue;

#[cfg(test)]
Expand Down Expand Up @@ -33,13 +33,13 @@ impl Command for CommandImpl {
Box::new((*self).clone())
}

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

let key = &arguments.args[0];
let key = &context.arguments[0];

match state.get(key) {
Some(state_value) => match state_value {
Expand Down
Loading

0 comments on commit 5299842

Please sign in to comment.