From 362a209492d88c953d74260fae6745fb1c4a2f56 Mon Sep 17 00:00:00 2001 From: Sudo Dios Date: Sat, 25 Oct 2025 17:33:09 +0330 Subject: [PATCH 1/5] Add function remove_value to HashMapContext --- src/context/mod.rs | 14 ++++++++++++++ src/lib.rs | 4 ++++ tests/integration.rs | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/src/context/mod.rs b/src/context/mod.rs index 82a83f2..07885bc 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -56,6 +56,13 @@ pub trait ContextWithMutableVariables: Context { ) -> EvalexprResult<(), Self::NumericTypes> { Err(EvalexprError::ContextNotMutable) } + /// Removes the variable with the given identifier from the context. + fn remove_value( + &mut self, + _identifier: &str + ) -> EvalexprResult>, Self::NumericTypes> { + Err(EvalexprError::ContextNotMutable) + } } /// A context that allows to assign to function identifiers. @@ -352,6 +359,13 @@ impl ContextWithMutableVariables self.variables.insert(identifier, value); Ok(()) } + fn remove_value( + &mut self, + identifier: &str + ) -> EvalexprResult>, Self::NumericTypes> { + // Removes a value from the `self.variables`, returning the value at the key if the key was previously in the map. + Ok(self.variables.remove(identifier)) + } } impl ContextWithMutableFunctions diff --git a/src/lib.rs b/src/lib.rs index 4f8a35a..50136e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -309,6 +309,10 @@ //! // ...and read the value in code like this //! assert_eq!(context.get_value("a"), Some(&Value::from_int(10))); //! assert_eq!(context.get_value("b"), Some(&Value::from_float(1.0))); +//! // ...and remove the value in code like this +//! assert_eq!(context.remove_value("a"), Ok(Some(Value::from_int(10)))); +//! // ...and if the value does not exist when removing, it returns None. +//! assert_eq!(context.remove_value("a"), Ok(None)); //! ``` //! //! Contexts are also required for user-defined functions. diff --git a/tests/integration.rs b/tests/integration.rs index 96a772a..a102da3 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -142,6 +142,10 @@ fn test_with_context() { eval_with_context("five < six && true", &context), Ok(Value::Boolean(true)) ); + + assert_eq!(context.remove_value("half"),Ok(Some(Value::Float(0.5)))); + assert_eq!(context.remove_value("zero"),Ok(Some(Value::Int(0)))); + assert_eq!(context.remove_value("zero"),Ok(None)); } #[test] From 20c67364ca5f09d3f3217b7b37930f2f98ca100f Mon Sep 17 00:00:00 2001 From: Sudo Dios Date: Wed, 29 Oct 2025 09:21:05 +0330 Subject: [PATCH 2/5] Fix formatting with rustfmt --- src/context/mod.rs | 4 ++-- tests/integration.rs | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 07885bc..9c3ca21 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -59,7 +59,7 @@ pub trait ContextWithMutableVariables: Context { /// Removes the variable with the given identifier from the context. fn remove_value( &mut self, - _identifier: &str + _identifier: &str, ) -> EvalexprResult>, Self::NumericTypes> { Err(EvalexprError::ContextNotMutable) } @@ -361,7 +361,7 @@ impl ContextWithMutableVariables } fn remove_value( &mut self, - identifier: &str + identifier: &str, ) -> EvalexprResult>, Self::NumericTypes> { // Removes a value from the `self.variables`, returning the value at the key if the key was previously in the map. Ok(self.variables.remove(identifier)) diff --git a/tests/integration.rs b/tests/integration.rs index a102da3..b7129ed 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -142,10 +142,9 @@ fn test_with_context() { eval_with_context("five < six && true", &context), Ok(Value::Boolean(true)) ); - - assert_eq!(context.remove_value("half"),Ok(Some(Value::Float(0.5)))); - assert_eq!(context.remove_value("zero"),Ok(Some(Value::Int(0)))); - assert_eq!(context.remove_value("zero"),Ok(None)); + assert_eq!(context.remove_value("half"), Ok(Some(Value::Float(0.5)))); + assert_eq!(context.remove_value("zero"), Ok(Some(Value::Int(0)))); + assert_eq!(context.remove_value("zero"), Ok(None)); } #[test] From 780457253c5e53853fa3dfaf48491998860a343c Mon Sep 17 00:00:00 2001 From: Sudo Dios Date: Wed, 29 Oct 2025 09:27:18 +0330 Subject: [PATCH 3/5] Sync README.md with rust docs --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 828ab71..f2e9c4e 100644 --- a/README.md +++ b/README.md @@ -329,6 +329,10 @@ assert_eq!(eval_with_context_mut("a = 10; b = 1.0;", &mut context), Ok(().into() // ...and read the value in code like this assert_eq!(context.get_value("a"), Some(&Value::from_int(10))); assert_eq!(context.get_value("b"), Some(&Value::from_float(1.0))); +// ...and remove the value in code like this +assert_eq!(context.remove_value("a"), Ok(Some(Value::from_int(10)))); +// ...and if the value does not exist when removing, it returns None. +assert_eq!(context.remove_value("a"), Ok(None)); ``` Contexts are also required for user-defined functions. From 8869ddbe4aad29503e2f42a0f6748d93b25dc5db Mon Sep 17 00:00:00 2001 From: Sudo Dios Date: Wed, 29 Oct 2025 10:35:53 +0330 Subject: [PATCH 4/5] Adding empty lines to resemble evalexpr style --- src/context/mod.rs | 2 ++ tests/integration.rs | 1 + 2 files changed, 3 insertions(+) diff --git a/src/context/mod.rs b/src/context/mod.rs index 9c3ca21..ea7b8d5 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -56,6 +56,7 @@ pub trait ContextWithMutableVariables: Context { ) -> EvalexprResult<(), Self::NumericTypes> { Err(EvalexprError::ContextNotMutable) } + /// Removes the variable with the given identifier from the context. fn remove_value( &mut self, @@ -359,6 +360,7 @@ impl ContextWithMutableVariables self.variables.insert(identifier, value); Ok(()) } + fn remove_value( &mut self, identifier: &str, diff --git a/tests/integration.rs b/tests/integration.rs index b7129ed..515ffea 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -142,6 +142,7 @@ fn test_with_context() { eval_with_context("five < six && true", &context), Ok(Value::Boolean(true)) ); + assert_eq!(context.remove_value("half"), Ok(Some(Value::Float(0.5)))); assert_eq!(context.remove_value("zero"), Ok(Some(Value::Int(0)))); assert_eq!(context.remove_value("zero"), Ok(None)); From 0bc1b24678fe2fa84390f80c8287a4a1ee34ff9f Mon Sep 17 00:00:00 2001 From: Sudo Dios Date: Wed, 29 Oct 2025 10:36:49 +0330 Subject: [PATCH 5/5] Create a test to confirm the deletion of a value --- tests/integration.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/integration.rs b/tests/integration.rs index 515ffea..cbd0852 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -146,6 +146,12 @@ fn test_with_context() { assert_eq!(context.remove_value("half"), Ok(Some(Value::Float(0.5)))); assert_eq!(context.remove_value("zero"), Ok(Some(Value::Int(0)))); assert_eq!(context.remove_value("zero"), Ok(None)); + assert_eq!( + eval_with_context("zero", &context), + Err(EvalexprError::VariableIdentifierNotFound( + "zero".to_string() + )) + ); } #[test]