1
0

Implement length tool

This commit is contained in:
Jeff 2023-10-17 12:43:33 -04:00
parent e95aa1e437
commit f07e20c403
2 changed files with 28 additions and 1 deletions

View File

@ -14,6 +14,8 @@ pub enum Tool {
Read, Read,
Help, Help,
Length,
Raw, Raw,
Sh, Sh,
Bash, Bash,
@ -38,6 +40,8 @@ impl Tool {
"assert_equal" => Tool::AssertEqual, "assert_equal" => Tool::AssertEqual,
"output" => Tool::Output, "output" => Tool::Output,
"length" => Tool::Length,
"raw" => Tool::Raw, "raw" => Tool::Raw,
"sh" => Tool::Sh, "sh" => Tool::Sh,
"bash" => Tool::Bash, "bash" => Tool::Bash,
@ -118,6 +122,29 @@ impl Tool {
Value::Empty Value::Empty
} }
Tool::Length => {
if values.len() != 1 {
return Err(Error::ExpectedToolArgumentAmount {
tool_name: "len",
expected: 1,
actual: values.len(),
});
}
let length = if let Ok(list) = values[0].as_list() {
list.len()
} else if let Ok(map) = values[0].as_map() {
map.len()
} else if let Ok(table) = values[0].as_table() {
table.len()
} else if let Ok(string) = values[0].as_string() {
string.chars().count()
} else {
1
};
Value::Integer(length as i64)
}
Tool::Read => { Tool::Read => {
if values.len() != 1 { if values.len() != 1 {
return Err(Error::ExpectedToolArgumentAmount { return Err(Error::ExpectedToolArgumentAmount {

@ -1 +1 @@
Subproject commit cf22950b7ee6f40c7840b6af4d7b2dda69f4965b Subproject commit 9ec43a98418f395a668c59a79b0832e563d9f0e4