Skip to content

Commit e6ec471

Browse files
committed
change case defaults, fix bugs
1 parent 869d34f commit e6ec471

File tree

6 files changed

+151
-101
lines changed

6 files changed

+151
-101
lines changed

src/config.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
33

44
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
55
#[derive(Clone, PartialEq, Eq, Debug, Default)]
6+
#[non_exhaustive]
67
pub struct Config {
78
pub target: Target,
89
pub atomics: bool,
@@ -13,7 +14,6 @@ pub struct Config {
1314
pub ignore_groups: bool,
1415
pub keep_list: bool,
1516
pub strict: bool,
16-
pub pascal_enum_values: bool,
1717
pub feature_group: bool,
1818
pub feature_peripheral: bool,
1919
pub max_cluster_size: bool,
@@ -135,6 +135,7 @@ pub enum Case {
135135
#[derive(Clone, Debug, Default, PartialEq, Eq)]
136136
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
137137
pub struct IdentFormat {
138+
// Ident case. `None` means don't change
138139
pub case: Option<Case>,
139140
pub prefix: String,
140141
pub suffix: String,
@@ -153,8 +154,8 @@ impl IdentFormat {
153154
self.case = Some(Case::Pascal);
154155
self
155156
}
156-
pub fn scake_case(mut self) -> Self {
157-
self.case = Some(Case::Pascal);
157+
pub fn snake_case(mut self) -> Self {
158+
self.case = Some(Case::Snake);
158159
self
159160
}
160161
pub fn prefix(mut self, prefix: &str) -> Self {
@@ -177,24 +178,38 @@ pub struct IdentFormats {
177178
pub enum_value: IdentFormat,
178179
pub interrupt: IdentFormat,
179180
pub cluster: IdentFormat,
181+
pub cluster_accessor: IdentFormat,
182+
//pub cluster_mod: IdentFormat,
180183
pub register: IdentFormat,
181184
pub register_spec: IdentFormat,
185+
pub register_accessor: IdentFormat,
186+
//pub register_mod: IdentFormat,
182187
pub peripheral: IdentFormat,
188+
pub peripheral_sigleton: IdentFormat,
189+
//pub peripheral_mod: IdentFormat,
190+
pub peripheral_feature: IdentFormat,
183191
}
184192

185193
impl Default for IdentFormats {
186194
fn default() -> Self {
187195
Self {
188-
field_reader: IdentFormat::default().constant_case().suffix("_R"),
189-
field_writer: IdentFormat::default().constant_case().suffix("_W"),
190-
enum_name: IdentFormat::default().constant_case().suffix("_A"),
191-
enum_write_name: IdentFormat::default().constant_case().suffix("_AW"),
192-
enum_value: IdentFormat::default().constant_case(),
193-
interrupt: IdentFormat::default().constant_case(),
194-
cluster: IdentFormat::default().constant_case(),
195-
register: IdentFormat::default().constant_case(),
196-
register_spec: IdentFormat::default().constant_case().suffix("_SPEC"),
197-
peripheral: IdentFormat::default().constant_case(),
196+
field_reader: IdentFormat::default().pascal_case().suffix("R"),
197+
field_writer: IdentFormat::default().pascal_case().suffix("W"),
198+
enum_name: IdentFormat::default().pascal_case().suffix("A"),
199+
enum_write_name: IdentFormat::default().pascal_case().suffix("AW"),
200+
enum_value: IdentFormat::default().pascal_case(),
201+
interrupt: IdentFormat::default(),
202+
cluster: IdentFormat::default().pascal_case(),
203+
cluster_accessor: IdentFormat::default().snake_case(),
204+
//cluster_mod: IdentFormat::default().snake_case(),
205+
register: IdentFormat::default().pascal_case(),
206+
register_spec: IdentFormat::default().pascal_case().suffix("Spec"),
207+
register_accessor: IdentFormat::default().snake_case(),
208+
//register_mod: IdentFormat::default().snake_case(),
209+
peripheral: IdentFormat::default().pascal_case(),
210+
peripheral_sigleton: IdentFormat::default().snake_case(),
211+
//peripheral_mod: IdentFormat::default().snake_case(),
212+
peripheral_feature: IdentFormat::default().snake_case(),
198213
}
199214
}
200215
}

src/generate/device.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::io::Write;
88
use std::path::Path;
99

1010
use crate::config::{Config, Target};
11-
use crate::util::{self, ident, ToSanitizedCase};
11+
use crate::util::{self, ident};
1212
use anyhow::{Context, Result};
1313

1414
use crate::generate::{interrupt, peripheral};
@@ -226,39 +226,49 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
226226
}
227227
let mut feature_attribute = TokenStream::new();
228228
if config.feature_group && p.group_name.is_some() {
229-
let feature_name = p.group_name.as_ref().unwrap().to_sanitized_snake_case();
229+
let feature_name = config
230+
.ident_formats
231+
.peripheral_feature
232+
.apply(p.group_name.as_deref().unwrap());
230233
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] })
231234
};
232235

233236
let span = Span::call_site();
234237
match p {
235238
Peripheral::Single(_p) => {
236239
let p_name = util::name_of(p, config.ignore_groups);
237-
let p_snake = p_name.to_sanitized_snake_case();
240+
let p_feature = config.ident_formats.peripheral_feature.apply(&p_name);
238241
let p_ty = ident(&p_name, &config.ident_formats.peripheral, span);
242+
let p_singleton = ident(&p_name, &config.ident_formats.peripheral_sigleton, span);
239243
if config.feature_peripheral {
240-
feature_attribute.extend(quote! { #[cfg(feature = #p_snake)] })
244+
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
241245
};
242246
fields.extend(quote! {
243247
#[doc = #p_name]
244248
#feature_attribute
245-
pub #p_ty: #p_ty,
249+
pub #p_singleton: #p_ty,
246250
});
247-
exprs.extend(quote!(#feature_attribute #p_ty: #p_ty { _marker: PhantomData },));
251+
exprs.extend(
252+
quote!(#feature_attribute #p_singleton: #p_ty { _marker: PhantomData },),
253+
);
248254
}
249255
Peripheral::Array(p, dim_element) => {
250256
for p_name in names(p, dim_element) {
251-
let p_snake = p_name.to_sanitized_snake_case();
257+
let p_feature = config.ident_formats.peripheral_feature.apply(&p_name);
252258
let p_ty = ident(&p_name, &config.ident_formats.peripheral, span);
259+
let p_singleton =
260+
ident(&p_name, &config.ident_formats.peripheral_sigleton, span);
253261
if config.feature_peripheral {
254-
feature_attribute.extend(quote! { #[cfg(feature = #p_snake)] })
262+
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
255263
};
256264
fields.extend(quote! {
257265
#[doc = #p_name]
258266
#feature_attribute
259-
pub #p_ty: #p_ty,
267+
pub #p_singleton: #p_ty,
260268
});
261-
exprs.extend(quote!(#feature_attribute #p_ty: #p_ty { _marker: PhantomData },));
269+
exprs.extend(
270+
quote!(#feature_attribute #p_singleton: #p_ty { _marker: PhantomData },),
271+
);
262272
}
263273
}
264274
}

src/generate/peripheral.rs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
7777

7878
match &p {
7979
Peripheral::Array(p, dim) => {
80-
let mut snake_names = Vec::with_capacity(dim.dim as _);
80+
let mut feature_names = Vec::with_capacity(dim.dim as _);
8181
for pi in svd::peripheral::expand(p, dim) {
8282
let name = &pi.name;
8383
let description = pi.description.as_deref().unwrap_or(&p.name);
8484
let p_ty = ident(name, &config.ident_formats.peripheral, span);
8585
let name_str = p_ty.to_string();
8686
let address = util::hex(pi.base_address + config.base_address_shift);
87-
let p_snake = name.to_sanitized_snake_case();
88-
snake_names.push(p_snake.to_string());
87+
let feature_name = config.ident_formats.peripheral_feature.apply(name);
88+
feature_names.push(feature_name.to_string());
8989
let mut feature_attribute_n = feature_attribute.clone();
9090
if config.feature_peripheral {
91-
feature_attribute_n.extend(quote! { #[cfg(feature = #p_snake)] })
91+
feature_attribute_n.extend(quote! { #[cfg(feature = #feature_name)] })
9292
};
9393
// Insert the peripherals structure
9494
out.extend(quote! {
@@ -132,7 +132,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
132132
});
133133
}
134134

135-
let feature_any_attribute = quote! {#[cfg(any(#(feature = #snake_names),*))]};
135+
let feature_any_attribute = quote! {#[cfg(any(#(feature = #feature_names),*))]};
136136

137137
// Derived peripherals may not require re-implementation, and will instead
138138
// use a single definition of the non-derived version.
@@ -147,9 +147,9 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
147147
}
148148
}
149149
Peripheral::Single(_) => {
150-
let p_snake = name.to_sanitized_snake_case();
150+
let feature_name = config.ident_formats.peripheral_feature.apply(&name);
151151
if config.feature_peripheral {
152-
feature_attribute.extend(quote! { #[cfg(feature = #p_snake)] })
152+
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] })
153153
};
154154
// Insert the peripheral structure
155155
out.extend(quote! {
@@ -981,12 +981,13 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
981981
} else {
982982
util::replace_suffix(&cluster.name, "")
983983
};
984-
let ty = name_to_ty(&ty_name);
984+
let span = Span::call_site();
985+
let ty = name_to_ty(ident(&ty_name, &config.ident_formats.cluster, span));
985986

986987
match cluster {
987988
Cluster::Single(info) => {
988989
let doc = make_comment(cluster_size, info.address_offset, &description);
989-
let name: Ident = info.name.to_snake_case_ident(Span::call_site());
990+
let name: Ident = ident(&info.name, &config.ident_formats.cluster_accessor, span);
990991
let syn_field = new_syn_field(name.clone(), ty.clone());
991992
cluster_expanded.push(RegisterBlockField {
992993
syn_field,
@@ -1029,12 +1030,15 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10291030
let array_convertible = sequential_addresses && convert_list;
10301031

10311032
if convert_list {
1032-
let span = Span::call_site();
1033-
let nb_name_sc = if let Some(dim_name) = array_info.dim_name.as_ref() {
1034-
dim_name.to_snake_case_ident(span)
1035-
} else {
1036-
ty_name.to_snake_case_ident(span)
1037-
};
1033+
let accessor_name = ident(
1034+
if let Some(dim_name) = array_info.dim_name.as_ref() {
1035+
dim_name
1036+
} else {
1037+
&ty_name
1038+
},
1039+
&config.ident_formats.cluster_accessor,
1040+
span,
1041+
);
10381042
let doc = make_comment(
10391043
cluster_size * array_info.dim,
10401044
info.address_offset,
@@ -1044,7 +1048,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10441048
accessors.push(if array_convertible {
10451049
ArrayAccessor {
10461050
doc,
1047-
name: nb_name_sc.clone(),
1051+
name: accessor_name.clone(),
10481052
ty: ty.clone(),
10491053
offset: unsuffixed(info.address_offset),
10501054
dim: unsuffixed(array_info.dim),
@@ -1054,7 +1058,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10541058
} else {
10551059
RawArrayAccessor {
10561060
doc,
1057-
name: nb_name_sc.clone(),
1061+
name: accessor_name.clone(),
10581062
ty: ty.clone(),
10591063
offset: unsuffixed(info.address_offset),
10601064
dim: unsuffixed(array_info.dim),
@@ -1076,7 +1080,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10761080
doc,
10771081
name: idx_name,
10781082
ty: ty.clone(),
1079-
basename: nb_name_sc.clone(),
1083+
basename: accessor_name.clone(),
10801084
i,
10811085
}
10821086
.into(),
@@ -1088,7 +1092,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10881092
} else {
10891093
zst_type()
10901094
};
1091-
let syn_field = new_syn_field(nb_name_sc, array_ty);
1095+
let syn_field = new_syn_field(accessor_name, array_ty);
10921096
cluster_expanded.push(RegisterBlockField {
10931097
syn_field,
10941098
offset: info.address_offset,
@@ -1106,7 +1110,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
11061110
ci.address_offset,
11071111
ci.description.as_deref().unwrap_or(&ci.name),
11081112
);
1109-
let name = ci.name.to_snake_case_ident(Span::call_site());
1113+
let name = ident(&ci.name, &config.ident_formats.cluster_accessor, span);
11101114
let syn_field = new_syn_field(name.clone(), ty.clone());
11111115

11121116
cluster_expanded.push(RegisterBlockField {
@@ -1155,8 +1159,9 @@ fn expand_register(
11551159
match register {
11561160
Register::Single(info) => {
11571161
let doc = make_comment(register_size, info.address_offset, &description);
1158-
let ty = name_to_ty(&ty_name);
1159-
let name = ty_name.to_snake_case_ident(Span::call_site());
1162+
let span = Span::call_site();
1163+
let ty = name_to_ty(ident(&ty_name, &config.ident_formats.register, span));
1164+
let name: Ident = ident(&ty_name, &config.ident_formats.register_accessor, span);
11601165
let syn_field = new_syn_field(name.clone(), ty.clone());
11611166
register_expanded.push(RegisterBlockField {
11621167
syn_field,
@@ -1210,15 +1215,18 @@ fn expand_register(
12101215
};
12111216
let array_convertible = ac && sequential_addresses;
12121217
let array_proxy_convertible = ac && disjoint_sequential_addresses;
1213-
let ty = name_to_ty(&ty_name);
1218+
let span = Span::call_site();
1219+
let ty = name_to_ty(ident(&ty_name, &config.ident_formats.register, span));
12141220

12151221
if array_convertible || array_proxy_convertible {
1216-
let span = Span::call_site();
1217-
let nb_name_sc = if let Some(dim_name) = array_info.dim_name.as_ref() {
1218-
util::fullname(dim_name, &info.alternate_group, config.ignore_groups)
1219-
.to_snake_case_ident(span)
1222+
let accessor_name = if let Some(dim_name) = array_info.dim_name.as_ref() {
1223+
ident(
1224+
&util::fullname(dim_name, &info.alternate_group, config.ignore_groups),
1225+
&config.ident_formats.register,
1226+
span,
1227+
)
12201228
} else {
1221-
ty_name.to_snake_case_ident(span)
1229+
ident(&ty_name, &config.ident_formats.register, span)
12221230
};
12231231
let doc = make_comment(
12241232
register_size * array_info.dim,
@@ -1229,7 +1237,7 @@ fn expand_register(
12291237
accessors.push(if array_convertible {
12301238
ArrayAccessor {
12311239
doc,
1232-
name: nb_name_sc.clone(),
1240+
name: accessor_name.clone(),
12331241
ty: ty.clone(),
12341242
offset: unsuffixed(info.address_offset),
12351243
dim: unsuffixed(array_info.dim),
@@ -1239,7 +1247,7 @@ fn expand_register(
12391247
} else {
12401248
RawArrayAccessor {
12411249
doc,
1242-
name: nb_name_sc.clone(),
1250+
name: accessor_name.clone(),
12431251
ty: ty.clone(),
12441252
offset: unsuffixed(info.address_offset),
12451253
dim: unsuffixed(array_info.dim),
@@ -1263,7 +1271,7 @@ fn expand_register(
12631271
doc,
12641272
name: idx_name,
12651273
ty: ty.clone(),
1266-
basename: nb_name_sc.clone(),
1274+
basename: accessor_name.clone(),
12671275
i,
12681276
}
12691277
.into(),
@@ -1275,7 +1283,7 @@ fn expand_register(
12751283
} else {
12761284
zst_type()
12771285
};
1278-
let syn_field = new_syn_field(nb_name_sc, array_ty);
1286+
let syn_field = new_syn_field(accessor_name, array_ty);
12791287
register_expanded.push(RegisterBlockField {
12801288
syn_field,
12811289
offset: info.address_offset,
@@ -1293,7 +1301,7 @@ fn expand_register(
12931301
info.address_offset,
12941302
ri.description.as_deref().unwrap_or(&ri.name),
12951303
);
1296-
let name = ri.name.to_snake_case_ident(Span::call_site());
1304+
let name = ident(&ri.name, &config.ident_formats.register, span);
12971305
let syn_field = new_syn_field(name.clone(), ty.clone());
12981306

12991307
register_expanded.push(RegisterBlockField {

0 commit comments

Comments
 (0)