diff --git a/pyckel/src/lib.rs b/pyckel/src/lib.rs index 6257b2fb..3550c56b 100644 --- a/pyckel/src/lib.rs +++ b/pyckel/src/lib.rs @@ -1,11 +1,21 @@ use std::io::Cursor; -use nickel_lang::{eval::cache::CBNCache, program::Program, serialize}; +use nickel_lang::{ + error::Error, + eval::cache::{CBNCache, Cache}, + program::Program, + serialize, +}; use pyo3::{create_exception, exceptions::PyException, prelude::*}; create_exception!(pyckel, NickelException, PyException); +/// Turn a Nickel (Rust) error into a NickelException (Python) +fn error_to_exception, EC: Cache>(error: E, program: &mut Program) -> PyErr { + NickelException::new_err(program.report_as_str(error.into())) +} + /// Evaluate from a Python str of a Nickel expression to a Python str of the resulting JSON. #[pyfunction] pub fn run(s: String) -> PyResult { @@ -14,13 +24,13 @@ pub fn run(s: String) -> PyResult { let term = program .eval_full() - .map_err(|err| NickelException::new_err(program.report_as_str(err)))?; + .map_err(|error| error_to_exception(error, &mut program))?; serialize::validate(serialize::ExportFormat::Json, &term) - .map_err(|err| NickelException::new_err(program.report_as_str(err)))?; + .map_err(|error| error_to_exception(error, &mut program))?; let json_string = serialize::to_string(serialize::ExportFormat::Json, &term) - .map_err(|err| NickelException::new_err(program.report_as_str(err)))?; + .map_err(|error| error_to_exception(error, &mut program))?; Ok(json_string) }