Skip to content

Commit 1224c81

Browse files
committed
cache_strings arg
1 parent 903c3f0 commit 1224c81

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/lib.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern crate core;
44

55
use std::sync::OnceLock;
66

7+
use jiter::StringCacheMode;
78
use pyo3::exceptions::PyTypeError;
89
use pyo3::{prelude::*, sync::GILOnceCell};
910

@@ -38,20 +39,29 @@ pub use validators::{validate_core_schema, PySome, SchemaValidator};
3839

3940
use crate::input::Input;
4041

41-
#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=true))]
42+
#[derive(FromPyObject)]
43+
pub enum CacheStringsArg {
44+
Bool(bool),
45+
Literal(StringCacheMode),
46+
}
47+
48+
#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=CacheStringsArg::Bool(true)))]
4249
pub fn from_json<'py>(
4350
py: Python<'py>,
4451
data: &Bound<'_, PyAny>,
4552
allow_inf_nan: bool,
46-
cache_strings: bool,
53+
cache_strings: CacheStringsArg,
4754
) -> PyResult<Bound<'py, PyAny>> {
4855
let v_match = data
4956
.validate_bytes(false)
5057
.map_err(|_| PyTypeError::new_err("Expected bytes, bytearray or str"))?;
5158
let json_either_bytes = v_match.into_inner();
5259
let json_bytes = json_either_bytes.as_slice();
53-
jiter::python_parse(py, json_bytes, allow_inf_nan, cache_strings.into())
54-
.map_err(|e| jiter::map_json_error(json_bytes, &e))
60+
let cache_mode = match cache_strings {
61+
CacheStringsArg::Bool(b) => b.into(),
62+
CacheStringsArg::Literal(mode) => mode,
63+
};
64+
jiter::python_parse(py, json_bytes, allow_inf_nan, cache_mode).map_err(|e| jiter::map_json_error(json_bytes, &e))
5565
}
5666

5767
pub fn get_pydantic_core_version() -> &'static str {

0 commit comments

Comments
 (0)