Skip to content

Commit

Permalink
return JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDesforges committed Jan 9, 2023
1 parent 86b33ec commit a454f9a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .vim/coc-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.features": ["python-module"]
}
27 changes: 22 additions & 5 deletions src/python.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::io::Cursor;

use crate::{error::Error, eval::cache::CBNCache, program::Program};
use crate::{
error::{Error, SerializationError},
eval::cache::CBNCache,
program::Program,
serialize,
};

use pyo3::{create_exception, exceptions::PyException, prelude::*};

Expand All @@ -10,18 +15,30 @@ impl std::convert::From<Error> for PyErr {
fn from(err: Error) -> PyErr {
match err {
// TODO better exceptions
error => NickelException::new_err(format!("unexpected error: {error:?}")),
error => NickelException::new_err(format!("{error:?}")),
}
}
}

// Some test function
impl std::convert::From<SerializationError> for PyErr {
fn from(err: SerializationError) -> PyErr {
match err {
// TODO better exceptions
error => NickelException::new_err(format!("{error:?}")),
}
}
}

/// 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> {
let mut program: Program<CBNCache> =
Program::new_from_source(Cursor::new(s.to_string()), "python")?;
let term = program.eval()?;
Ok(term.to_string())
let term = program.eval_full()?;
serialize::validate(serialize::ExportFormat::Json, &term)?;
let json_string = serialize::to_string(serialize::ExportFormat::Json, &term)?;

Ok(json_string)
}

#[pymodule]
Expand Down

0 comments on commit a454f9a

Please sign in to comment.