1
- // Copyright 2021 Oxide Computer Company
1
+ // Copyright 2022 Oxide Computer Company
2
2
3
3
use std:: collections:: HashSet ;
4
4
@@ -7,6 +7,7 @@ use schemars::schema::{
7
7
ArrayValidation , InstanceType , Metadata , ObjectValidation , Schema , SchemaObject , SingleOrVec ,
8
8
SubschemaValidation ,
9
9
} ;
10
+ use unicode_xid:: UnicodeXID ;
10
11
11
12
use crate :: Name ;
12
13
@@ -463,16 +464,19 @@ pub(crate) fn schema_is_named(schema: &Schema) -> Option<String> {
463
464
}
464
465
465
466
fn sanitize ( input : & str , case : Case ) -> String {
466
- let out = input
467
- . replace ( "$" , "-" )
468
- . replace ( "@" , "-" )
469
- . replace ( "/" , "-" )
470
- . replace ( "+" , "-plus-" )
471
- . replace ( "'" , "" )
472
- . to_case ( case) ;
467
+ // If every case was special then none of them would be.
468
+ let out = match input {
469
+ "+1" => "plus1" . to_string ( ) ,
470
+ "-1" => "minus1" . to_string ( ) ,
471
+ _ => input
472
+ . replace ( "'" , "" )
473
+ . replace ( |c : char | !c. is_xid_continue ( ) , "-" )
474
+ . to_case ( case) ,
475
+ } ;
476
+
473
477
let out = match out. chars ( ) . next ( ) {
474
478
None => "x" . to_case ( case) ,
475
- Some ( 'a' ..= 'z' | 'A' ..= 'Z' | '_' ) => out,
479
+ Some ( c ) if c . is_xid_start ( ) => out,
476
480
Some ( _) => format ! ( "_{}" , out) ,
477
481
} ;
478
482
@@ -618,8 +622,16 @@ mod tests {
618
622
fn test_sanitize ( ) {
619
623
assert_eq ! ( sanitize( "type" , Case :: Snake ) , "type_" ) ;
620
624
assert_eq ! ( sanitize( "ref" , Case :: Snake ) , "ref_" ) ;
621
- assert_eq ! ( sanitize( "+1" , Case :: Snake ) , "plus_1 " ) ;
622
- assert_eq ! ( sanitize( "-1" , Case :: Snake ) , "_1 " ) ;
625
+ assert_eq ! ( sanitize( "+1" , Case :: Snake ) , "plus1 " ) ;
626
+ assert_eq ! ( sanitize( "-1" , Case :: Snake ) , "minus1 " ) ;
623
627
assert_eq ! ( sanitize( "@timestamp" , Case :: Pascal ) , "Timestamp" ) ;
628
+ assert_eq ! ( sanitize( "won't and can't" , Case :: Pascal ) , "WontAndCant" ) ;
629
+ assert_eq ! (
630
+ sanitize(
631
+ "urn:ietf:params:scim:schemas:extension:gluu:2.0:user_" ,
632
+ Case :: Camel
633
+ ) ,
634
+ "urnIetfParamsScimSchemasExtensionGluu20User"
635
+ ) ;
624
636
}
625
637
}
0 commit comments