diff --git a/jiter-python/src/lib.rs b/jiter-python/src/lib.rs index 160c608..ca2e0fd 100644 --- a/jiter-python/src/lib.rs +++ b/jiter-python/src/lib.rs @@ -1,11 +1,17 @@ use pyo3::prelude::*; -use jiter::{python_parse, map_json_error}; +use jiter::{map_json_error, python_parse}; #[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=true))] -pub fn from_json(py: Python, data: &[u8], allow_inf_nan: bool, cache_strings: bool) -> PyResult { +pub fn from_json( + py: Python, + data: &[u8], + allow_inf_nan: bool, + cache_strings: bool, +) -> PyResult { + let cache_mode = cache_strings.into(); let json_bytes = data; - python_parse(py, json_bytes, allow_inf_nan, cache_strings).map_err(|e| map_json_error(json_bytes, &e)) + python_parse(py, json_bytes, allow_inf_nan, cache_mode).map_err(|e| map_json_error(json_bytes, &e)) } #[pymodule] diff --git a/src/py_string_cache.rs b/src/py_string_cache.rs index cb8d7b4..7087b42 100644 --- a/src/py_string_cache.rs +++ b/src/py_string_cache.rs @@ -15,23 +15,31 @@ pub enum StringCacheMode { None, } -impl TryFrom<&PyAny> for StringCacheMode { - type Error = PyErr; - - fn try_from(mode: &PyAny) -> PyResult { - if let Ok(bool_mode) = mode.downcast::() { - Ok(if bool_mode.is_true() { Self::All } else { Self::None }) +impl<'py> FromPyObject<'py> for StringCacheMode { + fn extract(ob: &'py PyAny) -> PyResult { + if let Ok(bool_mode) = ob.downcast::() { + Ok(bool_mode.is_true().into()) } else { - match mode.extract()? { + match ob.extract()? { "all" => Ok(Self::All), "keys" => Ok(Self::Keys), "none" => Ok(Self::None), - _ => Err(PyTypeError::new_err(format!("Invalid string cache mode: {}", mode))), + _ => Err(PyTypeError::new_err(format!("Invalid string cache mode: {}", ob))), } } } } +impl From for StringCacheMode { + fn from(mode: bool) -> Self { + if mode { + Self::All + } else { + Self::None + } + } +} + pub trait StringMaybeCache { fn get_key(py: Python, json_str: &str) -> PyObject;