mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 11:53:05 +00:00
New set_to_array command
This commit is contained in:
parent
6dca69a650
commit
c59743ecef
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.3.4
|
||||
|
||||
* New set_to_array command.
|
||||
* New set_contains command.
|
||||
* New set_remove command.
|
||||
* New set_size command.
|
||||
|
|
28
docs/sdk.md
28
docs/sdk.md
|
@ -42,6 +42,7 @@
|
|||
* [std::collections::SetPut (set_put, set_add)](#std__collections__SetPut)
|
||||
* [std::collections::SetRemove (set_remove)](#std__collections__SetRemove)
|
||||
* [std::collections::SetSize (set_size)](#std__collections__SetSize)
|
||||
* [std::collections::SetToArray (set_to_array)](#std__collections__SetToArray)
|
||||
* [std::collections::WriteProperties (write_properties)](#std__collections__WriteProperties)
|
||||
* [std::debug::DuckscriptSDKVersion (duckscript_sdk_version)](#std__debug__DuckscriptSDKVersion)
|
||||
* [std::debug::DuckscriptVersion (duckscript_version)](#std__debug__DuckscriptVersion)
|
||||
|
@ -1759,6 +1760,33 @@ release ${handle}
|
|||
#### Aliases:
|
||||
set_size
|
||||
|
||||
<a name="std__collections__SetToArray"></a>
|
||||
## std::collections::SetToArray
|
||||
```sh
|
||||
array_handle = set_to_array set_handle
|
||||
```
|
||||
|
||||
Converts the provided set to an array and returns the new array handle.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The set handle.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The array handle or false in case of error.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
set_handle = set_new value1 value2 value3
|
||||
array_handle = set_to_array ${set_handle}
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
set_to_array
|
||||
|
||||
<a name="std__collections__WriteProperties"></a>
|
||||
## std::collections::WriteProperties
|
||||
```sh
|
||||
|
|
|
@ -34,6 +34,7 @@ mod set_is_empty;
|
|||
mod set_put;
|
||||
mod set_remove;
|
||||
mod set_size;
|
||||
mod set_to_array;
|
||||
mod write_properties;
|
||||
|
||||
use crate::utils::pckg;
|
||||
|
@ -81,6 +82,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(set_put::create(&package))?;
|
||||
commands.set(set_remove::create(&package))?;
|
||||
commands.set(set_size::create(&package))?;
|
||||
commands.set(set_to_array::create(&package))?;
|
||||
commands.set(write_properties::create(&package))?;
|
||||
|
||||
Ok(())
|
||||
|
|
20
duckscript_sdk/src/sdk/std/collections/set_to_array/help.md
Normal file
20
duckscript_sdk/src/sdk/std/collections/set_to_array/help.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
```sh
|
||||
array_handle = set_to_array set_handle
|
||||
```
|
||||
|
||||
Converts the provided set to an array and returns the new array handle.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The set handle.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The array handle or false in case of error.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
set_handle = set_new value1 value2 value3
|
||||
array_handle = set_to_array ${set_handle}
|
||||
```
|
80
duckscript_sdk/src/sdk/std/collections/set_to_array/mod.rs
Executable file
80
duckscript_sdk/src/sdk/std/collections/set_to_array/mod.rs
Executable file
|
@ -0,0 +1,80 @@
|
|||
use crate::utils::pckg;
|
||||
use crate::utils::state::{get_handles_sub_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, "SetToArray")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["set_to_array".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 {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Error("Set handle not provided.".to_string())
|
||||
} else {
|
||||
let handles_state = get_handles_sub_state(state);
|
||||
|
||||
match handles_state.get(&arguments[0]) {
|
||||
Some(state_value) => match state_value {
|
||||
StateValue::Set(ref set) => {
|
||||
let mut array = vec![];
|
||||
|
||||
for value in set {
|
||||
array.push(StateValue::String(value.to_string()));
|
||||
}
|
||||
|
||||
let key = put_handle(state, StateValue::List(array));
|
||||
|
||||
CommandResult::Continue(Some(key))
|
||||
}
|
||||
_ => CommandResult::Error("Invalid handle provided.".to_string()),
|
||||
},
|
||||
None => CommandResult::Error(
|
||||
format!("Set for handle: {} not found.", &arguments[0]).to_string(),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
use super::*;
|
||||
use crate::sdk::std::collections::{array_length, set};
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_args() {
|
||||
test::run_script_and_error(vec![create("")], "out = set_to_array", "out");
|
||||
}
|
||||
#[test]
|
||||
fn run_not_found() {
|
||||
test::run_script_and_error(vec![create("")], "out = set_to_array bad_handle", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_found() {
|
||||
test::run_script_and_validate(
|
||||
vec![create(""), set::create(""), array_length::create("")],
|
||||
r#"
|
||||
handle = set_new 1 2 3
|
||||
array = set_to_array ${handle}
|
||||
out = array_length ${array}
|
||||
"#,
|
||||
CommandValidation::Match("out".to_string(), "3".to_string()),
|
||||
);
|
||||
}
|
24
test/std/collections/set_to_array_test.ds
Normal file
24
test/std/collections/set_to_array_test.ds
Normal file
|
@ -0,0 +1,24 @@
|
|||
|
||||
fn test_map_keys
|
||||
handle = set_new 1 2 3 4
|
||||
|
||||
arr = set_to_array ${handle}
|
||||
size = array_length ${arr}
|
||||
assert_eq ${size} 4
|
||||
|
||||
result = array_contains ${arr} 1
|
||||
contained = not equals ${result} false
|
||||
assert ${contained}
|
||||
result = array_contains ${arr} 2
|
||||
contained = not equals ${result} false
|
||||
assert ${contained}
|
||||
result = array_contains ${arr} 3
|
||||
contained = not equals ${result} false
|
||||
assert ${contained}
|
||||
result = array_contains ${arr} 4
|
||||
contained = not equals ${result} false
|
||||
assert ${contained}
|
||||
|
||||
release ${handle}
|
||||
release ${arr}
|
||||
end
|
Loading…
Reference in a new issue