New map command

This commit is contained in:
sagie gur ari 2020-02-01 11:00:18 +00:00
parent 3ee2413ab8
commit 2d49a8c43e
6 changed files with 145 additions and 1 deletions

View file

@ -2,7 +2,8 @@
### v0.1.9
* The set command now supports 'or' condition
* New map command.
* The set command now supports 'or' condition.
* New base64 command #79
* New write_binary_file command #78
* New read_binary_file command #78

View file

@ -21,6 +21,7 @@
* [std::collections::ArrayPop (array_pop)](#std__collections__ArrayPop)
* [std::collections::ArrayPush (array_push)](#std__collections__ArrayPush)
* [std::collections::IsArray (is_array)](#std__collections__IsArray)
* [std::collections::Map (map)](#std__collections__Map)
* [std::collections::Range (range)](#std__collections__Range)
* [std::collections::ReadProperties (read_properties)](#std__collections__ReadProperties)
* [std::collections::WriteProperties (write_properties)](#std__collections__WriteProperties)
@ -1036,6 +1037,37 @@ echo Array length: ${len} released: ${released}
#### Aliases:
is_array
<a name="std__collections__Map"></a>
## std::collections::Map
```sh
handle = map
```
Creates an empty map and returns a handle to that array.<br>
This handle can be passed to other commands which support maps using handles.<br>
Once the map is no longer used, it should be released using the **release** command.
#### Parameters
None
#### Return Value
A handle to the map.
#### Examples
```sh
handle = map
# once done we should release the handle
release ${handle}
```
#### Aliases:
map
<a name="std__collections__Range"></a>
## std::collections::Range
```sh

View file

@ -0,0 +1,24 @@
```sh
handle = map
```
Creates an empty map and returns a handle to that array.<br>
This handle can be passed to other commands which support maps using handles.<br>
Once the map is no longer used, it should be released using the **release** command.
#### Parameters
None
#### Return Value
A handle to the map.
#### Examples
```sh
handle = map
# once done we should release the handle
release ${handle}
```

View file

@ -0,0 +1,60 @@
use crate::utils::pckg;
use crate::utils::state::put_handle;
use duckscript::types::command::{Command, CommandResult, Commands};
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
use std::collections::HashMap;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "Map")
}
fn aliases(&self) -> Vec<String> {
vec!["map".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").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 {
let map = HashMap::new();
let key = put_handle(state, StateValue::SubState(map));
CommandResult::Continue(Some(key))
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,25 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
use crate::utils::state::get_handles_sub_state;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
let mut context = test::run_script_and_validate(
vec![create("")],
"out = map",
CommandValidation::Contains("out".to_string(), "handle:".to_string()),
);
let state = get_handles_sub_state(&mut context.state);
let map_value = state.get(context.variables.get("out").unwrap()).unwrap();
match map_value {
StateValue::SubState(map) => assert!(map.is_empty()),
_ => panic!("Invalid handle type."),
}
}

View file

@ -6,6 +6,7 @@ pub(crate) mod array_length;
pub(crate) mod array_pop;
mod array_push;
mod is_array;
mod map;
mod range;
mod read_properties;
mod write_properties;
@ -27,6 +28,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(array_length::create(&package))?;
commands.set(array_pop::create(&package))?;
commands.set(is_array::create(&package))?;
commands.set(map::create(&package))?;
commands.set(range::create(&package))?;
commands.set(read_properties::create(&package))?;
commands.set(write_properties::create(&package))?;