Skip to content

Commit d3fb281

Browse files
committed
Normalize SVD names to upper case where they were to pascal case.
Fixes #76.
1 parent c758eb7 commit d3fb281

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cast = "0.2.0"
1414
clap = "2.20.5"
1515
either = "1.0.3"
1616
error-chain = "0.10.0"
17-
inflections = "1.0.0"
17+
inflections = { git = "https://github.com/whitequark/inflections" }
1818
quote = "0.3.15"
1919
svd-parser = "0.5.1"
2020
syn = "0.11.9"

src/generate.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use svd::{Access, BitRange, Defaults, Device, EnumeratedValues, Field,
99
use syn::{Ident, Lit};
1010

1111
use errors::*;
12-
use util::{self, ToSanitizedPascalCase, ToSanitizedSnakeCase, U32Ext};
12+
use util::{self, ToSanitizedPascalCase, ToSanitizedSnakeCase,
13+
ToSanitizedUpperCase, U32Ext};
1314

1415
/// Whole device generation
1516
pub fn device(d: &Device, items: &mut Vec<Tokens>) -> Result<()> {
@@ -24,6 +25,7 @@ pub fn device(d: &Device, items: &mut Vec<Tokens>) -> Result<()> {
2425
#![doc = #doc]
2526
#![deny(missing_docs)]
2627
#![deny(warnings)]
28+
#![allow(non_camel_case_types)]
2729
#![feature(const_fn)]
2830
#![no_std]
2931

@@ -127,7 +129,7 @@ pub fn interrupt(peripherals: &[Peripheral], items: &mut Vec<Tokens>) {
127129
);
128130
}
129131

130-
let name_pc = Ident::new(interrupt.name.to_sanitized_pascal_case());
132+
let name_pc = Ident::new(interrupt.name.to_sanitized_upper_case());
131133
let description = format!(
132134
"{} - {}",
133135
interrupt.value,
@@ -237,7 +239,7 @@ pub fn peripheral(
237239
defaults: &Defaults,
238240
) -> Result<()> {
239241
let name = Ident::new(&*p.name.to_uppercase());
240-
let name_pc = Ident::new(&*p.name.to_sanitized_pascal_case());
242+
let name_pc = Ident::new(&*p.name.to_sanitized_upper_case());
241243
let address = util::unsuffixed(u64(p.base_address));
242244
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));
243245

@@ -413,7 +415,7 @@ pub fn register(
413415
) -> Result<()> {
414416
let access = util::access_of(register);
415417
let name = util::name_of(register);
416-
let name_pc = Ident::new(&*name.to_sanitized_pascal_case());
418+
let name_pc = Ident::new(&*name.to_sanitized_upper_case());
417419
let name_sc = Ident::new(&*name.to_sanitized_snake_case());
418420
let rsize = register.size
419421
.or(defs.size)
@@ -637,7 +639,7 @@ pub fn fields(
637639
fn from(f: &'a Field) -> Result<Self> {
638640
let BitRange { offset, width } = f.bit_range;
639641
let sc = f.name.to_sanitized_snake_case();
640-
let pc = f.name.to_sanitized_pascal_case();
642+
let pc = f.name.to_sanitized_upper_case();
641643
let pc_r = Ident::new(&*format!("{}R", pc));
642644
let pc_w = Ident::new(&*format!("{}W", pc));
643645
let _pc_w = Ident::new(&*format!("_{}W", pc));
@@ -726,15 +728,15 @@ pub fn fields(
726728
description: description,
727729
sc: sc,
728730
pc: Ident::new(&*ev.name
729-
.to_sanitized_pascal_case()),
731+
.to_sanitized_upper_case()),
730732
value: value,
731733
})
732734
})
733735
.collect::<Result<Vec<_>>>()?;
734736

735737
let pc_r = &f.pc_r;
736738
if let Some(ref base) = base {
737-
let pc = base.field.to_sanitized_pascal_case();
739+
let pc = base.field.to_sanitized_upper_case();
738740
let base_pc_r = Ident::new(&*format!("{}R", pc));
739741
let description =
740742
format!("Possible values of the field `{}`", f.name);
@@ -988,7 +990,7 @@ pub fn fields(
988990
let base_pc_w = base.as_ref()
989991
.map(
990992
|base| {
991-
let pc = base.field.to_sanitized_pascal_case();
993+
let pc = base.field.to_sanitized_upper_case();
992994
let base_pc_w = Ident::new(&*format!("{}W", pc));
993995

994996
if let Some(ref register) = base.register {
@@ -1036,7 +1038,7 @@ pub fn fields(
10361038
format!("`{:b}`", value)
10371039
}),
10381040
pc: Ident::new(&*ev.name
1039-
.to_sanitized_pascal_case()),
1041+
.to_sanitized_upper_case()),
10401042
sc: Ident::new(&*ev.name
10411043
.to_sanitized_snake_case()),
10421044
value: value,

src/util.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub trait ToSanitizedPascalCase {
1717
fn to_sanitized_pascal_case(&self) -> Cow<str>;
1818
}
1919

20+
pub trait ToSanitizedUpperCase {
21+
fn to_sanitized_upper_case(&self) -> Cow<str>;
22+
}
23+
2024
pub trait ToSanitizedSnakeCase {
2125
fn to_sanitized_snake_case(&self) -> Cow<str>;
2226
}
@@ -98,6 +102,19 @@ impl ToSanitizedSnakeCase for str {
98102
}
99103
}
100104

105+
impl ToSanitizedUpperCase for str {
106+
fn to_sanitized_upper_case(&self) -> Cow<str> {
107+
let s = self.replace(BLACKLIST_CHARS, "");
108+
109+
match s.chars().next().unwrap_or('\0') {
110+
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
111+
Cow::from(format!("_{}", s.to_upper_case()))
112+
}
113+
_ => Cow::from(s.to_upper_case()),
114+
}
115+
}
116+
}
117+
101118
impl ToSanitizedPascalCase for str {
102119
fn to_sanitized_pascal_case(&self) -> Cow<str> {
103120
let s = self.replace(BLACKLIST_CHARS, "");
@@ -138,7 +155,7 @@ pub fn expand(registers: &[Register]) -> Vec<ExpandedRegister> {
138155
offset: info.address_offset,
139156
ty: Either::Left(
140157
info.name
141-
.to_sanitized_pascal_case()
158+
.to_sanitized_upper_case()
142159
.into_owned(),
143160
),
144161
},
@@ -153,7 +170,7 @@ pub fn expand(registers: &[Register]) -> Vec<ExpandedRegister> {
153170
info.name.replace("%s", "")
154171
};
155172

156-
let ty = Rc::new(ty.to_sanitized_pascal_case().into_owned());
173+
let ty = Rc::new(ty.to_sanitized_upper_case().into_owned());
157174

158175
let indices = array_info
159176
.dim_index

0 commit comments

Comments
 (0)