@@ -11,8 +11,8 @@ use std::fmt::{self, Write};
1111///
1212/// These are the URL safe characters, except for `.` which we use as separator between
1313/// key and value of Scope components in backends.
14- const ALLOWED_CHARS : & [ u8 ] =
15- b "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-()$!+'";
14+ const ALLOWED_CHARS : & str =
15+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-()$!+'" ;
1616
1717/// A single scope value of an object.
1818///
@@ -56,9 +56,9 @@ impl Scope {
5656 return Err ( InvalidScopeError :: Empty ) ;
5757 }
5858
59- for & b in name. as_bytes ( ) . iter ( ) . chain ( value. as_bytes ( ) ) {
60- if !ALLOWED_CHARS . contains ( & b ) {
61- return Err ( InvalidScopeError :: InvalidChar ( b . into ( ) ) ) ;
59+ for c in name. chars ( ) . chain ( value. chars ( ) ) {
60+ if !ALLOWED_CHARS . contains ( c ) {
61+ return Err ( InvalidScopeError :: InvalidChar ( c ) ) ;
6262 }
6363 }
6464
@@ -218,11 +218,59 @@ mod tests {
218218 #[ test]
219219 fn test_allowed_characters ( ) {
220220 // Storage paths
221- assert ! ( !ALLOWED_CHARS . contains( & b '.') ) ;
222- assert ! ( !ALLOWED_CHARS . contains( & b '/') ) ;
221+ assert ! ( !ALLOWED_CHARS . contains( '.' ) ) ;
222+ assert ! ( !ALLOWED_CHARS . contains( '/' ) ) ;
223223
224224 // API paths
225- assert ! ( !ALLOWED_CHARS . contains( & b'=' ) ) ;
226- assert ! ( !ALLOWED_CHARS . contains( & b';' ) ) ;
225+ assert ! ( !ALLOWED_CHARS . contains( '=' ) ) ;
226+ assert ! ( !ALLOWED_CHARS . contains( ';' ) ) ;
227+ }
228+
229+ #[ test]
230+ fn test_create_scope_empty ( ) {
231+ let err = Scope :: create ( "" , "value" ) . unwrap_err ( ) ;
232+ assert ! ( matches!( err, InvalidScopeError :: Empty ) ) ;
233+
234+ let err = Scope :: create ( "key" , "" ) . unwrap_err ( ) ;
235+ assert ! ( matches!( err, InvalidScopeError :: Empty ) ) ;
236+ }
237+
238+ #[ test]
239+ fn test_create_scope_invalid_char ( ) {
240+ let err = Scope :: create ( "key/" , "value" ) . unwrap_err ( ) ;
241+ assert ! ( matches!( err, InvalidScopeError :: InvalidChar ( '/' ) ) ) ;
242+
243+ let err = dbg ! ( Scope :: create( "key" , "⚠️" ) . unwrap_err( ) ) ;
244+ assert ! ( matches!( err, InvalidScopeError :: InvalidChar ( '⚠' ) ) ) ;
245+ }
246+
247+ #[ test]
248+ fn test_as_storage_path ( ) {
249+ let scopes = Scopes :: from_iter ( [
250+ Scope :: create ( "org" , "12345" ) . unwrap ( ) ,
251+ Scope :: create ( "project" , "1337" ) . unwrap ( ) ,
252+ ] ) ;
253+
254+ let storage_path = scopes. as_storage_path ( ) . to_string ( ) ;
255+ assert_eq ! ( storage_path, "org.12345/project.1337" ) ;
256+
257+ let empty_scopes = Scopes :: empty ( ) ;
258+ let storage_path = empty_scopes. as_storage_path ( ) . to_string ( ) ;
259+ assert_eq ! ( storage_path, "" ) ;
260+ }
261+
262+ #[ test]
263+ fn test_as_api_path ( ) {
264+ let scopes = Scopes :: from_iter ( [
265+ Scope :: create ( "org" , "12345" ) . unwrap ( ) ,
266+ Scope :: create ( "project" , "1337" ) . unwrap ( ) ,
267+ ] ) ;
268+
269+ let api_path = scopes. as_api_path ( ) . to_string ( ) ;
270+ assert_eq ! ( api_path, "org=12345;project=1337" ) ;
271+
272+ let empty_scopes = Scopes :: empty ( ) ;
273+ let api_path = empty_scopes. as_api_path ( ) . to_string ( ) ;
274+ assert_eq ! ( api_path, "_" ) ;
227275 }
228276}
0 commit comments