Skip to content

Commit

Permalink
Support parameter amount errors for functions with varying parameter …
Browse files Browse the repository at this point in the history
…amounts (#160)
  • Loading branch information
ISibboI authored Dec 8, 2023
2 parents 61eb901 + ca67b09 commit 23f83a1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

### Changed

* The error type `WrongFunctionArgumentAmount` now expects a range of argument amounts instead of a single one (#159)

### Fixed

* Minor error in README.
* Regex functions do not silently ignore superfluous arguments anymore.

### Deprecated

Expand Down
17 changes: 12 additions & 5 deletions src/error/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ impl fmt::Display for EvalexprError {
"An operator expected {} arguments, but got {}.",
expected, actual
),
WrongFunctionArgumentAmount { expected, actual } => write!(
f,
"A function expected {} arguments, but got {}.",
expected, actual
),
WrongFunctionArgumentAmount { expected, actual } => {
let expected_arguments = if expected.start() == expected.end() {
format!("{}", expected.start())
} else {
format!("{} to {}", expected.start(), expected.end())
};
write!(
f,
"A function expected {} arguments, but got {}.",
expected_arguments, actual
)
},
ExpectedString { actual } => {
write!(f, "Expected a Value::String, but got {:?}.", actual)
},
Expand Down
21 changes: 18 additions & 3 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! The module also contains some helper functions starting with `expect_` that check for a condition and return `Err(_)` if the condition is not fulfilled.
//! They are meant as shortcuts to not write the same error checking code everywhere.

use std::ops::RangeInclusive;

use crate::{token::PartialToken, value::value_type::ValueType};

use crate::{operator::Operator, value::Value};
Expand All @@ -28,7 +30,7 @@ pub enum EvalexprError {
/// A function was called with a wrong amount of arguments.
WrongFunctionArgumentAmount {
/// The expected amount of arguments.
expected: usize,
expected: RangeInclusive<usize>,
/// The actual amount of arguments.
actual: usize,
},
Expand Down Expand Up @@ -217,11 +219,24 @@ pub enum EvalexprError {
}

impl EvalexprError {
pub(crate) fn wrong_operator_argument_amount(actual: usize, expected: usize) -> Self {
/// Construct a `WrongOperatorArgumentAmount` error.
pub fn wrong_operator_argument_amount(actual: usize, expected: usize) -> Self {
EvalexprError::WrongOperatorArgumentAmount { actual, expected }
}

pub(crate) fn wrong_function_argument_amount(actual: usize, expected: usize) -> Self {
/// Construct a `WrongFunctionArgumentAmount` error for a function with a fixed amount of arguments.
pub fn wrong_function_argument_amount(actual: usize, expected: usize) -> Self {
EvalexprError::WrongFunctionArgumentAmount {
actual,
expected: expected..=expected,
}
}

/// Construct a `WrongFunctionArgumentAmount` error for a function with a range of possible amounts of arguments.
pub fn wrong_function_argument_amount_range(
actual: usize,
expected: RangeInclusive<usize>,
) -> Self {
EvalexprError::WrongFunctionArgumentAmount { actual, expected }
}

Expand Down
4 changes: 2 additions & 2 deletions src/function/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
// String functions
#[cfg(feature = "regex_support")]
"str::regex_matches" => Some(Function::new(|argument| {
let arguments = argument.as_tuple()?;
let arguments = argument.as_fixed_len_tuple(2)?;

let subject = arguments[0].as_string()?;
let re_str = arguments[1].as_string()?;
Expand All @@ -237,7 +237,7 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
})),
#[cfg(feature = "regex_support")]
"str::regex_replace" => Some(Function::new(|argument| {
let arguments = argument.as_tuple()?;
let arguments = argument.as_fixed_len_tuple(3)?;

let subject = arguments[0].as_string()?;
let re_str = arguments[1].as_string()?;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ fn test_error_constructors() {
assert_eq!(
expect_function_argument_amount(2, 3),
Err(EvalexprError::WrongFunctionArgumentAmount {
expected: 3,
expected: 3..=3,
actual: 2
})
);
Expand Down

0 comments on commit 23f83a1

Please sign in to comment.