@@ -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
8063impl < ' 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
11679impl < ' 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