Skip to content

Commit

Permalink
update JsonValue allow_partial to use PartialMode (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Nov 12, 2024
1 parent cf80fbb commit 1fbdb1e
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resolver = "2"

[workspace.package]
authors = ["Samuel Colvin <samuel@pydantic.dev>"]
version = "0.7.0"
version = "0.7.1"
edition = "2021"
license = "MIT"
keywords = ["JSON", "parsing", "deserialization", "iter"]
Expand Down
6 changes: 3 additions & 3 deletions crates/jiter/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hint::black_box;
use std::fs::File;
use std::io::Read;

use jiter::{Jiter, JsonValue, LazyIndexMap, Peek};
use jiter::{Jiter, JsonValue, LazyIndexMap, PartialMode, Peek};
use serde_json::Value;

fn read_file(path: &str) -> String {
Expand Down Expand Up @@ -258,7 +258,7 @@ fn string_array_jiter_value_owned(bench: &mut Bencher) {
let json = read_file("./benches/string_array.json");
let json_data = json.as_bytes();
bench.iter(|| {
let v = JsonValue::parse_owned(black_box(json_data), false, false).unwrap();
let v = JsonValue::parse_owned(black_box(json_data), false, PartialMode::Off).unwrap();
black_box(v)
})
}
Expand All @@ -267,7 +267,7 @@ fn medium_response_jiter_value_owned(bench: &mut Bencher) {
let json = read_file("./benches/medium_response.json");
let json_data = json.as_bytes();
bench.iter(|| {
let v = JsonValue::parse_owned(black_box(json_data), false, false).unwrap();
let v = JsonValue::parse_owned(black_box(json_data), false, PartialMode::Off).unwrap();
black_box(v)
})
}
Expand Down
6 changes: 3 additions & 3 deletions crates/jiter/src/jiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::number_decoder::{NumberAny, NumberFloat, NumberInt, NumberRange};
use crate::parse::{Parser, Peek};
use crate::string_decoder::{StringDecoder, StringDecoderRange, Tape};
use crate::value::{take_value_borrowed, take_value_owned, take_value_skip, JsonValue};
use crate::{JsonError, JsonErrorType};
use crate::{JsonError, JsonErrorType, PartialMode};

pub type JiterResult<T> = Result<T, JiterError>;

Expand Down Expand Up @@ -242,7 +242,7 @@ impl<'j> Jiter<'j> {
&mut self.tape,
DEFAULT_RECURSION_LIMIT,
self.allow_inf_nan,
false,
PartialMode::Off,
)
.map_err(Into::into)
}
Expand Down Expand Up @@ -289,7 +289,7 @@ impl<'j> Jiter<'j> {
&mut self.tape,
DEFAULT_RECURSION_LIMIT,
self.allow_inf_nan,
false,
PartialMode::Off,
)
.map_err(Into::into)
}
Expand Down
35 changes: 34 additions & 1 deletion crates/jiter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,37 @@ pub use py_lossless_float::{FloatMode, LosslessFloat};
#[cfg(feature = "python")]
pub use py_string_cache::{cache_clear, cache_usage, cached_py_string, pystring_fast_new, StringCacheMode};
#[cfg(feature = "python")]
pub use python::{map_json_error, PartialMode, PythonParse};
pub use python::{map_json_error, PythonParse};

#[derive(Debug, Clone, Copy)]
pub enum PartialMode {
Off,
On,
TrailingStrings,
}

impl Default for PartialMode {
fn default() -> Self {
Self::Off
}
}

impl From<bool> for PartialMode {
fn from(mode: bool) -> Self {
if mode {
Self::On
} else {
Self::Off
}
}
}

impl PartialMode {
pub fn is_active(self) -> bool {
!matches!(self, Self::Off)
}

pub fn allow_trailing_str(self) -> bool {
matches!(self, Self::TrailingStrings)
}
}
35 changes: 1 addition & 34 deletions crates/jiter/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::parse::{Parser, Peek};
use crate::py_lossless_float::{get_decimal_type, FloatMode};
use crate::py_string_cache::{StringCacheAll, StringCacheKeys, StringCacheMode, StringMaybeCache, StringNoCache};
use crate::string_decoder::{StringDecoder, Tape};
use crate::{JsonErrorType, LosslessFloat};
use crate::{JsonErrorType, LosslessFloat, PartialMode};

#[derive(Default)]
#[allow(clippy::struct_excessive_bools)]
Expand Down Expand Up @@ -234,19 +234,6 @@ impl<'j, StringCache: StringMaybeCache, KeyCheck: MaybeKeyCheck, ParseNumber: Ma
}
}

#[derive(Debug, Clone, Copy)]
pub enum PartialMode {
Off,
On,
TrailingStrings,
}

impl Default for PartialMode {
fn default() -> Self {
Self::Off
}
}

const PARTIAL_ERROR: &str = "Invalid partial mode, should be `'off'`, `'on'`, `'trailing-strings'` or a `bool`";

impl<'py> FromPyObject<'py> for PartialMode {
Expand All @@ -266,26 +253,6 @@ impl<'py> FromPyObject<'py> for PartialMode {
}
}

impl From<bool> for PartialMode {
fn from(mode: bool) -> Self {
if mode {
Self::On
} else {
Self::Off
}
}
}

impl PartialMode {
fn is_active(self) -> bool {
!matches!(self, Self::Off)
}

fn allow_trailing_str(self) -> bool {
matches!(self, Self::TrailingStrings)
}
}

trait MaybeKeyCheck: Default {
fn check(&mut self, key: &str, index: usize) -> JsonResult<()>;
}
Expand Down
Loading

0 comments on commit 1fbdb1e

Please sign in to comment.