diff --git a/CHANGELOG.md b/CHANGELOG.md index f560400..482ed08 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### v0.3.4 +* New set_from_array command. * New set_to_array command. * New set_contains command. * New set_remove command. diff --git a/docs/sdk.md b/docs/sdk.md index 2f8643c..87eaf28 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -38,6 +38,7 @@ * [std::collections::Set (set_new)](#std__collections__Set) * [std::collections::SetClear (set_clear)](#std__collections__SetClear) * [std::collections::SetContains (set_contains)](#std__collections__SetContains) +* [std::collections::SetFromArray (set_from_array)](#std__collections__SetFromArray) * [std::collections::SetIsEmpty (set_is_empty)](#std__collections__SetIsEmpty) * [std::collections::SetPut (set_put, set_add)](#std__collections__SetPut) * [std::collections::SetRemove (set_remove)](#std__collections__SetRemove) @@ -1622,6 +1623,52 @@ found = set_contains ${handle} value2 #### Aliases: set_contains + +## std::collections::SetFromArray + +```sh +set_handle = set_from_array array_handle +``` + +Returns a set handle created from the provided array values. + +#### Parameters + +The array handle. + +#### Return Value + +The new set handle. + +#### Examples + +```sh +array_handle = array value1 value2 value3 +set_handle = set_from_array ${handle} +``` + + +#### Source: + +```sh + +if not is_array ${scope::set_from_array::argument::1} + trigger_error "Invalid input, non array handle or array not found." +end + +scope::set_from_array::set = set_new +for scope::set_from_array::next_value in ${scope::set_from_array::argument::1} + set_put ${scope::set_from_array::set} ${scope::set_from_array::next_value} +end + +set ${scope::set_from_array::set} + +``` + + +#### Aliases: +set_from_array + ## std::collections::SetIsEmpty diff --git a/duckscript_sdk/src/sdk/std/collections/mod.rs b/duckscript_sdk/src/sdk/std/collections/mod.rs index fe68ddb..400b4e3 100755 --- a/duckscript_sdk/src/sdk/std/collections/mod.rs +++ b/duckscript_sdk/src/sdk/std/collections/mod.rs @@ -30,6 +30,7 @@ mod read_properties; mod set; mod set_clear; mod set_contains; +mod set_from_array; mod set_is_empty; mod set_put; mod set_remove; @@ -78,6 +79,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr commands.set(set::create(&package))?; commands.set(set_clear::create(&package))?; commands.set(set_contains::create(&package))?; + commands.set(set_from_array::create(&package)?)?; commands.set(set_is_empty::create(&package)?)?; commands.set(set_put::create(&package))?; commands.set(set_remove::create(&package))?; diff --git a/duckscript_sdk/src/sdk/std/collections/set_from_array/help.md b/duckscript_sdk/src/sdk/std/collections/set_from_array/help.md new file mode 100644 index 0000000..c5a48ca --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/set_from_array/help.md @@ -0,0 +1,20 @@ +```sh +set_handle = set_from_array array_handle +``` + +Returns a set handle created from the provided array values. + +#### Parameters + +The array handle. + +#### Return Value + +The new set handle. + +#### Examples + +```sh +array_handle = array value1 value2 value3 +set_handle = set_from_array ${handle} +``` diff --git a/duckscript_sdk/src/sdk/std/collections/set_from_array/mod.rs b/duckscript_sdk/src/sdk/std/collections/set_from_array/mod.rs new file mode 100755 index 0000000..e1e9d70 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/set_from_array/mod.rs @@ -0,0 +1,22 @@ +use crate::types::command::create_alias_command; +use crate::utils::pckg; +use duckscript::types::command::Command; +use duckscript::types::error::ScriptError; + +#[cfg(test)] +#[path = "./mod_test.rs"] +mod mod_test; + +pub(crate) fn create(package: &str) -> Result, ScriptError> { + let name = pckg::concat(package, "SetFromArray"); + let command = create_alias_command( + name, + vec!["set_from_array".to_string()], + include_str!("help.md").to_string(), + "set_from_array".to_string(), + include_str!("script.ds").to_string(), + 1, + )?; + + Ok(Box::new(command)) +} diff --git a/duckscript_sdk/src/sdk/std/collections/set_from_array/mod_test.rs b/duckscript_sdk/src/sdk/std/collections/set_from_array/mod_test.rs new file mode 100644 index 0000000..cbc4367 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/set_from_array/mod_test.rs @@ -0,0 +1,7 @@ +use super::*; +use crate::test; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("").unwrap()); +} diff --git a/duckscript_sdk/src/sdk/std/collections/set_from_array/script.ds b/duckscript_sdk/src/sdk/std/collections/set_from_array/script.ds new file mode 100644 index 0000000..1eca694 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/set_from_array/script.ds @@ -0,0 +1,11 @@ + +if not is_array ${scope::set_from_array::argument::1} + trigger_error "Invalid input, non array handle or array not found." +end + +scope::set_from_array::set = set_new +for scope::set_from_array::next_value in ${scope::set_from_array::argument::1} + set_put ${scope::set_from_array::set} ${scope::set_from_array::next_value} +end + +set ${scope::set_from_array::set} diff --git a/test/std/collections/set_from_array_test.ds b/test/std/collections/set_from_array_test.ds new file mode 100644 index 0000000..9f6dcf3 --- /dev/null +++ b/test/std/collections/set_from_array_test.ds @@ -0,0 +1,21 @@ + +fn test_valid + arr = array 1 2 3 1 + + handle = set_from_array ${arr} + size = set_size ${handle} + assert_eq ${size} 3 + + result = set_contains ${handle} 1 + contained = not equals ${result} false + assert ${contained} + result = set_contains ${handle} 2 + contained = not equals ${result} false + assert ${contained} + result = set_contains ${handle} 3 + contained = not equals ${result} false + assert ${contained} + + release ${handle} + release ${arr} +end diff --git a/test/std/collections/set_to_array_test.ds b/test/std/collections/set_to_array_test.ds index 0235e2e..38059d1 100644 --- a/test/std/collections/set_to_array_test.ds +++ b/test/std/collections/set_to_array_test.ds @@ -1,5 +1,5 @@ -fn test_map_keys +fn test_valid handle = set_new 1 2 3 4 arr = set_to_array ${handle}