1
0

Add more tools

This commit is contained in:
Jeff 2023-10-13 15:48:48 -04:00
parent da7d6a8493
commit d043f334db
3 changed files with 71 additions and 26 deletions

View File

@ -22,23 +22,13 @@ impl AbstractTree for FunctionCall {
debug_assert_eq!("function_call", node.kind()); debug_assert_eq!("function_call", node.kind());
let name_node = node.child(1).unwrap(); let name_node = node.child(1).unwrap();
let name = match name_node.kind() { let name = match name_node.kind() {
"identifier" => { "identifier" => {
FunctionName::Identifier(Identifier::from_syntax_node(source, name_node)?) FunctionName::Identifier(Identifier::from_syntax_node(source, name_node)?)
} }
"tool" => { "tool" => {
let tool_node = name_node.child(0).unwrap(); let tool_node = name_node.child(0).unwrap();
let tool = match tool_node.kind() { let tool = Tool::new(tool_node.kind())?;
"assert" => Tool::Assert,
"assert_equal" => Tool::AssertEqual,
"output" => Tool::Output,
"random" => Tool::Random,
"random_integer" => Tool::RandomInteger,
"read" => Tool::Read,
"help" => Tool::Help,
_ => panic!("Tool name not recognized."),
};
FunctionName::Tool(tool) FunctionName::Tool(tool)
} }

View File

@ -10,13 +10,57 @@ pub enum Tool {
Assert, Assert,
AssertEqual, AssertEqual,
Output, Output,
Random,
Read, Read,
Help, Help,
Raw,
Sh,
Bash,
Fish,
Zsh,
FromCsv,
ToCsv,
FromJson,
ToJson,
Random,
RandomFloat,
RandomInteger, RandomInteger,
RandomString,
} }
impl Tool { impl Tool {
pub fn new(kind: &str) -> Result<Self> {
let tool = match kind {
"assert" => Tool::Assert,
"assert_equal" => Tool::AssertEqual,
"output" => Tool::Output,
"raw" => Tool::Raw,
"sh" => Tool::Sh,
"bash" => Tool::Bash,
"fish" => Tool::Fish,
"zsh" => Tool::Zsh,
"from_csv" => Tool::FromCsv,
"to_csv" => Tool::ToCsv,
"from_json" => Tool::FromJson,
"to_json" => Tool::ToJson,
"random" => Tool::Random,
"random_integer" => Tool::RandomInteger,
"random_float" => Tool::RandomFloat,
"random_string" => Tool::RandomString,
"read" => Tool::Read,
"help" => Tool::Help,
_ => todo!("Tool name not recognized."),
};
Ok(tool)
}
pub fn run(&self, values: &[Value]) -> Result<Value> { pub fn run(&self, values: &[Value]) -> Result<Value> {
let value = match self { let value = match self {
Tool::Assert => { Tool::Assert => {
@ -65,7 +109,22 @@ impl Tool {
Value::Empty Value::Empty
} }
Tool::Read => {
if values.len() != 1 {
return Err(Error::ExpectedToolArgumentAmount {
tool_name: "read",
expected: 1,
actual: values.len(),
});
}
let file_contents = read_to_string(values[0].as_string()?)?;
Value::String(file_contents)
}
Tool::Random => todo!(), Tool::Random => todo!(),
Tool::RandomFloat => todo!(),
Tool::RandomString => todo!(),
Tool::RandomInteger => { Tool::RandomInteger => {
if values.len() == 0 { if values.len() == 0 {
Value::Integer(random()) Value::Integer(random())
@ -83,19 +142,6 @@ impl Tool {
}); });
} }
} }
Tool::Read => {
if values.len() != 1 {
return Err(Error::ExpectedToolArgumentAmount {
tool_name: "read",
expected: 1,
actual: values.len(),
});
}
let file_contents = read_to_string(values[0].as_string()?)?;
Value::String(file_contents)
}
Tool::Help => { Tool::Help => {
if values.len() > 1 { if values.len() > 1 {
return Err(Error::ExpectedToolArgumentAmount { return Err(Error::ExpectedToolArgumentAmount {
@ -131,6 +177,15 @@ impl Tool {
Value::Table(help_table) Value::Table(help_table)
} }
Tool::Raw => todo!(),
Tool::Sh => todo!(),
Tool::Bash => todo!(),
Tool::Fish => todo!(),
Tool::Zsh => todo!(),
Tool::FromCsv => todo!(),
Tool::ToCsv => todo!(),
Tool::FromJson => todo!(),
Tool::ToJson => todo!(),
}; };
Ok(value) Ok(value)

@ -1 +1 @@
Subproject commit 7021c7f7894b54ddf8c46c91477613b3071819a4 Subproject commit b47907ee26b42990e5170ba9521de5ae66d596ef