Improve type checking
This commit is contained in:
parent
0b93b6de23
commit
19a053a288
@ -1,4 +1,9 @@
|
|||||||
//! This module contains dust's built-in commands.
|
//! Dust's built-in commands.
|
||||||
|
//!
|
||||||
|
//! When a tool in invoked in Dust, the input is checked against the inputs listed in its ToolInfo.
|
||||||
|
//! The input should then be double-checked by `Tool::check_input` when you implement `run`. The
|
||||||
|
//! purpose of the second check is to weed out mistakes in how the inputs were described in the
|
||||||
|
//! ToolInfo. The errors from the second check should only come up during development and should not //! be seen by the user.
|
||||||
//!
|
//!
|
||||||
//! ## Writing macros
|
//! ## Writing macros
|
||||||
//!
|
//!
|
||||||
@ -43,10 +48,9 @@ pub mod random;
|
|||||||
pub mod system;
|
pub mod system;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
|
|
||||||
/// Master list of all macros.
|
/// Master list of all tools.
|
||||||
///
|
///
|
||||||
/// This list is used to match identifiers with macros and to provide info to
|
/// This list is used to match identifiers with tools and to provide info to the shell.
|
||||||
/// the shell.
|
|
||||||
pub const TOOL_LIST: [&'static dyn Tool; 57] = [
|
pub const TOOL_LIST: [&'static dyn Tool; 57] = [
|
||||||
&collections::Count,
|
&collections::Count,
|
||||||
&collections::CreateTable,
|
&collections::CreateTable,
|
||||||
@ -111,6 +115,29 @@ pub const TOOL_LIST: [&'static dyn Tool; 57] = [
|
|||||||
pub trait Tool: Sync + Send {
|
pub trait Tool: Sync + Send {
|
||||||
fn info(&self) -> ToolInfo<'static>;
|
fn info(&self) -> ToolInfo<'static>;
|
||||||
fn run(&self, argument: &Value) -> Result<Value>;
|
fn run(&self, argument: &Value) -> Result<Value>;
|
||||||
|
|
||||||
|
fn check_type<'a>(&self, argument: &'a Value) -> Result<&'a Value> {
|
||||||
|
if self
|
||||||
|
.info()
|
||||||
|
.inputs
|
||||||
|
.iter()
|
||||||
|
.any(|value_type| &argument.value_type() == value_type)
|
||||||
|
{
|
||||||
|
Ok(argument)
|
||||||
|
} else {
|
||||||
|
Err(crate::Error::TypeCheckFailure {
|
||||||
|
tool_info: self.info(),
|
||||||
|
argument: argument.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fail(&self, argument: &Value) -> Result<Value> {
|
||||||
|
Err(crate::Error::TypeCheckFailure {
|
||||||
|
tool_info: self.info(),
|
||||||
|
argument: argument.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Information needed for each macro.
|
/// Information needed for each macro.
|
||||||
|
@ -17,24 +17,14 @@ impl Tool for RandomBoolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, argument: &Value) -> Result<Value> {
|
fn run(&self, argument: &Value) -> Result<Value> {
|
||||||
match argument {
|
let argument = self.check_type(argument)?;
|
||||||
Value::Empty => {
|
|
||||||
|
if let Value::Empty = argument {
|
||||||
let boolean = rand::thread_rng().gen();
|
let boolean = rand::thread_rng().gen();
|
||||||
|
|
||||||
Ok(Value::Boolean(boolean))
|
Ok(Value::Boolean(boolean))
|
||||||
}
|
} else {
|
||||||
Value::String(_)
|
self.fail(argument)
|
||||||
| Value::Float(_)
|
|
||||||
| Value::Integer(_)
|
|
||||||
| Value::Boolean(_)
|
|
||||||
| Value::List(_)
|
|
||||||
| Value::Map(_)
|
|
||||||
| Value::Table(_)
|
|
||||||
| Value::Time(_)
|
|
||||||
| Value::Function(_) => Err(Error::TypeCheckFailure {
|
|
||||||
tool_info: self.info(),
|
|
||||||
argument: argument.clone(),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -72,10 +62,7 @@ impl Tool for RandomInteger {
|
|||||||
Ok(Value::Integer(integer))
|
Ok(Value::Integer(integer))
|
||||||
}
|
}
|
||||||
Value::Empty => Ok(crate::Value::Integer(random())),
|
Value::Empty => Ok(crate::Value::Integer(random())),
|
||||||
_ => Err(Error::TypeCheckFailure {
|
_ => self.fail(argument),
|
||||||
tool_info: self.info(),
|
|
||||||
argument: argument.clone(),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,8 +80,9 @@ impl Tool for RandomString {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, argument: &Value) -> Result<Value> {
|
fn run(&self, argument: &Value) -> Result<Value> {
|
||||||
match argument {
|
let argument = self.check_type(argument)?;
|
||||||
Value::Integer(length) => {
|
|
||||||
|
if let Value::Integer(length) = argument {
|
||||||
let length: usize = length.unsigned_abs().try_into().unwrap_or(0);
|
let length: usize = length.unsigned_abs().try_into().unwrap_or(0);
|
||||||
let mut random = String::with_capacity(length);
|
let mut random = String::with_capacity(length);
|
||||||
|
|
||||||
@ -104,9 +92,10 @@ impl Tool for RandomString {
|
|||||||
random.push_str(&random_char);
|
random.push_str(&random_char);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Value::String(random))
|
return Ok(Value::String(random));
|
||||||
}
|
}
|
||||||
Value::Empty => {
|
|
||||||
|
if let Value::Empty = argument {
|
||||||
let mut random = String::with_capacity(10);
|
let mut random = String::with_capacity(10);
|
||||||
|
|
||||||
for _ in 0..10 {
|
for _ in 0..10 {
|
||||||
@ -115,13 +104,10 @@ impl Tool for RandomString {
|
|||||||
random.push_str(&random_char);
|
random.push_str(&random_char);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Value::String(random))
|
return Ok(Value::String(random));
|
||||||
}
|
|
||||||
_ => Err(Error::TypeCheckFailure {
|
|
||||||
tool_info: self.info(),
|
|
||||||
argument: argument.clone(),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.fail(argument)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,14 +119,18 @@ impl Tool for RandomFloat {
|
|||||||
identifier: "random_float",
|
identifier: "random_float",
|
||||||
description: "Generate a random floating point value between 0 and 1.",
|
description: "Generate a random floating point value between 0 and 1.",
|
||||||
group: "random",
|
group: "random",
|
||||||
inputs: vec![],
|
inputs: vec![ValueType::Empty],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, argument: &Value) -> Result<Value> {
|
fn run(&self, argument: &Value) -> Result<Value> {
|
||||||
argument.as_empty()?;
|
let argument = self.check_type(argument)?;
|
||||||
|
|
||||||
|
if argument.is_empty() {
|
||||||
Ok(Value::Float(random()))
|
Ok(Value::Float(random()))
|
||||||
|
} else {
|
||||||
|
self.fail(argument)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,22 +140,22 @@ impl Tool for Random {
|
|||||||
fn info(&self) -> ToolInfo<'static> {
|
fn info(&self) -> ToolInfo<'static> {
|
||||||
ToolInfo {
|
ToolInfo {
|
||||||
identifier: "random",
|
identifier: "random",
|
||||||
description: "Select a random item from a collection.",
|
description: "Select a random item from a list.",
|
||||||
group: "random",
|
group: "random",
|
||||||
inputs: vec![],
|
inputs: vec![ValueType::List],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, argument: &Value) -> Result<Value> {
|
fn run(&self, argument: &Value) -> Result<Value> {
|
||||||
if let Ok(list) = argument.as_list() {
|
let argument = self.check_type(argument)?;
|
||||||
|
|
||||||
|
if let Value::List(list) = argument {
|
||||||
let random_index = thread_rng().gen_range(0..list.len());
|
let random_index = thread_rng().gen_range(0..list.len());
|
||||||
let random_item = list.get(random_index).unwrap();
|
let random_item = list.get(random_index).unwrap();
|
||||||
|
|
||||||
Ok(random_item.clone())
|
Ok(random_item.clone())
|
||||||
} else {
|
} else {
|
||||||
Err(Error::ExpectedCollection {
|
self.fail(argument)
|
||||||
actual: argument.clone(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user