Skip to content

Commit c33ce8c

Browse files
author
Jorge Aparicio
committed
sanitize against parentheses in idents
1 parent 56e0ef0 commit c33ce8c

File tree

1 file changed

+80
-67
lines changed

1 file changed

+80
-67
lines changed

src/lib.rs

Lines changed: 80 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@
315315
//! gpioa.dir.write(|w| w.pin0().output());
316316
//! ```
317317
//!
318-
//! The `bits` method is still available but will become safe if it's impossible to
319-
//! write a reserved bit pattern into the register
318+
//! The `bits` method is still available but will become safe if it's impossible
319+
//! to write a reserved bit pattern into the register
320320
//!
321321
//! ```
322322
//! // safe because there are only two options: `0` or `1`
@@ -345,6 +345,10 @@ use svd::{Access, BitRange, Defaults, EnumeratedValues, Field, Peripheral,
345345
Register, RegisterInfo, Usage};
346346
use syn::*;
347347

348+
/// List of chars that some vendors use in their peripheral/field names but
349+
/// that are not valid in Rust ident
350+
const BLACKLIST_CHARS: &[char] = &['(', ')'];
351+
348352
trait ToSanitizedPascalCase {
349353
fn to_sanitized_pascal_case(&self) -> Cow<str>;
350354
}
@@ -356,84 +360,89 @@ trait ToSanitizedSnakeCase {
356360
impl ToSanitizedSnakeCase for str {
357361
fn to_sanitized_snake_case(&self) -> Cow<str> {
358362
macro_rules! keywords {
359-
($($kw:ident),+,) => {
360-
Cow::from(match &self.to_lowercase()[..] {
363+
($s:expr, $($kw:ident),+,) => {
364+
Cow::from(match &$s.to_lowercase()[..] {
361365
$(stringify!($kw) => concat!(stringify!($kw), "_")),+,
362-
_ => return Cow::from(self.to_snake_case())
366+
_ => return Cow::from($s.to_snake_case())
363367
})
364368
}
365369
}
366370

367-
match self.chars().next().unwrap_or('\0') {
371+
let s = self.replace(BLACKLIST_CHARS, "");
372+
373+
match s.chars().next().unwrap_or('\0') {
368374
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
369-
Cow::from(format!("_{}", self.to_snake_case()))
375+
Cow::from(format!("_{}", s.to_snake_case()))
370376
}
371377
_ => {
372-
keywords!{
373-
abstract,
374-
alignof,
375-
as,
376-
become,
377-
box,
378-
break,
379-
const,
380-
continue,
381-
crate,
382-
do,
383-
else,
384-
enum,
385-
extern,
386-
false,
387-
final,
388-
fn,
389-
for,
390-
if,
391-
impl,
392-
in,
393-
let,
394-
loop,
395-
macro,
396-
match,
397-
mod,
398-
move,
399-
mut,
400-
offsetof,
401-
override,
402-
priv,
403-
proc,
404-
pub,
405-
pure,
406-
ref,
407-
return,
408-
self,
409-
sizeof,
410-
static,
411-
struct,
412-
super,
413-
trait,
414-
true,
415-
type,
416-
typeof,
417-
unsafe,
418-
unsized,
419-
use,
420-
virtual,
421-
where,
422-
while,
423-
yield,
424-
}
378+
keywords! {
379+
s,
380+
abstract,
381+
alignof,
382+
as,
383+
become,
384+
box,
385+
break,
386+
const,
387+
continue,
388+
crate,
389+
do,
390+
else,
391+
enum,
392+
extern,
393+
false,
394+
final,
395+
fn,
396+
for,
397+
if,
398+
impl,
399+
in,
400+
let,
401+
loop,
402+
macro,
403+
match,
404+
mod,
405+
move,
406+
mut,
407+
offsetof,
408+
override,
409+
priv,
410+
proc,
411+
pub,
412+
pure,
413+
ref,
414+
return,
415+
self,
416+
sizeof,
417+
static,
418+
struct,
419+
super,
420+
trait,
421+
true,
422+
type,
423+
typeof,
424+
unsafe,
425+
unsized,
426+
use,
427+
virtual,
428+
where,
429+
while,
430+
yield,
431+
}
425432
}
426433
}
427434
}
428435
}
429436

430437
impl ToSanitizedPascalCase for str {
431438
fn to_sanitized_pascal_case(&self) -> Cow<str> {
432-
match self.chars().next().unwrap_or('\0') {
439+
let s = self.replace(BLACKLIST_CHARS, "");
440+
441+
match s.chars().next().unwrap_or('\0') {
433442
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
434-
Cow::from(format!("_{}", self.to_pascal_case()))
443+
Cow::from(format!("_{}", s.to_pascal_case()))
435444
}
436-
_ => Cow::from(self.to_pascal_case()),
445+
_ => Cow::from(s.to_pascal_case()),
437446
}
438447
}
439448
}
@@ -770,9 +779,11 @@ pub fn gen_register(r: &Register,
770779

771780
let field_name = Ident::new(&*field.name
772781
.to_sanitized_snake_case());
773-
let _field_name = Ident::new(&*format!("_{}",
774-
field.name
775-
.to_snake_case()));
782+
let _field_name =
783+
Ident::new(&*format!("_{}",
784+
field.name
785+
.replace(BLACKLIST_CHARS, "")
786+
.to_snake_case()));
776787
let width = field.bit_range.width;
777788
let mask = Lit::Int((1u64 << width) - 1, IntTy::Unsuffixed);
778789
let offset = Lit::Int(u64::from(field.bit_range.offset),
@@ -807,7 +818,9 @@ pub fn gen_register(r: &Register,
807818
if let Some(ev) = evs.values
808819
.iter()
809820
.find(|ev| ev.value == Some(i)) {
810-
let sc = Ident::new(&*ev.name.to_snake_case());
821+
let sc = Ident::new(&*ev.name
822+
.replace(BLACKLIST_CHARS, "")
823+
.to_snake_case());
811824
let doc = Cow::from(ev.description
812825
.clone()
813826
.unwrap_or_else(|| {

0 commit comments

Comments
 (0)