1pub mod toon;
2
3#[cfg(feature = "python")]
4mod py_binding {
5 use crate::toon::{encode_with, Config};
6 use pyo3::prelude::*;
7 use pyo3::types::PyBytes;
8
9 #[pyfunction]
10 #[pyo3(signature = (json_bytes, delimiter=",", key_folding=false, flatten_depth=None, empty_array_bare=true, escape_controls=true))]
11 fn dumps_bytes<'py>(
12 py: Python<'py>,
13 json_bytes: &Bound<'py, PyBytes>,
14 delimiter: &str,
15 key_folding: bool,
16 flatten_depth: Option<usize>,
17 empty_array_bare: bool,
18 escape_controls: bool,
19 ) -> PyResult<String> {
20 let delim = delimiter.as_bytes().first().copied().unwrap_or(b',');
21 if !matches!(delim, b',' | b'\t' | b'|') {
22 return Err(pyo3::exceptions::PyValueError::new_err(
23 "delimiter must be ',', '\\t', or '|'",
24 ));
25 }
26 let cfg = Config {
27 delimiter: delim,
28 key_folding,
29 flatten_depth,
30 empty_array_bare,
31 escape_controls,
32 };
33 let bytes = json_bytes.as_bytes();
34 py.detach(|| encode_with(bytes, &cfg))
35 .map_err(pyo3::exceptions::PyValueError::new_err)
36 }
37
38 #[pymodule]
39 fn _etoon(m: &Bound<'_, PyModule>) -> PyResult<()> {
40 m.add_function(wrap_pyfunction!(dumps_bytes, m)?)?;
41 Ok(())
42 }
43}