11use inflections:: Inflect ;
2+ use std:: borrow:: Cow ;
23
34#[ derive( Debug , PartialEq ) ]
45pub enum Architecture {
@@ -68,7 +69,9 @@ impl TestCase {
6869 }
6970
7071 pub fn name ( & self ) -> String {
71- format ! ( "{:?}-{}" , self . mfgr, self . chip. replace( "." , "_" ) ) . to_sanitized_snake_case ( )
72+ format ! ( "{:?}-{}" , self . mfgr, self . chip. replace( "." , "_" ) )
73+ . to_sanitized_snake_case ( )
74+ . into ( )
7275 }
7376}
7477
@@ -80,95 +83,52 @@ use self::RunWhen::*;
8083/// that are not valid in Rust ident
8184const BLACKLIST_CHARS : & [ char ] = & [ '(' , ')' , '[' , ']' ] ;
8285
83- /// Lovingly stolen from `svd2rust`. Probably could be `Cow`
84- pub trait ToSanitizedSnakeCase {
85- fn to_sanitized_snake_case ( & self ) -> String ;
86+ /// Lovingly stolen from `svd2rust`
87+ pub trait ToSanitizedCase {
88+ fn to_sanitized_not_keyword_snake_case ( & self ) -> Cow < str > ;
89+ fn to_sanitized_snake_case ( & self ) -> Cow < str > {
90+ let s = self . to_sanitized_not_keyword_snake_case ( ) ;
91+ sanitize_keyword ( s)
92+ }
8693}
8794
88- impl ToSanitizedSnakeCase for str {
89- fn to_sanitized_snake_case ( & self ) -> String {
90- macro_rules! keywords {
91- ( $s: expr, $( $kw: ident) ,+, ) => {
92- String :: from( match & $s. to_lowercase( ) [ ..] {
93- $( stringify!( $kw) => concat!( stringify!( $kw) , "_" ) ) ,+,
94- _ => return String :: from( $s. to_snake_case( ) )
95- } )
96- }
97- }
95+ impl ToSanitizedCase for str {
96+ fn to_sanitized_not_keyword_snake_case ( & self ) -> Cow < str > {
97+ const INTERNALS : [ & str ; 4 ] = [ "set_bit" , "clear_bit" , "bit" , "bits" ] ;
9898
9999 let s = self . replace ( BLACKLIST_CHARS , "" ) ;
100-
101100 match s. chars ( ) . next ( ) . unwrap_or ( '\0' ) {
102101 '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
103- format ! ( "_{}" , s. to_snake_case( ) )
102+ format ! ( "_{}" , s. to_snake_case( ) ) . into ( )
104103 }
105104 _ => {
106- keywords ! {
107- s,
108- abstract,
109- alignof,
110- as ,
111- r#async,
112- r#await,
113- become,
114- box,
115- break ,
116- const ,
117- continue ,
118- crate ,
119- do,
120- else,
121- enum ,
122- extern,
123- false ,
124- final,
125- fn ,
126- for ,
127- if ,
128- impl ,
129- in,
130- let ,
131- loop ,
132- macro,
133- match ,
134- mod ,
135- move,
136- mut ,
137- offsetof,
138- override,
139- priv,
140- proc,
141- pub ,
142- pure,
143- ref,
144- return ,
145- self ,
146- sizeof,
147- static ,
148- struct ,
149- super ,
150- trait ,
151- true ,
152- r#try,
153- type ,
154- typeof,
155- unsafe ,
156- unsized,
157- use ,
158- virtual,
159- where ,
160- while ,
161- yield,
162- set_bit,
163- clear_bit,
164- bit,
165- bits,
105+ let s = Cow :: from ( s. to_snake_case ( ) ) ;
106+ if INTERNALS . contains ( & s. as_ref ( ) ) {
107+ s + "_"
108+ } else {
109+ s
166110 }
167111 }
168112 }
169113 }
170114}
171115
116+ pub fn sanitize_keyword ( sc : Cow < str > ) -> Cow < str > {
117+ const KEYWORDS : [ & str ; 55 ] = [
118+ "abstract" , "alignof" , "as" , "async" , "await" , "become" , "box" , "break" , "const" ,
119+ "continue" , "crate" , "do" , "dyn" , "else" , "enum" , "extern" , "false" , "final" , "fn" , "for" ,
120+ "if" , "impl" , "in" , "let" , "loop" , "macro" , "match" , "mod" , "move" , "mut" , "offsetof" ,
121+ "override" , "priv" , "proc" , "pub" , "pure" , "ref" , "return" , "self" , "sizeof" , "static" ,
122+ "struct" , "super" , "trait" , "true" , "try" , "type" , "typeof" , "unsafe" , "unsized" , "use" ,
123+ "virtual" , "where" , "while" , "yield" ,
124+ ] ;
125+ if KEYWORDS . contains ( & sc. as_ref ( ) ) {
126+ sc + "_"
127+ } else {
128+ sc
129+ }
130+ }
131+
172132// NOTE: All chip names must be unique!
173133pub const TESTS : & [ & TestCase ] = & [
174134 // BAD-SVD missing resetValue
0 commit comments