Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature_method_to_disable_builtin_function #129

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ pub trait Context {
/// Calls the function that is linked to the given identifier with the given argument.
/// If no function with the given identifier is found, this method returns `EvalexprError::FunctionIdentifierNotFound`.
fn call_function(&self, identifier: &str, argument: &Value) -> EvalexprResult<Value>;

/// Checks if builtin function has been disabled.
fn is_builtin_fn_disabled(&self) -> bool;
hexofyore marked this conversation as resolved.
Show resolved Hide resolved

/// Disables Builtin function.
fn disable_builtin_fn(&mut self);
hexofyore marked this conversation as resolved.
Show resolved Hide resolved
}

/// A context that allows to assign to variables.
Expand Down Expand Up @@ -79,6 +85,12 @@ impl Context for EmptyContext {
identifier.to_string(),
))
}
/// Builtin functions can't be disbaled for Empty Context.
fn disable_builtin_fn(&mut self) {}
/// Builtin functions are always disabled for Empty Context.
fn is_builtin_fn_disabled(&self) -> bool {
true
hexofyore marked this conversation as resolved.
Show resolved Hide resolved
}
hexofyore marked this conversation as resolved.
Show resolved Hide resolved
}

impl<'a> IterateVariablesContext<'a> for EmptyContext {
Expand All @@ -105,6 +117,7 @@ pub struct HashMapContext {
variables: HashMap<String, Value>,
#[cfg_attr(feature = "serde_support", serde(skip))]
functions: HashMap<String, Function>,
without_builtin_fn: bool,
hexofyore marked this conversation as resolved.
Show resolved Hide resolved
}

impl HashMapContext {
Expand All @@ -128,6 +141,14 @@ impl Context for HashMapContext {
))
}
}

fn disable_builtin_fn(&mut self) {
self.without_builtin_fn = true;
}

fn is_builtin_fn_disabled(&self) -> bool {
self.without_builtin_fn
}
}

impl ContextWithMutableVariables for HashMapContext {
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@
//! When assigning to variables, the assignment is stored in a context.
//! When the variable is read later on, it is read from the context.
//! Contexts can be preserved between multiple calls to eval by creating them yourself.
//! By default, Builtin functions are diabled.
hexofyore marked this conversation as resolved.
Show resolved Hide resolved
//! Builtin functions can be disabled by calling Context::disable_builtin_fn().
//! Here is a simple example to show the difference between preserving and not preserving context between evaluations:
//!
//! ```rust
Expand All @@ -270,6 +272,12 @@
//! Err(EvalexprError::ExpectedInt { actual: Value::from(5.5) }));
//! // Reading a variable does not require a mutable context
//! assert_eq!(eval_with_context("a", &context), Ok(Value::from(5)));
//! // Builtin functions are enabled by default.
hexofyore marked this conversation as resolved.
Show resolved Hide resolved
//! assert_eq!(eval_with_context("max(1,3)",&context),Ok(Value::from(3)));
//! //Disabling builtin function in Context.
//! context.disable_builtin_fn();
//! // Builtin functions are disabled and using them returns Error.
//! assert_eq!(eval_with_context("max(1,3)",&context),Err(EvalexprError::FunctionIdentifierNotFound(String::from("max"))));
//!
//! ```
//!
Expand Down Expand Up @@ -377,6 +385,9 @@
//! Otherwise, a float is returned.
//!
//! The regex functions require the feature flag `regex_support`.
//!
hexofyore marked this conversation as resolved.
Show resolved Hide resolved
//! Builtin functions are enabled by Default.
//! It can be disabled by calling Context::disable_builtin_fn().
//!
//! ### Values
//!
Expand Down Expand Up @@ -519,7 +530,7 @@
//! ## License
//!
//! This crate is primarily distributed under the terms of the MIT license.
//! See [LICENSE](LICENSE) for details.

Check warning on line 533 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, nightly)

unresolved link to `LICENSE`

Check warning on line 533 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, beta)

unresolved link to `LICENSE`

Check warning on line 533 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, stable)

unresolved link to `LICENSE`
//!

#![deny(missing_docs)]
Expand Down
4 changes: 3 additions & 1 deletion src/operator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,9 @@ impl Operator {
let arguments = &arguments[0];

match context.call_function(identifier, arguments) {
Err(EvalexprError::FunctionIdentifierNotFound(_)) => {
Err(EvalexprError::FunctionIdentifierNotFound(_))
if !context.is_builtin_fn_disabled() =>
{
if let Some(builtin_function) = builtin_function(identifier) {
builtin_function.call(arguments)
} else {
Expand Down
11 changes: 11 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2243,3 +2243,14 @@ fn test_negative_power() {
assert_eq!(eval("(-3)^-2"), Ok(Value::Float(1.0 / 9.0)));
assert_eq!(eval("-(3^-2)"), Ok(Value::Float(-1.0 / 9.0)));
}

#[test]
fn test_disabling_builtin_fn() {
let mut context = HashMapContext::new();
// Built in functions are enabled by default.
assert_eq!(eval_with_context("max(1,3)",&context),Ok(Value::from(3)));
// Disabling builtin function in Context.
context.disable_builtin_fn();
// Builting functions are disabled and using them returns Error.
assert_eq!(eval_with_context("max(1,3)",&context),Err(EvalexprError::FunctionIdentifierNotFound(String::from("max"))));
}
hexofyore marked this conversation as resolved.
Show resolved Hide resolved
Loading