Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make PythonParser generic around cache mode, not its functions #79

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 41 additions & 40 deletions src/python.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::marker::PhantomData;

use pyo3::exceptions::PyValueError;
use pyo3::ffi;
use pyo3::prelude::*;
Expand Down Expand Up @@ -31,45 +33,52 @@ pub fn python_parse<'py>(
cache_mode: StringCacheMode,
allow_partial: bool,
) -> JsonResult<Bound<'py, PyAny>> {
let mut python_parser = PythonParser {
parser: Parser::new(json_data),
tape: Tape::default(),
recursion_limit: DEFAULT_RECURSION_LIMIT,
allow_inf_nan,
allow_partial,
};

let peek = python_parser.parser.peek()?;
let v = match cache_mode {
StringCacheMode::All => python_parser.py_take_value::<StringCacheAll>(py, peek)?,
StringCacheMode::Keys => python_parser.py_take_value::<StringCacheKeys>(py, peek)?,
StringCacheMode::None => python_parser.py_take_value::<StringNoCache>(py, peek)?,
};
if !allow_partial {
python_parser.parser.finish()?;
match cache_mode {
StringCacheMode::All => PythonParser::<StringCacheAll>::parse(py, json_data, allow_inf_nan, allow_partial),
StringCacheMode::Keys => PythonParser::<StringCacheKeys>::parse(py, json_data, allow_inf_nan, allow_partial),
StringCacheMode::None => PythonParser::<StringNoCache>::parse(py, json_data, allow_inf_nan, allow_partial),
}
Ok(v)
}

/// Map a `JsonError` to a `PyErr` which can be raised as an exception in Python as a `ValueError`.
pub fn map_json_error(json_data: &[u8], json_error: &JsonError) -> PyErr {
PyValueError::new_err(json_error.description(json_data))
}

struct PythonParser<'j> {
struct PythonParser<'j, StringCache> {
_string_cache: PhantomData<StringCache>,
parser: Parser<'j>,
tape: Tape,
recursion_limit: u8,
allow_inf_nan: bool,
allow_partial: bool,
}

impl<'j> PythonParser<'j> {
fn py_take_value<'py, StringCache: StringMaybeCache>(
&mut self,
impl<'j, StringCache: StringMaybeCache> PythonParser<'j, StringCache> {
fn parse<'py>(
py: Python<'py>,
peek: Peek,
json_data: &[u8],
allow_inf_nan: bool,
allow_partial: bool,
) -> JsonResult<Bound<'py, PyAny>> {
let mut slf = PythonParser {
_string_cache: PhantomData::<StringCache>,
parser: Parser::new(json_data),
tape: Tape::default(),
recursion_limit: DEFAULT_RECURSION_LIMIT,
allow_inf_nan,
allow_partial,
};

let peek = slf.parser.peek()?;
let v = slf.py_take_value(py, peek)?;
if !allow_partial {
slf.parser.finish()?;
}
Ok(v)
}

fn py_take_value<'py>(&mut self, py: Python<'py>, peek: Peek) -> JsonResult<Bound<'py, PyAny>> {
match peek {
Peek::Null => {
self.parser.consume_null()?;
Expand All @@ -95,7 +104,7 @@ impl<'j> PythonParser<'j> {
};

let mut vec: SmallVec<[Bound<'_, PyAny>; 8]> = SmallVec::with_capacity(8);
if let Err(e) = self._parse_array::<StringCache>(py, peek_first, &mut vec) {
if let Err(e) = self._parse_array(py, peek_first, &mut vec) {
if !self._allow_partial_err(&e) {
return Err(e);
}
Expand All @@ -105,7 +114,7 @@ impl<'j> PythonParser<'j> {
}
Peek::Object => {
let dict = PyDict::new_bound(py);
if let Err(e) = self._parse_object::<StringCache>(py, &dict) {
if let Err(e) = self._parse_object(py, &dict) {
if !self._allow_partial_err(&e) {
return Err(e);
}
Expand All @@ -132,26 +141,22 @@ impl<'j> PythonParser<'j> {
}
}

fn _parse_array<'py, StringCache: StringMaybeCache>(
fn _parse_array<'py>(
&mut self,
py: Python<'py>,
peek_first: Peek,
vec: &mut SmallVec<[Bound<'py, PyAny>; 8]>,
) -> JsonResult<()> {
let v = self._check_take_value::<StringCache>(py, peek_first)?;
let v = self._check_take_value(py, peek_first)?;
vec.push(v);
while let Some(peek) = self.parser.array_step()? {
let v = self._check_take_value::<StringCache>(py, peek)?;
let v = self._check_take_value(py, peek)?;
vec.push(v);
}
Ok(())
}

fn _parse_object<'py, StringCache: StringMaybeCache>(
&mut self,
py: Python<'py>,
dict: &Bound<'py, PyDict>,
) -> JsonResult<()> {
fn _parse_object<'py>(&mut self, py: Python<'py>, dict: &Bound<'py, PyDict>) -> JsonResult<()> {
let set_item = |key: Bound<'py, PyString>, value: Bound<'py, PyAny>| {
let r = unsafe { ffi::PyDict_SetItem(dict.as_ptr(), key.as_ptr(), value.as_ptr()) };
// AFAIK this shouldn't happen since the key will always be a string which is hashable
Expand All @@ -164,12 +169,12 @@ impl<'j> PythonParser<'j> {
if let Some(first_key) = self.parser.object_first::<StringDecoder>(&mut self.tape)? {
let first_key = StringCache::get_key(py, first_key.as_str(), first_key.ascii_only());
let peek = self.parser.peek()?;
let first_value = self._check_take_value::<StringCache>(py, peek)?;
let first_value = self._check_take_value(py, peek)?;
set_item(first_key, first_value);
while let Some(key) = self.parser.object_step::<StringDecoder>(&mut self.tape)? {
let key = StringCache::get_key(py, key.as_str(), key.ascii_only());
let peek = self.parser.peek()?;
let value = self._check_take_value::<StringCache>(py, peek)?;
let value = self._check_take_value(py, peek)?;
set_item(key, value);
}
}
Expand All @@ -192,17 +197,13 @@ impl<'j> PythonParser<'j> {
}
}

fn _check_take_value<'py, StringCache: StringMaybeCache>(
&mut self,
py: Python<'py>,
peek: Peek,
) -> JsonResult<Bound<'py, PyAny>> {
fn _check_take_value<'py>(&mut self, py: Python<'py>, peek: Peek) -> JsonResult<Bound<'py, PyAny>> {
self.recursion_limit = match self.recursion_limit.checked_sub(1) {
Some(limit) => limit,
None => return json_err!(RecursionLimitExceeded, self.parser.index),
};

let r = self.py_take_value::<StringCache>(py, peek);
let r = self.py_take_value(py, peek);

self.recursion_limit += 1;
r
Expand Down
Loading