From f07e20c403f9d947ad4830ea5c2c04640dc37a3c Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 17 Oct 2023 12:43:33 -0400 Subject: [PATCH] Implement length tool --- src/abstract_tree/tool.rs | 27 +++++++++++++++++++++++++++ tree-sitter-dust | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/abstract_tree/tool.rs b/src/abstract_tree/tool.rs index 5c5f983..8f6d91e 100644 --- a/src/abstract_tree/tool.rs +++ b/src/abstract_tree/tool.rs @@ -14,6 +14,8 @@ pub enum Tool { Read, Help, + Length, + Raw, Sh, Bash, @@ -38,6 +40,8 @@ impl Tool { "assert_equal" => Tool::AssertEqual, "output" => Tool::Output, + "length" => Tool::Length, + "raw" => Tool::Raw, "sh" => Tool::Sh, "bash" => Tool::Bash, @@ -118,6 +122,29 @@ impl Tool { 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 => { if values.len() != 1 { return Err(Error::ExpectedToolArgumentAmount { diff --git a/tree-sitter-dust b/tree-sitter-dust index cf22950..9ec43a9 160000 --- a/tree-sitter-dust +++ b/tree-sitter-dust @@ -1 +1 @@ -Subproject commit cf22950b7ee6f40c7840b6af4d7b2dda69f4965b +Subproject commit 9ec43a98418f395a668c59a79b0832e563d9f0e4