@@ -55,51 +55,14 @@ fn unescape_next_fragment(
55
55
} )
56
56
}
57
57
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
-
75
58
/// A borrowed escaped string
76
59
#[ derive( Debug , Clone , Copy , PartialEq , Eq , serde:: Serialize , serde:: Deserialize ) ]
77
60
#[ serde( rename = "__serde_json_core_escaped_string__" ) ]
78
- pub struct EscapedStr < ' a > ( & ' a str ) ;
61
+ pub struct EscapedStr < ' a > ( pub & ' a str ) ;
79
62
80
63
impl < ' a > EscapedStr < ' a > {
81
64
pub ( crate ) const NAME : & ' static str = "__serde_json_core_escaped_string__" ;
82
65
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
-
103
66
pub fn fragments ( & self ) -> EscapedStringFragmentIter < ' a > {
104
67
EscapedStringFragmentIter ( self . 0 )
105
68
}
@@ -114,26 +77,17 @@ impl<'a> EscapedStringFragmentIter<'a> {
114
77
}
115
78
116
79
impl < ' a > Iterator for EscapedStringFragmentIter < ' a > {
117
- type Item = EscapedStringFragment < ' a > ;
80
+ type Item = Result < EscapedStringFragment < ' a > , StringUnescapeError > ;
118
81
119
82
fn next ( & mut self ) -> Option < Self :: Item > {
120
83
if self . 0 . is_empty ( ) {
121
84
return None ;
122
85
}
123
86
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;
136
89
137
- Some ( fragment)
90
+ fragment
91
+ } ) )
138
92
}
139
93
}
0 commit comments