1
1
use inflections:: Inflect ;
2
+ use std:: borrow:: Cow ;
2
3
3
4
#[ derive( Debug , PartialEq ) ]
4
5
pub enum Architecture {
@@ -68,7 +69,9 @@ impl TestCase {
68
69
}
69
70
70
71
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 ( )
72
75
}
73
76
}
74
77
@@ -81,94 +84,51 @@ use self::RunWhen::*;
81
84
const BLACKLIST_CHARS : & [ char ] = & [ '(' , ')' , '[' , ']' ] ;
82
85
83
86
/// Lovingly stolen from `svd2rust`. Probably could be `Cow`
84
- pub trait ToSanitizedSnakeCase {
85
- fn to_sanitized_snake_case ( & self ) -> String ;
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
+ }
86
93
}
87
94
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" ] ;
98
98
99
99
let s = self . replace ( BLACKLIST_CHARS , "" ) ;
100
-
101
100
match s. chars ( ) . next ( ) . unwrap_or ( '\0' ) {
102
101
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
103
- format ! ( "_{}" , s. to_snake_case( ) )
102
+ format ! ( "_{}" , s. to_snake_case( ) ) . into ( )
104
103
}
105
104
_ => {
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
166
110
}
167
111
}
168
112
}
169
113
}
170
114
}
171
115
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
+
172
132
// NOTE: All chip names must be unique!
173
133
pub const TESTS : & [ & TestCase ] = & [
174
134
// BAD-SVD missing resetValue
0 commit comments