1
1
mirror of https://github.com/tweag/nickel.git synced 2024-09-21 00:19:57 +03:00

apply review to pyckel/src/lib.rs

This commit is contained in:
Guillaume Desforges 2023-01-18 22:45:02 +01:00
parent 201455755f
commit 54e7cad356

View File

@ -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<E: Into<Error>, EC: Cache>(error: E, program: &mut Program<EC>) -> 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<String> {
@ -14,13 +24,13 @@ pub fn run(s: String) -> PyResult<String> {
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)
}