Compare commits

...

2 Commits

Author SHA1 Message Date
678fb659ee Add new error for type checks 2023-08-24 03:50:59 -04:00
e55c5688a8 Add type checks 2023-08-24 03:50:23 -04:00
2 changed files with 18 additions and 7 deletions

View File

@ -14,6 +14,12 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[non_exhaustive] #[non_exhaustive]
pub enum Error { pub enum Error {
/// Dust's internal type checking failed to identify a type mismatch. This should never happen, /// the error prompts the user to report the bug.
TypeCheckFailure {
tool_info: ToolInfo<'static>,
argument: Value,
},
/// The 'assert' macro did not resolve successfully. /// The 'assert' macro did not resolve successfully.
AssertEqualFailed { AssertEqualFailed {
expected: Value, expected: Value,
@ -463,6 +469,7 @@ impl fmt::Display for Error {
use Error::*; use Error::*;
match self { match self {
TypeCheckFailure { tool_info, argument } => write!(f, "Type check failure. This is a bug with the tool or with Dust's internal type checking. Please report this bug and include this error message.\nToolInfo = {tool_info:?}\nargument = {argument}"),
AssertEqualFailed {expected, actual } => write!(f, "Equality assertion failed. {expected} does not equal {actual}."), AssertEqualFailed {expected, actual } => write!(f, "Equality assertion failed. {expected} does not equal {actual}."),
AssertFailed => write!(f, "Assertion failed. A false value was passed to \"assert\"."), AssertFailed => write!(f, "Assertion failed. A false value was passed to \"assert\"."),
ExpectedOperatorArgumentAmount { expected, actual } => write!( ExpectedOperatorArgumentAmount { expected, actual } => write!(

View File

@ -2,7 +2,7 @@ use std::convert::TryInto;
use rand::{random, thread_rng, Rng}; use rand::{random, thread_rng, Rng};
use crate::{Error, Result, Tool, ToolInfo, Value}; use crate::{Error, Result, Tool, ToolInfo, Value, ValueType};
pub struct RandomBoolean; pub struct RandomBoolean;
@ -12,7 +12,7 @@ impl Tool for RandomBoolean {
identifier: "random_boolean", identifier: "random_boolean",
description: "Create a random boolean.", description: "Create a random boolean.",
group: "random", group: "random",
inputs: vec![], inputs: vec![ValueType::Empty],
} }
} }
@ -33,7 +33,11 @@ impl Tool for RandomInteger {
identifier: "random_integer", identifier: "random_integer",
description: "Create a random integer.", description: "Create a random integer.",
group: "random", group: "random",
inputs: vec![], inputs: vec![
ValueType::Empty,
ValueType::Integer,
ValueType::ListExact(vec![ValueType::Integer, ValueType::Integer]),
],
} }
} }
@ -47,14 +51,14 @@ impl Tool for RandomInteger {
Value::List(min_max) => { Value::List(min_max) => {
Error::expect_function_argument_amount(self.info().identifier, min_max.len(), 2)?; Error::expect_function_argument_amount(self.info().identifier, min_max.len(), 2)?;
let min = min_max.get(0).unwrap().as_int()?; let min = min_max[0].as_int()?;
let max = min_max.get(1).unwrap().as_int()? + 1; let max = min_max[1].as_int()?;
let integer = rand::thread_rng().gen_range(min..max); let integer = rand::thread_rng().gen_range(min..=max);
Ok(Value::Integer(integer)) Ok(Value::Integer(integer))
} }
Value::Empty => Ok(crate::Value::Integer(random())), Value::Empty => Ok(crate::Value::Integer(random())),
_ => todo!(), value => Err(Error::expected_empty(value.clone())),
} }
} }
} }