diff --git a/src/built_in_functions/assert.rs b/src/built_in_functions/assert.rs index 77ee769..e3f3b64 100644 --- a/src/built_in_functions/assert.rs +++ b/src/built_in_functions/assert.rs @@ -1,4 +1,4 @@ -use crate::{BuiltInFunction, Error, Result, Value}; +use crate::{BuiltInFunction, Error, Map, Result, Value}; pub struct Assert; @@ -7,7 +7,7 @@ impl BuiltInFunction for Assert { "assert" } - fn run(&self, arguments: &[Value]) -> Result { + fn run(&self, arguments: &[Value], _context: &Map) -> Result { for argument in arguments { if !argument.as_boolean()? { return Err(Error::AssertFailed); @@ -17,3 +17,27 @@ impl BuiltInFunction for Assert { Ok(Value::Empty) } } + +pub struct AssertEqual; + +impl BuiltInFunction for AssertEqual { + fn name(&self) -> &'static str { + "assert_equal" + } + + fn run(&self, arguments: &[Value], _context: &Map) -> Result { + Error::expect_built_in_function_argument_amount(self, 2, arguments.len())?; + + let left = arguments.get(0).unwrap(); + let right = arguments.get(1).unwrap(); + + if left == right { + Ok(Value::Empty) + } else { + Err(Error::AssertEqualFailed { + expected: left.clone(), + actual: right.clone(), + }) + } + } +} diff --git a/src/built_in_functions/mod.rs b/src/built_in_functions/mod.rs index 28b2a31..ae50ff0 100644 --- a/src/built_in_functions/mod.rs +++ b/src/built_in_functions/mod.rs @@ -1,10 +1,13 @@ use crate::{Map, Result, Value}; +mod assert; mod fs; mod output; mod r#type; -pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 5] = [ +pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 7] = [ + &assert::Assert, + &assert::AssertEqual, &fs::Read, &fs::Write, &fs::Append, diff --git a/src/built_in_functions/type.rs b/src/built_in_functions/type.rs index f9b3f80..2756aba 100644 --- a/src/built_in_functions/type.rs +++ b/src/built_in_functions/type.rs @@ -8,7 +8,7 @@ impl BuiltInFunction for Type { } fn run(&self, arguments: &[Value], context: &Map) -> Result { - Error::expect_function_argument_amount("type", 1, arguments.len())?; + Error::expect_built_in_function_argument_amount(self, 1, arguments.len())?; if arguments.len() == 1 { let type_definition = arguments.first().unwrap().r#type(context)?; diff --git a/src/error.rs b/src/error.rs index 4dc7b77..f0e81e7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,7 +5,7 @@ use tree_sitter::{Node, Point}; -use crate::{value::Value, Identifier, TypeDefinition}; +use crate::{value::Value, BuiltInFunction, Identifier, TypeDefinition}; use std::{fmt, io, num::ParseFloatError, string::FromUtf8Error, sync::PoisonError, time}; @@ -166,8 +166,8 @@ impl Error { } } - pub fn expect_function_argument_amount( - tool_name: &'static str, + pub fn expect_built_in_function_argument_amount( + function: &F, expected: usize, actual: usize, ) -> Result<()> { @@ -175,7 +175,7 @@ impl Error { Ok(()) } else { Err(Error::ExpectedToolArgumentAmount { - tool_name, + tool_name: function.name(), expected, actual, })