Skip to content

Commit d87b779

Browse files
committed
Set version to 0.3.0
1 parent ea614af commit d87b779

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cel"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/context.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use cel_interpreter::objects::TryIntoValue;
22
use cel_interpreter::Value;
3+
use pyo3::exceptions::PyValueError;
34
use pyo3::prelude::*;
45
use pyo3::types::{PyDict, PyTuple};
56
use std::collections::HashMap;
6-
use pyo3::exceptions::PyValueError;
77

88
#[pyo3::pyclass]
99
pub struct Context {
@@ -13,7 +13,6 @@ pub struct Context {
1313

1414
#[pyo3::pymethods]
1515
impl Context {
16-
1716
#[new]
1817
pub fn new(variables: Option<&PyDict>, functions: Option<&PyDict>) -> PyResult<Self> {
1918
let mut context = Context {
@@ -24,24 +23,20 @@ impl Context {
2423
if let Some(variables) = variables {
2524
//context.variables.extend(variables.clone());
2625
for (k, v) in variables {
27-
let key = k.extract::<String>().map_err(|_| {
28-
PyValueError::new_err("Keys must be strings")
29-
});
26+
let key = k
27+
.extract::<String>()
28+
.map_err(|_| PyValueError::new_err("Keys must be strings"));
3029
key.map(|key| context.add_variable(key, v))??;
31-
3230
}
3331
};
3432

3533
if let Some(functions) = functions {
3634
context.update(functions)?;
3735
};
3836

39-
40-
4137
Ok(context)
4238
}
4339

44-
4540
fn add_function(&mut self, name: String, function: Py<PyAny>) {
4641
self.functions.insert(name, function);
4742
}
@@ -58,12 +53,11 @@ impl Context {
5853
}
5954

6055
pub fn update(&mut self, variables: &PyDict) -> PyResult<()> {
61-
6256
for (key, value) in variables {
6357
// Attempt to extract the key as a String
64-
let key = key.extract::<String>().map_err(|_| {
65-
PyValueError::new_err("Keys must be strings")
66-
})?;
58+
let key = key
59+
.extract::<String>()
60+
.map_err(|_| PyValueError::new_err("Keys must be strings"))?;
6761

6862
if value.is_callable() {
6963
// Value is a function, add it to the functions hashmap
@@ -77,7 +71,6 @@ impl Context {
7771

7872
self.variables.insert(key, value);
7973
}
80-
8174
}
8275

8376
Ok(())

src/lib.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,12 @@ impl TryIntoValue for RustyPyType<'_> {
186186
fn evaluate(src: String, evaluation_context: Option<&PyAny>) -> PyResult<RustyCelType> {
187187
debug!("Evaluating CEL expression: {}", src);
188188

189-
let program = Program::compile(&src)
190-
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Failed to compile expression '{}': {}", src, e)))?;
189+
let program = Program::compile(&src).map_err(|e| {
190+
PyValueError::new_err(format!(
191+
"Failed to compile expression '{}': {}",
192+
src, e
193+
))
194+
})?;
191195

192196
debug!("Compiled program: {:?}", program);
193197

@@ -205,20 +209,22 @@ fn evaluate(src: String, evaluation_context: Option<&PyAny>) -> PyResult<RustyCe
205209
// Clone variables and functions into our local Context
206210
ctx.variables = py_context_ref.variables.clone();
207211
ctx.functions = py_context_ref.functions.clone();
208-
209212
} else if let Ok(py_dict) = evaluation_context.extract::<&PyDict>() {
210213
// User passed in a dict - let's process variables and functions from the dict
211214
ctx.update(&py_dict)?;
212215
} else {
213-
return Err(PyValueError::new_err("evaluation_context must be a Context object or a dict"))
216+
return Err(PyValueError::new_err(
217+
"evaluation_context must be a Context object or a dict",
218+
));
214219
};
215220

216-
217221
// Add any variables from the passed in Python context
218222
for (name, value) in &ctx.variables {
219223
environment
220224
.add_variable(name.clone(), value.clone())
221-
.map_err(|e| PyValueError::new_err(format!("Failed to add variable '{}': {}", name, e)))?;
225+
.map_err(|e| {
226+
PyValueError::new_err(format!("Failed to add variable '{}': {}", name, e))
227+
})?;
222228
}
223229

224230
// Add functions
@@ -244,11 +250,12 @@ fn evaluate(src: String, evaluation_context: Option<&PyAny>) -> PyResult<RustyCe
244250
let py_args = PyTuple::new_bound(py, py_args);
245251

246252
// Call the Python function
247-
let py_result = py_function.call1(py, py_args)
248-
.map_err(|e| ExecutionError::FunctionError {
253+
let py_result = py_function.call1(py, py_args).map_err(|e| {
254+
ExecutionError::FunctionError {
249255
function: name.clone(),
250256
message: e.to_string(),
251-
})?;
257+
}
258+
})?;
252259
// Convert the PyObject to &PyAny
253260
let py_result_ref = py_result.as_ref(py);
254261

@@ -266,7 +273,6 @@ fn evaluate(src: String, evaluation_context: Option<&PyAny>) -> PyResult<RustyCe
266273
}
267274
}
268275

269-
270276
let result = program.execute(&environment);
271277
match result {
272278
Err(error) => {

0 commit comments

Comments
 (0)