Skip to content

Commit e4cf2e2

Browse files
authored
tweak parse_json (#207)
* tweak parse_json * further tweaks
1 parent f5f90ca commit e4cf2e2

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

src/input/input_python.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::str::from_utf8;
23

34
use pyo3::exceptions::PyAttributeError;
@@ -159,8 +160,8 @@ impl<'a> Input<'a> for PyAny {
159160
fn lax_bool(&self) -> ValResult<bool> {
160161
if let Ok(bool) = self.extract::<bool>() {
161162
Ok(bool)
162-
} else if let Some(either_str) = maybe_as_string(self, ErrorKind::BoolParsing)? {
163-
str_as_bool(self, &either_str.as_cow())
163+
} else if let Some(cow_str) = maybe_as_string(self, ErrorKind::BoolParsing)? {
164+
str_as_bool(self, &cow_str)
164165
} else if let Ok(int) = self.extract::<i64>() {
165166
int_as_bool(self, int)
166167
} else if let Ok(float) = self.extract::<f64>() {
@@ -187,8 +188,8 @@ impl<'a> Input<'a> for PyAny {
187188
fn lax_int(&self) -> ValResult<i64> {
188189
if let Ok(int) = self.extract::<i64>() {
189190
Ok(int)
190-
} else if let Some(either_str) = maybe_as_string(self, ErrorKind::IntParsing)? {
191-
str_as_int(self, &either_str.as_cow())
191+
} else if let Some(cow_str) = maybe_as_string(self, ErrorKind::IntParsing)? {
192+
str_as_int(self, &cow_str)
192193
} else if let Ok(float) = self.lax_float() {
193194
float_as_int(self, float)
194195
} else {
@@ -209,8 +210,8 @@ impl<'a> Input<'a> for PyAny {
209210
fn lax_float(&self) -> ValResult<f64> {
210211
if let Ok(float) = self.extract::<f64>() {
211212
Ok(float)
212-
} else if let Some(either_str) = maybe_as_string(self, ErrorKind::FloatParsing)? {
213-
match either_str.as_cow().as_ref().parse() {
213+
} else if let Some(cow_str) = maybe_as_string(self, ErrorKind::FloatParsing)? {
214+
match cow_str.as_ref().parse() {
214215
Ok(i) => Ok(i),
215216
Err(_) => Err(ValError::new(ErrorKind::FloatParsing, self)),
216217
}
@@ -515,12 +516,12 @@ fn from_attributes_applicable(obj: &PyAny) -> bool {
515516
}
516517

517518
/// Utility for extracting a string from a PyAny, if possible.
518-
fn maybe_as_string(v: &PyAny, unicode_error: ErrorKind) -> ValResult<Option<EitherString>> {
519+
fn maybe_as_string(v: &PyAny, unicode_error: ErrorKind) -> ValResult<Option<Cow<str>>> {
519520
if let Ok(py_string) = v.cast_as::<PyString>() {
520-
Ok(Some(py_string.into()))
521+
Ok(Some(py_string.to_string_lossy()))
521522
} else if let Ok(bytes) = v.cast_as::<PyBytes>() {
522523
match from_utf8(bytes.as_bytes()) {
523-
Ok(s) => Ok(Some(s.into())),
524+
Ok(s) => Ok(Some(Cow::Owned(s.to_string()))),
524525
Err(_) => Err(ValError::new(unicode_error, v)),
525526
}
526527
} else {

src/input/parse_json.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ use pyo3::prelude::*;
55
use pyo3::types::PyDict;
66
use serde::de::{Deserialize, DeserializeSeed, Error as SerdeError, MapAccess, SeqAccess, Visitor};
77

8-
// taken from `serde_json`
9-
// We only use our own error type; no need for From conversions provided by the
10-
// standard library's try! macro. This reduces lines of LLVM IR by 4%.
11-
macro_rules! tri {
12-
($e:expr $(,)?) => {
13-
match $e {
14-
Ok(val) => val,
15-
Err(err) => return Err(err),
16-
}
17-
};
18-
}
19-
208
/// similar to serde `Value` but with int and float split
219
#[derive(Clone, Debug)]
2210
pub enum JsonInput {
@@ -52,7 +40,6 @@ impl ToPyObject for JsonInput {
5240
}
5341

5442
impl<'de> Deserialize<'de> for JsonInput {
55-
#[inline]
5643
fn deserialize<D>(deserializer: D) -> Result<JsonInput, D::Error>
5744
where
5845
D: serde::Deserializer<'de>,
@@ -67,65 +54,58 @@ impl<'de> Deserialize<'de> for JsonInput {
6754
formatter.write_str("any valid JSON value")
6855
}
6956

70-
#[inline]
7157
fn visit_bool<E>(self, value: bool) -> Result<JsonInput, E> {
7258
Ok(JsonInput::Bool(value))
7359
}
7460

75-
#[inline]
7661
fn visit_i64<E>(self, value: i64) -> Result<JsonInput, E> {
7762
Ok(JsonInput::Int(value))
7863
}
7964

80-
#[inline]
8165
fn visit_u64<E>(self, value: u64) -> Result<JsonInput, E> {
8266
Ok(JsonInput::Int(value as i64))
8367
}
8468

85-
#[inline]
8669
fn visit_f64<E>(self, value: f64) -> Result<JsonInput, E> {
8770
Ok(JsonInput::Float(value))
8871
}
8972

90-
#[inline]
9173
fn visit_str<E>(self, value: &str) -> Result<JsonInput, E>
9274
where
9375
E: SerdeError,
9476
{
9577
Ok(JsonInput::String(value.to_string()))
9678
}
9779

98-
#[inline]
99-
fn visit_string<E>(self, value: String) -> Result<JsonInput, E> {
100-
Ok(JsonInput::String(value))
80+
#[cfg_attr(has_no_coverage, no_coverage)]
81+
fn visit_string<E>(self, _: String) -> Result<JsonInput, E> {
82+
unreachable!()
10183
}
10284

103-
#[inline]
85+
#[cfg_attr(has_no_coverage, no_coverage)]
10486
fn visit_none<E>(self) -> Result<JsonInput, E> {
105-
Ok(JsonInput::Null)
87+
unreachable!()
10688
}
10789

108-
#[inline]
109-
fn visit_some<D>(self, deserializer: D) -> Result<JsonInput, D::Error>
90+
#[cfg_attr(has_no_coverage, no_coverage)]
91+
fn visit_some<D>(self, _: D) -> Result<JsonInput, D::Error>
11092
where
11193
D: serde::Deserializer<'de>,
11294
{
113-
Deserialize::deserialize(deserializer)
95+
unreachable!()
11496
}
11597

116-
#[inline]
11798
fn visit_unit<E>(self) -> Result<JsonInput, E> {
11899
Ok(JsonInput::Null)
119100
}
120101

121-
#[inline]
122102
fn visit_seq<V>(self, mut visitor: V) -> Result<JsonInput, V::Error>
123103
where
124104
V: SeqAccess<'de>,
125105
{
126106
let mut vec = Vec::new();
127107

128-
while let Some(elem) = tri!(visitor.next_element()) {
108+
while let Some(elem) = visitor.next_element()? {
129109
vec.push(elem);
130110
}
131111

@@ -140,8 +120,8 @@ impl<'de> Deserialize<'de> for JsonInput {
140120
Some(first_key) => {
141121
let mut values = IndexMap::new();
142122

143-
values.insert(first_key, tri!(visitor.next_value()));
144-
while let Some((key, value)) = tri!(visitor.next_entry()) {
123+
values.insert(first_key, visitor.next_value()?);
124+
while let Some((key, value)) = visitor.next_entry()? {
145125
values.insert(key, value);
146126
}
147127
Ok(JsonInput::Object(values))
@@ -183,10 +163,11 @@ impl<'de> Visitor<'de> for KeyDeserializer {
183163
Ok(s.to_string())
184164
}
185165

186-
fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
166+
#[cfg_attr(has_no_coverage, no_coverage)]
167+
fn visit_string<E>(self, _: String) -> Result<Self::Value, E>
187168
where
188169
E: serde::de::Error,
189170
{
190-
Ok(s)
171+
unreachable!()
191172
}
192173
}

0 commit comments

Comments
 (0)