mirror of
https://github.com/sagiegurari/duckscript
synced 2024-09-29 20:54:34 +00:00
New array_length command
This commit is contained in:
parent
fa5a3ee08c
commit
bb005815fe
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.1.5
|
||||
|
||||
* New array_length command
|
||||
* ForIn accepts handle value not variable name
|
||||
* New length command (strlen)
|
||||
* New substring command #37
|
||||
|
|
35
docs/sdk.md
35
docs/sdk.md
|
@ -15,6 +15,7 @@
|
|||
* [std::ShowCommandDocumentation (man)](#std__ShowCommandDocumentation)
|
||||
* [std::Unalias (unalias)](#std__Unalias)
|
||||
* [std::collections::Array (array)](#std__collections__Array)
|
||||
* [std::collections::ArrayLength (array_length, arrlen)](#std__collections__ArrayLength)
|
||||
* [std::collections::Range (range)](#std__collections__Range)
|
||||
* [std::env::GetVar (get_env)](#std__env__GetVar)
|
||||
* [std::env::PrintCurrentDirectory (pwd)](#std__env__PrintCurrentDirectory)
|
||||
|
@ -712,6 +713,40 @@ release ${handle}
|
|||
#### Aliases:
|
||||
array
|
||||
|
||||
<a name="std__collections__ArrayLength"></a>
|
||||
## std::collections::ArrayLength
|
||||
```sh
|
||||
var = array_length handle
|
||||
```
|
||||
|
||||
Returns the array length based on the provided array handle.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The array handle.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The array length.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = array a b c "d e"
|
||||
len = array_length ${handle}
|
||||
released = release ${handle}
|
||||
echo Array length: ${len} released: ${released}
|
||||
|
||||
handle = range 0 10
|
||||
len = array_length ${handle}
|
||||
released = release ${handle}
|
||||
echo Array length: ${len} released: ${released}
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
array_length, arrlen
|
||||
|
||||
<a name="std__collections__Range"></a>
|
||||
## std::collections::Range
|
||||
```sh
|
||||
|
|
27
duckscript_sdk/src/sdk/std/collections/array_length/help.md
Normal file
27
duckscript_sdk/src/sdk/std/collections/array_length/help.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
```sh
|
||||
var = array_length handle
|
||||
```
|
||||
|
||||
Returns the array length based on the provided array handle.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The array handle.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The array length.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = array a b c "d e"
|
||||
len = array_length ${handle}
|
||||
released = release ${handle}
|
||||
echo Array length: ${len} released: ${released}
|
||||
|
||||
handle = range 0 10
|
||||
len = array_length ${handle}
|
||||
released = release ${handle}
|
||||
echo Array length: ${len} released: ${released}
|
||||
```
|
65
duckscript_sdk/src/sdk/std/collections/array_length/mod.rs
Executable file
65
duckscript_sdk/src/sdk/std/collections/array_length/mod.rs
Executable file
|
@ -0,0 +1,65 @@
|
|||
use crate::utils::pckg;
|
||||
use crate::utils::state::get_handles_sub_state;
|
||||
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;
|
||||
|
||||
struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "ArrayLength")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["array_length".to_string(), "arrlen".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
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("Array handle not provided.".to_string())
|
||||
} else {
|
||||
let state = get_handles_sub_state(state);
|
||||
|
||||
let key = &arguments[0];
|
||||
|
||||
match state.get(key) {
|
||||
Some(state_value) => match state_value {
|
||||
StateValue::List(list) => CommandResult::Continue(Some(list.len().to_string())),
|
||||
_ => CommandResult::Error("Invalid handle provided.".to_string()),
|
||||
},
|
||||
None => CommandResult::Error("Array not found.".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;
|
||||
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 = array_length", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_not_found() {
|
||||
test::run_script_and_error(vec![create("")], "out = array_length bad_handle", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_found() {
|
||||
test::run_script_and_validate(
|
||||
vec![create(""), array::create("")],
|
||||
r#"
|
||||
handle = array a b c "d e"
|
||||
out = array_length ${handle}
|
||||
"#,
|
||||
CommandValidation::Match("out".to_string(), "4".to_string()),
|
||||
);
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
mod array;
|
||||
pub(crate) mod array;
|
||||
mod array_length;
|
||||
mod range;
|
||||
|
||||
use crate::utils::pckg;
|
||||
|
@ -11,6 +12,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
let package = pckg::concat(parent, PACKAGE);
|
||||
|
||||
commands.set(array::create(&package))?;
|
||||
commands.set(array_length::create(&package))?;
|
||||
commands.set(range::create(&package))?;
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
pub(crate) mod alias;
|
||||
mod collections;
|
||||
pub(crate) mod collections;
|
||||
mod echo;
|
||||
mod env;
|
||||
mod eval;
|
||||
|
|
10
examples/array.ds
Normal file
10
examples/array.ds
Normal file
|
@ -0,0 +1,10 @@
|
|||
|
||||
handle = array a b c "d e"
|
||||
len = array_length ${handle}
|
||||
released = release ${handle}
|
||||
echo Array length: ${len} released: ${released}
|
||||
|
||||
handle = range 0 10
|
||||
len = array_length ${handle}
|
||||
released = release ${handle}
|
||||
echo Array length: ${len} released: ${released}
|
Loading…
Reference in a new issue