Skip to content

Commit a454f9a

Browse files
return JSON
1 parent 86b33ec commit a454f9a

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

.vim/coc-settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rust-analyzer.cargo.features": ["python-module"]
3+
}

src/python.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use std::io::Cursor;
22

3-
use crate::{error::Error, eval::cache::CBNCache, program::Program};
3+
use crate::{
4+
error::{Error, SerializationError},
5+
eval::cache::CBNCache,
6+
program::Program,
7+
serialize,
8+
};
49

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

@@ -10,18 +15,30 @@ impl std::convert::From<Error> for PyErr {
1015
fn from(err: Error) -> PyErr {
1116
match err {
1217
// TODO better exceptions
13-
error => NickelException::new_err(format!("unexpected error: {error:?}")),
18+
error => NickelException::new_err(format!("{error:?}")),
1419
}
1520
}
1621
}
1722

18-
// Some test function
23+
impl std::convert::From<SerializationError> for PyErr {
24+
fn from(err: SerializationError) -> PyErr {
25+
match err {
26+
// TODO better exceptions
27+
error => NickelException::new_err(format!("{error:?}")),
28+
}
29+
}
30+
}
31+
32+
/// Evaluate from a Python str of a Nickel expression to a Python str of the resulting JSON.
1933
#[pyfunction]
2034
pub fn run(s: String) -> PyResult<String> {
2135
let mut program: Program<CBNCache> =
2236
Program::new_from_source(Cursor::new(s.to_string()), "python")?;
23-
let term = program.eval()?;
24-
Ok(term.to_string())
37+
let term = program.eval_full()?;
38+
serialize::validate(serialize::ExportFormat::Json, &term)?;
39+
let json_string = serialize::to_string(serialize::ExportFormat::Json, &term)?;
40+
41+
Ok(json_string)
2542
}
2643

2744
#[pymodule]

0 commit comments

Comments
 (0)