Skip to content

Commit 0350c3f

Browse files
committed
Simplified EscapedStr, propegating unescaping errors upwards
1 parent bb38c68 commit 0350c3f

File tree

3 files changed

+9
-56
lines changed

3 files changed

+9
-56
lines changed

src/de/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl<'a, 'de, 's> de::Deserializer<'de> for &'a mut Deserializer<'de, 's> {
499499
if escaped_string.as_bytes().contains(&b'\\') {
500500
let mut string_unescape_buffer_write_position = 0;
501501

502-
for fragment in crate::str::unescape_fragments(escaped_string) {
502+
for fragment in crate::str::EscapedStr(escaped_string).fragments() {
503503
let char_encode_buffer = &mut [0; 4];
504504

505505
let unescaped_bytes = match fragment? {
@@ -1047,7 +1047,7 @@ mod tests {
10471047
fn escaped_str() {
10481048
assert_eq!(
10491049
crate::from_str(r#""Hello\nWorld""#),
1050-
Ok((crate::str::EscapedStr::new(r#"Hello\nWorld"#).unwrap(), 14))
1050+
Ok((crate::str::EscapedStr(r#"Hello\nWorld"#), 14))
10511051
);
10521052
}
10531053

src/ser/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,7 @@ mod tests {
831831
#[test]
832832
fn escaped_str() {
833833
assert_eq!(
834-
crate::to_string::<_, N>(&crate::str::EscapedStr::new(r#"Hello\\nWorld"#).unwrap())
835-
.unwrap(),
834+
crate::to_string::<_, N>(&crate::str::EscapedStr(r#"Hello\\nWorld"#)).unwrap(),
836835
r#""Hello\\nWorld""#
837836
);
838837
}

src/str.rs

+6-52
Original file line numberDiff line numberDiff line change
@@ -55,51 +55,14 @@ fn unescape_next_fragment(
5555
})
5656
}
5757

58-
pub(crate) fn unescape_fragments(
59-
mut escaped_string: &str,
60-
) -> impl Iterator<Item = Result<EscapedStringFragment<'_>, StringUnescapeError>> {
61-
core::iter::from_fn(move || {
62-
if escaped_string.is_empty() {
63-
None
64-
} else {
65-
Some(
66-
unescape_next_fragment(escaped_string).map(|(fragment, rest)| {
67-
escaped_string = rest;
68-
fragment
69-
}),
70-
)
71-
}
72-
})
73-
}
74-
7558
/// A borrowed escaped string
7659
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
7760
#[serde(rename = "__serde_json_core_escaped_string__")]
78-
pub struct EscapedStr<'a>(&'a str);
61+
pub struct EscapedStr<'a>(pub &'a str);
7962

8063
impl<'a> EscapedStr<'a> {
8164
pub(crate) const NAME: &'static str = "__serde_json_core_escaped_string__";
8265

83-
/// Create a new EscapedString, verifying that it's correctly escaped
84-
pub fn new(escaped_string: &'a str) -> Result<Self, StringUnescapeError> {
85-
// Check that all fragments are valid
86-
for fragment in unescape_fragments(escaped_string) {
87-
fragment?;
88-
}
89-
90-
// SAFETY: we've just checked that all fragments are valid
91-
unsafe { Ok(Self::new_unchecked(escaped_string)) }
92-
}
93-
94-
/// Create a new EscapedString without verifying that it's correctly escaped
95-
///
96-
/// # Safety
97-
///
98-
/// escaped_string must be a correctly escaped JSON string without the surrounding quotes.
99-
pub unsafe fn new_unchecked(escaped_string: &'a str) -> Self {
100-
Self(escaped_string)
101-
}
102-
10366
pub fn fragments(&self) -> EscapedStringFragmentIter<'a> {
10467
EscapedStringFragmentIter(self.0)
10568
}
@@ -114,26 +77,17 @@ impl<'a> EscapedStringFragmentIter<'a> {
11477
}
11578

11679
impl<'a> Iterator for EscapedStringFragmentIter<'a> {
117-
type Item = EscapedStringFragment<'a>;
80+
type Item = Result<EscapedStringFragment<'a>, StringUnescapeError>;
11881

11982
fn next(&mut self) -> Option<Self::Item> {
12083
if self.0.is_empty() {
12184
return None;
12285
}
12386

124-
let fragment_result = unescape_next_fragment(self.0);
125-
126-
debug_assert!(
127-
fragment_result.is_ok(),
128-
"{:?} must be valid",
129-
fragment_result
130-
);
131-
132-
// In release, if there's been a logic error, return early as it's better than panicing
133-
let (fragment, rest) = fragment_result.ok()?;
134-
135-
self.0 = rest;
87+
Some(unescape_next_fragment(self.0).map(|(fragment, rest)| {
88+
self.0 = rest;
13689

137-
Some(fragment)
90+
fragment
91+
}))
13892
}
13993
}

0 commit comments

Comments
 (0)