Add random tools
This commit is contained in:
parent
c6675e87ba
commit
7206d60ac9
@ -33,6 +33,8 @@ impl AbstractTree for FunctionCall {
|
|||||||
"assert" => Tool::Assert,
|
"assert" => Tool::Assert,
|
||||||
"assert_equal" => Tool::AssertEqual,
|
"assert_equal" => Tool::AssertEqual,
|
||||||
"output" => Tool::Output,
|
"output" => Tool::Output,
|
||||||
|
"random" => Tool::Random,
|
||||||
|
"random_integer" => Tool::RandomInteger,
|
||||||
"read" => Tool::Read,
|
"read" => Tool::Read,
|
||||||
"help" => Tool::Help,
|
"help" => Tool::Help,
|
||||||
_ => panic!("Tool name not recognized."),
|
_ => panic!("Tool name not recognized."),
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
use rand::{random, thread_rng, Rng};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{Error, Result, Table, Value};
|
use crate::{Error, Result, Table, Value};
|
||||||
@ -9,8 +10,10 @@ pub enum Tool {
|
|||||||
Assert,
|
Assert,
|
||||||
AssertEqual,
|
AssertEqual,
|
||||||
Output,
|
Output,
|
||||||
|
Random,
|
||||||
Read,
|
Read,
|
||||||
Help,
|
Help,
|
||||||
|
RandomInteger,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tool {
|
impl Tool {
|
||||||
@ -62,6 +65,24 @@ impl Tool {
|
|||||||
|
|
||||||
Value::Empty
|
Value::Empty
|
||||||
}
|
}
|
||||||
|
Tool::Random => todo!(),
|
||||||
|
Tool::RandomInteger => {
|
||||||
|
if values.len() == 0 {
|
||||||
|
Value::Integer(random())
|
||||||
|
} else if values.len() == 2 {
|
||||||
|
let range = values[0].as_int()?..values[1].as_int()?;
|
||||||
|
let mut rng = thread_rng();
|
||||||
|
let random = rng.gen_range(range);
|
||||||
|
|
||||||
|
Value::Integer(random)
|
||||||
|
} else {
|
||||||
|
return Err(Error::ExpectedToolArgumentAmount {
|
||||||
|
tool_name: "random_integer",
|
||||||
|
expected: 2,
|
||||||
|
actual: values.len(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Tool::Read => {
|
Tool::Read => {
|
||||||
if values.len() != 1 {
|
if values.len() != 1 {
|
||||||
return Err(Error::ExpectedToolArgumentAmount {
|
return Err(Error::ExpectedToolArgumentAmount {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Error and Result types.
|
//! Error and Result types.
|
||||||
//!
|
//!
|
||||||
//! To deal with errors from dependencies, either create a new error variant
|
//! To deal with errors from dependencies, either create a new error variant
|
||||||
//! or use the MacroFailure variant if the error can only occur inside a macro.
|
//! or use the ToolFailure variant if the error can only occur inside a tool.
|
||||||
|
|
||||||
use crate::{value::Value, Identifier};
|
use crate::{value::Value, Identifier};
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 0b6c6b7e25e137c8d5ad09d19584d028a371fd16
|
Subproject commit 7021c7f7894b54ddf8c46c91477613b3071819a4
|
Loading…
Reference in New Issue
Block a user