Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ pub trait ContextWithMutableVariables: Context {
) -> EvalexprResult<(), Self::NumericTypes> {
Err(EvalexprError::ContextNotMutable)
}

/// Removes the variable with the given identifier from the context.
Comment thread
ISibboI marked this conversation as resolved.
fn remove_value(
&mut self,
_identifier: &str,
) -> EvalexprResult<Option<Value<Self::NumericTypes>>, Self::NumericTypes> {
Err(EvalexprError::ContextNotMutable)
}
}

/// A context that allows to assign to function identifiers.
Expand Down Expand Up @@ -352,6 +360,14 @@ impl<NumericTypes: EvalexprNumericTypes> ContextWithMutableVariables
self.variables.insert(identifier, value);
Ok(())
}

fn remove_value(
Comment thread
ISibboI marked this conversation as resolved.
&mut self,
identifier: &str,
) -> EvalexprResult<Option<Value<Self::NumericTypes>>, 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<NumericTypes: EvalexprNumericTypes> ContextWithMutableFunctions
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ 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))));
Comment thread
ISibboI marked this conversation as resolved.
assert_eq!(context.remove_value("zero"), Ok(Some(Value::Int(0))));
assert_eq!(context.remove_value("zero"), Ok(None));
Comment thread
ISibboI marked this conversation as resolved.
assert_eq!(
eval_with_context("zero", &context),
Err(EvalexprError::VariableIdentifierNotFound(
"zero".to_string()
))
);
}

#[test]
Expand Down