Skip to content

Commit 756e025

Browse files
committed
mods
1 parent 4b08b9b commit 756e025

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl Default for IdentFormats {
200200
"cluster_accessor".into(),
201201
IdentFormat::default().snake_case(),
202202
);
203-
//map.insert("cluster_mod".into(), IdentFormat::default().snake_case());
203+
map.insert("cluster_mod".into(), IdentFormat::default().snake_case());
204204
map.insert("register".into(), IdentFormat::default().pascal_case());
205205
map.insert(
206206
"register_spec".into(),
@@ -210,13 +210,13 @@ impl Default for IdentFormats {
210210
"register_accessor".into(),
211211
IdentFormat::default().snake_case(),
212212
);
213-
//map.insert("register_mod".into(), IdentFormat::default().snake_case());
213+
map.insert("register_mod".into(), IdentFormat::default().snake_case());
214214
map.insert("peripheral".into(), IdentFormat::default().pascal_case());
215215
map.insert(
216216
"peripheral_sigleton".into(),
217217
IdentFormat::default().snake_case(),
218218
);
219-
//map.insert("peripheral_mod".into(), IdentFormat::default().snake_case());
219+
map.insert("peripheral_mod".into(), IdentFormat::default().snake_case());
220220
map.insert(
221221
"peripheral_feature".into(),
222222
IdentFormat::default().snake_case(),

src/generate/peripheral.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use quote::{quote, ToTokens};
1616
use syn::{punctuated::Punctuated, Token};
1717

1818
use crate::util::{
19-
self, ident, name_to_ty, path_segment, type_path, unsuffixed, zst_type, FullName,
20-
ToSanitizedCase, BITS_PER_BYTE,
19+
self, ident, name_to_ty, path_segment, type_path, unsuffixed, zst_type, FullName, BITS_PER_BYTE,
2120
};
2221
use anyhow::{anyhow, bail, Context, Result};
2322

@@ -43,9 +42,13 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
4342
let address = util::hex(p.base_address + config.base_address_shift);
4443
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));
4544

46-
let mod_ty = name.to_snake_case_ident(span);
45+
let mod_ty = ident(&name, config, "peripheral_mod", span);
4746
let (derive_regs, base, path) = if let Some(path) = path {
48-
(true, path.peripheral.to_snake_case_ident(span), path)
47+
(
48+
true,
49+
ident(&path.peripheral, config, "peripheral_mod", span),
50+
path,
51+
)
4952
} else {
5053
(false, mod_ty.clone(), BlockPath::new(&p.name))
5154
};
@@ -1399,15 +1402,15 @@ fn cluster_block(
13991402

14001403
// name_snake_case needs to take into account array type.
14011404
let span = Span::call_site();
1402-
let mod_ty = mod_name.to_snake_case_ident(span);
1405+
let mod_ty = ident(&mod_name, config, "cluster_mod", span);
14031406
let block_ty = ident(&mod_name, &config, "cluster", span);
14041407

14051408
if let Some(dpath) = dpath {
14061409
let dparent = dpath.parent().unwrap();
14071410
let mut derived = if &dparent == path {
14081411
type_path(Punctuated::new())
14091412
} else {
1410-
util::block_path_to_ty(&dparent, span)
1413+
util::block_path_to_ty(&dparent, config, span)
14111414
};
14121415
let dname = util::replace_suffix(&index.clusters.get(&dpath).unwrap().name, "");
14131416
let mut mod_derived = derived.clone();
@@ -1418,7 +1421,7 @@ fn cluster_block(
14181421
mod_derived
14191422
.path
14201423
.segments
1421-
.push(path_segment(dname.to_snake_case_ident(span)));
1424+
.push(path_segment(ident(&dname, config, "cluster_mod", span)));
14221425

14231426
Ok(quote! {
14241427
#[doc = #description]

src/generate/register.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn render(
4646
let span = Span::call_site();
4747
let reg_ty = ident(&name, &config, "register", span);
4848
let doc_alias = (&reg_ty.to_string() != &name).then(|| quote!(#[doc(alias = #name)]));
49-
let mod_ty = name.to_snake_case_ident(span);
49+
let mod_ty = ident(&name, config, "register_mod", span);
5050
let description = util::escape_special_chars(
5151
util::respace(&register.description.clone().unwrap_or_else(|| {
5252
warn!("Missing description for register {}", register.name);
@@ -59,7 +59,7 @@ pub fn render(
5959
let mut derived = if &dpath.block == path {
6060
type_path(Punctuated::new())
6161
} else {
62-
util::block_path_to_ty(&dpath.block, span)
62+
util::block_path_to_ty(&dpath.block, config, span)
6363
};
6464
let dname = util::name_of(index.registers.get(dpath).unwrap(), config.ignore_groups);
6565
let mut mod_derived = derived.clone();
@@ -70,7 +70,7 @@ pub fn render(
7070
mod_derived
7171
.path
7272
.segments
73-
.push(path_segment(dname.to_snake_case_ident(span)));
73+
.push(path_segment(ident(&dname, config, "register_mod", span)));
7474

7575
Ok(quote! {
7676
pub use #derived as #reg_ty;
@@ -203,7 +203,7 @@ pub fn render_register_mod(
203203
let name = util::name_of(register, config.ignore_groups);
204204
let span = Span::call_site();
205205
let regspec_ty = regspec(&name, config, span);
206-
let name_snake_case = name.to_snake_case_ident(span);
206+
let mod_ty = ident(&name, config, "regster_mod", span);
207207
let rsize = properties
208208
.size
209209
.ok_or_else(|| anyhow!("Register {} has no `size` field", register.name))?;
@@ -386,7 +386,7 @@ pub fn render_register_mod(
386386
can_read,
387387
can_write,
388388
can_reset,
389-
&name_snake_case,
389+
&mod_ty,
390390
true,
391391
register.read_action,
392392
)?
@@ -402,15 +402,14 @@ pub fn render_register_mod(
402402
});
403403

404404
if can_read {
405-
let doc = format!("`read()` method returns [`{name_snake_case}::R`](R) reader structure",);
405+
let doc = format!("`read()` method returns [`{mod_ty}::R`](R) reader structure",);
406406
mod_items.extend(quote! {
407407
#[doc = #doc]
408408
impl crate::Readable for #regspec_ty {}
409409
});
410410
}
411411
if can_write {
412-
let doc =
413-
format!("`write(|w| ..)` method takes [`{name_snake_case}::W`](W) writer structure",);
412+
let doc = format!("`write(|w| ..)` method takes [`{mod_ty}::W`](W) writer structure",);
414413

415414
let zero_to_modify_fields_bitmap = util::hex(zero_to_modify_fields_bitmap);
416415
let one_to_modify_fields_bitmap = util::hex(one_to_modify_fields_bitmap);
@@ -865,7 +864,7 @@ pub fn fields(
865864
let base_field = util::replace_suffix(&base.field.name, "");
866865
let base_r = ident(&base_field, &config, "field_reader", span);
867866
if !reader_derives.contains(&reader_ty) {
868-
let base_path = base_syn_path(base, &fpath, &base_r)?;
867+
let base_path = base_syn_path(base, &fpath, &base_r, config)?;
869868
mod_items.extend(quote! {
870869
#[doc = #field_reader_brief]
871870
pub use #base_path as #reader_ty;
@@ -877,7 +876,7 @@ pub fn fields(
877876
if base.register() != fpath.register() {
878877
// use the same enum structure name
879878
if !enum_derives.contains(&value_read_ty) {
880-
let base_path = base_syn_path(base, &fpath, &value_read_ty)?;
879+
let base_path = base_syn_path(base, &fpath, &value_read_ty, config)?;
881880
mod_items.extend(quote! {
882881
#[doc = #description]
883882
pub use #base_path as #value_read_ty;
@@ -1140,7 +1139,7 @@ pub fn fields(
11401139
let base_field = util::replace_suffix(&base.field.name, "");
11411140
let base_w = ident(&base_field, &config, "field_writer", span);
11421141
if !writer_derives.contains(&writer_ty) {
1143-
let base_path = base_syn_path(base, &fpath, &base_w)?;
1142+
let base_path = base_syn_path(base, &fpath, &base_w, config)?;
11441143
mod_items.extend(quote! {
11451144
#[doc = #field_writer_brief]
11461145
pub use #base_path as #writer_ty;
@@ -1153,7 +1152,7 @@ pub fn fields(
11531152
if writer_reader_different_enum {
11541153
// use the same enum structure name
11551154
if !writer_enum_derives.contains(&value_write_ty) {
1156-
let base_path = base_syn_path(base, &fpath, &value_write_ty)?;
1155+
let base_path = base_syn_path(base, &fpath, &value_write_ty, config)?;
11571156
mod_items.extend(quote! {
11581157
#[doc = #description]
11591158
pub use #base_path as #value_write_ty;
@@ -1458,18 +1457,24 @@ fn base_syn_path(
14581457
base: &EnumPath,
14591458
fpath: &FieldPath,
14601459
base_ident: &Ident,
1460+
config: &Config,
14611461
) -> Result<syn::TypePath, syn::Error> {
14621462
let span = Span::call_site();
14631463
let path = if base.register() == fpath.register() {
14641464
ident_to_path(base_ident.clone())
14651465
} else if base.register().block == fpath.register().block {
14661466
let mut segments = Punctuated::new();
14671467
segments.push(path_segment(Ident::new("super", span)));
1468-
segments.push(path_segment(base.register().name.to_snake_case_ident(span)));
1468+
segments.push(path_segment(ident(
1469+
&base.register().name,
1470+
config,
1471+
"register_mod",
1472+
span,
1473+
)));
14691474
segments.push(path_segment(base_ident.clone()));
14701475
type_path(segments)
14711476
} else {
1472-
let mut rmod_ = crate::util::register_path_to_ty(base.register(), span);
1477+
let mut rmod_ = crate::util::register_path_to_ty(base.register(), config, span);
14731478
rmod_.path.segments.push(path_segment(base_ident.clone()));
14741479
rmod_
14751480
};

src/util.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,21 +341,37 @@ pub fn name_to_ty(name: Ident) -> Type {
341341
syn::Type::Path(type_path(segments))
342342
}
343343

344-
pub fn block_path_to_ty(bpath: &svd_parser::expand::BlockPath, span: Span) -> TypePath {
344+
pub fn block_path_to_ty(
345+
bpath: &svd_parser::expand::BlockPath,
346+
config: &Config,
347+
span: Span,
348+
) -> TypePath {
345349
let mut segments = Punctuated::new();
346350
segments.push(path_segment(Ident::new("crate", span)));
347-
segments.push(path_segment(bpath.peripheral.to_snake_case_ident(span)));
351+
segments.push(path_segment(ident(
352+
&bpath.peripheral,
353+
config,
354+
"peripheral_mod",
355+
span,
356+
)));
348357
for ps in &bpath.path {
349-
segments.push(path_segment(ps.to_snake_case_ident(span)));
358+
segments.push(path_segment(ident(&ps, config, "cluster_mod", span)));
350359
}
351360
type_path(segments)
352361
}
353362

354-
pub fn register_path_to_ty(rpath: &svd_parser::expand::RegisterPath, span: Span) -> TypePath {
355-
let mut p = block_path_to_ty(&rpath.block, span);
356-
p.path
357-
.segments
358-
.push(path_segment(rpath.name.to_snake_case_ident(span)));
363+
pub fn register_path_to_ty(
364+
rpath: &svd_parser::expand::RegisterPath,
365+
config: &Config,
366+
span: Span,
367+
) -> TypePath {
368+
let mut p = block_path_to_ty(&rpath.block, config, span);
369+
p.path.segments.push(path_segment(ident(
370+
&rpath.name,
371+
config,
372+
"register_mod",
373+
span,
374+
)));
359375
p
360376
}
361377

0 commit comments

Comments
 (0)