@@ -118,7 +118,7 @@ impl Extensions {
118
118
119
119
/// Set content for an extension without a corresponding instance.
120
120
pub fn set_raw ( & mut self , identifier : String , content : Value ) {
121
- self . extensions . insert ( identifier. to_string ( ) , content) ;
121
+ self . extensions . insert ( identifier, content) ;
122
122
}
123
123
124
124
/// Retrieve the stored data of an instance.
@@ -130,33 +130,73 @@ impl Extensions {
130
130
}
131
131
132
132
/// Iterate of the public extensions whose presence and content is not secret.
133
+ #[ deprecated = "Use the simpler `public` instead." ]
133
134
pub fn iter_public ( & self ) -> PublicExtensions {
134
- PublicExtensions ( self . extensions . iter ( ) )
135
+ self . public ( )
136
+ }
137
+
138
+ /// Iterate of the public extensions whose presence and content is not secret.
139
+ pub fn public ( & self ) -> PublicExtensions {
140
+ PublicExtensions { iter : self . extensions . iter ( ) , private : false }
135
141
}
136
142
137
143
/// Iterate of the private extensions whose presence and content must not be revealed.
144
+ ///
145
+ /// Note: The return type is `PublicExtensions` by accident. This will be fixed in the next
146
+ /// breaking release. The values yielded by the iterator are the private extensions, contrary
147
+ /// to its name and short description.
148
+ #[ deprecated = "The method return type is incorrect. Use the `private` method instead,
149
+ or `public` if you actually intended to iterate public extensions." ]
138
150
pub fn iter_private ( & self ) -> PublicExtensions {
139
- PublicExtensions ( self . extensions . iter ( ) )
151
+ PublicExtensions { iter : self . extensions . iter ( ) , private : true }
152
+ }
153
+
154
+ /// Iterate of the private extensions whose presence and content must not be revealed.
155
+ pub fn private ( & self ) -> PrivateExtensions {
156
+ PrivateExtensions ( self . extensions . iter ( ) )
140
157
}
141
158
}
142
159
143
160
/// An iterator over the public extensions of a grant.
144
- pub struct PublicExtensions < ' a > ( Iter < ' a , String , Value > ) ;
161
+ ///
162
+ /// Note: Due to an api bug that would require a breaking change, this type is also created with
163
+ /// the [`Extensions::iter_private`][1] method. It will yield the private extensions in that case.
164
+ /// This behaviour will be removed in the next breaking release.
165
+ ///
166
+ /// [1]: struct.Extensions.html#method.iter_private
167
+ pub struct PublicExtensions < ' a > {
168
+ iter : Iter < ' a , String , Value > ,
169
+ /// FIXME: marker to simulate the `PrivateExtensions` instead. This avoids a breaking change,
170
+ /// so remove this in the next major version.
171
+ private : bool ,
172
+ }
145
173
146
174
/// An iterator over the private extensions of a grant.
147
175
///
148
176
/// Implementations which acquire an instance should take special not to leak any secrets to
149
177
/// clients and third parties.
150
178
pub struct PrivateExtensions < ' a > ( Iter < ' a , String , Value > ) ;
151
179
180
+ impl PublicExtensions < ' _ > {
181
+ /// Check if this iterator was created with [`iter_private`] and iterates private extensions.
182
+ ///
183
+ /// See the struct documentation for a note on why this method exists.
184
+ #[ deprecated = "This interface should not be required and will be removed." ]
185
+ pub fn is_private ( & self ) -> bool {
186
+ self . private
187
+ }
188
+ }
189
+
152
190
impl < ' a > Iterator for PublicExtensions < ' a > {
153
191
type Item = ( & ' a str , Option < & ' a str > ) ;
154
192
155
193
fn next ( & mut self ) -> Option < Self :: Item > {
156
194
loop {
157
- match self . 0 . next ( ) {
195
+ match self . iter . next ( ) {
158
196
None => return None ,
159
- Some ( ( key, Value :: Public ( content) ) )
197
+ Some ( ( key, Value :: Public ( content) ) ) if !self . private
198
+ => return Some ( ( key, content. as_ref ( ) . map ( String :: as_str) ) ) ,
199
+ Some ( ( key, Value :: Private ( content) ) ) if self . private
160
200
=> return Some ( ( key, content. as_ref ( ) . map ( String :: as_str) ) ) ,
161
201
_ => ( ) ,
162
202
}
@@ -210,3 +250,46 @@ impl<T: GrantExtension + ?Sized> GrantExtension for Rc<T> {
210
250
( * * self ) . identifier ( )
211
251
}
212
252
}
253
+
254
+ #[ cfg( test) ]
255
+ mod tests {
256
+ use super :: { Extensions , Value } ;
257
+
258
+ #[ test]
259
+ #[ allow( deprecated) ]
260
+ fn iteration ( ) {
261
+ let mut extensions = Extensions :: new ( ) ;
262
+ extensions. set_raw ( "pub" . into ( ) , Value :: Public ( Some ( "content" . into ( ) ) ) ) ;
263
+ extensions. set_raw ( "pub_none" . into ( ) , Value :: Public ( None ) ) ;
264
+ extensions. set_raw ( "priv" . into ( ) , Value :: Private ( Some ( "private" . into ( ) ) ) ) ;
265
+ extensions. set_raw ( "priv_none" . into ( ) , Value :: Private ( None ) ) ;
266
+
267
+ assert_eq ! ( extensions. public( )
268
+ . filter( |& ( name, value) | name == "pub" && value == Some ( "content" ) )
269
+ . count( ) , 1 ) ;
270
+ assert_eq ! ( extensions. iter_public( )
271
+ . filter( |& ( name, value) | name == "pub" && value == Some ( "content" ) )
272
+ . count( ) , 1 ) ;
273
+ assert_eq ! ( extensions. public( )
274
+ . filter( |& ( name, value) | name == "pub_none" && value == None )
275
+ . count( ) , 1 ) ;
276
+ assert_eq ! ( extensions. iter_public( )
277
+ . filter( |& ( name, value) | name == "pub_none" && value == None )
278
+ . count( ) , 1 ) ;
279
+ assert_eq ! ( extensions. public( ) . count( ) , 2 ) ;
280
+
281
+ assert_eq ! ( extensions. private( )
282
+ . filter( |& ( name, value) | name == "priv" && value == Some ( "private" ) )
283
+ . count( ) , 1 ) ;
284
+ assert_eq ! ( extensions. iter_private( )
285
+ . filter( |& ( name, value) | name == "priv" && value == Some ( "private" ) )
286
+ . count( ) , 1 ) ;
287
+ assert_eq ! ( extensions. private( )
288
+ . filter( |& ( name, value) | name == "priv_none" && value == None )
289
+ . count( ) , 1 ) ;
290
+ assert_eq ! ( extensions. iter_private( )
291
+ . filter( |& ( name, value) | name == "priv_none" && value == None )
292
+ . count( ) , 1 ) ;
293
+ assert_eq ! ( extensions. private( ) . count( ) , 2 ) ;
294
+ }
295
+ }
0 commit comments