Skip to content

Commit 018515c

Browse files
authored
Merge pull request #276 from rust-embedded/prefix
prefix & suffix
2 parents 0f39ea9 + 055ff93 commit 018515c

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

CHANGELOG-rust.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This changelog tracks the Rust `svdtools` project. See
55

66
## [Unreleased]
77

8+
* Add `prefix` and `suffix` as opposite to `strip` and `strip_end`
9+
810
## [v0.4.3] 2025-01-31
911

1012
* Allow shorthand when `_derive` across clusters

src/patch/peripheral.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub(crate) trait PeripheralExt: InterruptExt + RegisterBlockExt {
3232
"_copy",
3333
"_strip",
3434
"_strip_end",
35+
"_prefix",
36+
"_suffix",
3537
"_modify",
3638
"_clear_fields",
3739
"_add",
@@ -57,6 +59,8 @@ pub(crate) trait ClusterExt: RegisterBlockExt {
5759
"_copy",
5860
"_strip",
5961
"_strip_end",
62+
"_prefix",
63+
"_suffix",
6064
"_modify",
6165
"_clear_fields",
6266
"_add",
@@ -568,6 +572,34 @@ pub(crate) trait RegisterBlockExt: Name {
568572
Ok(())
569573
}
570574

575+
/// Add prefix at the beginning of register names inside ptag
576+
fn add_prefix(&mut self, prefix: &str) -> PatchResult {
577+
for rtag in self.regs_mut() {
578+
rtag.name.insert_str(0, prefix);
579+
if let Some(dname) = rtag.display_name.as_mut() {
580+
dname.insert_str(0, prefix);
581+
}
582+
if let Some(name) = rtag.alternate_register.as_mut() {
583+
name.insert_str(0, prefix);
584+
}
585+
}
586+
Ok(())
587+
}
588+
589+
/// Add suffix at the ending of register names inside ptag
590+
fn add_suffix(&mut self, suffix: &str) -> PatchResult {
591+
for rtag in self.regs_mut() {
592+
rtag.name.push_str(suffix);
593+
if let Some(dname) = rtag.display_name.as_mut() {
594+
dname.push_str(suffix);
595+
}
596+
if let Some(name) = rtag.alternate_register.as_mut() {
597+
name.push_str(suffix);
598+
}
599+
}
600+
Ok(())
601+
}
602+
571603
/// Collect same registers in peripheral into register array
572604
fn collect_in_array(
573605
&mut self,
@@ -1078,6 +1110,15 @@ impl PeripheralExt for Peripheral {
10781110
.with_context(|| format!("Stripping suffix `{suffix}` from register names"))?;
10791111
}
10801112

1113+
if let Some(prefix) = pmod.get_str("_prefix")? {
1114+
self.add_prefix(prefix)
1115+
.with_context(|| format!("Adding prefix `{prefix}` to register names"))?;
1116+
}
1117+
if let Some(suffix) = pmod.get_str("_suffix")? {
1118+
self.add_suffix(suffix)
1119+
.with_context(|| format!("Adding suffix `{suffix}` to register names"))?;
1120+
}
1121+
10811122
// Handle modifications
10821123
for (rspec, rmod) in pmod.hash_iter("_modify") {
10831124
let rmod = rmod.hash()?;
@@ -1386,6 +1427,15 @@ impl ClusterExt for Cluster {
13861427
.with_context(|| format!("Stripping suffix `{suffix}` from register names"))?;
13871428
}
13881429

1430+
if let Some(prefix) = cmod.get_str("_prefix")? {
1431+
self.add_prefix(prefix)
1432+
.with_context(|| format!("Adding prefix `{prefix}` to register names"))?;
1433+
}
1434+
if let Some(suffix) = cmod.get_str("_suffix")? {
1435+
self.add_suffix(suffix)
1436+
.with_context(|| format!("Adding suffix `{suffix}` to register names"))?;
1437+
}
1438+
13891439
// Handle modifications
13901440
for (rspec, rmod) in cmod.hash_iter("_modify") {
13911441
let rmod = rmod.hash()?;

src/patch/register.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub trait RegisterExt {
4747
"_derive",
4848
"_strip",
4949
"_strip_end",
50+
"_prefix",
51+
"_suffix",
5052
"_clear",
5153
"_modify",
5254
"_add",
@@ -115,6 +117,12 @@ pub trait RegisterExt {
115117
/// Delete substring from the ending bitfield names inside rtag
116118
fn strip_end(&mut self, substr: &str) -> PatchResult;
117119

120+
/// Add prefix at the beginning of bitfield names inside rtag
121+
fn add_prefix(&mut self, prefix: &str) -> PatchResult;
122+
123+
/// Add suffix at the ending of bitfield names inside rtag
124+
fn add_suffix(&mut self, suffix: &str) -> PatchResult;
125+
118126
/// Modify fspec inside rtag according to fmod
119127
fn modify_field(&mut self, fspec: &str, fmod: &Hash, rpath: &RegisterPath) -> PatchResult;
120128

@@ -172,6 +180,15 @@ impl RegisterExt for Register {
172180
.with_context(|| format!("Stripping suffix `{suffix}` from field names"))?;
173181
}
174182

183+
if let Some(prefix) = rmod.get_str("_prefix")? {
184+
self.add_prefix(prefix)
185+
.with_context(|| format!("Adding prefix `{prefix}` to field names"))?;
186+
}
187+
if let Some(suffix) = rmod.get_str("_suffix")? {
188+
self.add_suffix(suffix)
189+
.with_context(|| format!("Adding suffix `{suffix}` to field names"))?;
190+
}
191+
175192
// Handle field clearing
176193
for fspec in rmod.str_vec_iter("_clear")? {
177194
self.clear_field(fspec)
@@ -288,6 +305,22 @@ impl RegisterExt for Register {
288305
Ok(())
289306
}
290307

308+
/// Add prefix at the beginning of bitfield names inside rtag
309+
fn add_prefix(&mut self, prefix: &str) -> PatchResult {
310+
for ftag in self.fields_mut() {
311+
ftag.name.insert_str(0, prefix);
312+
}
313+
Ok(())
314+
}
315+
316+
/// Add suffix at the ending of bitfield names inside rtag
317+
fn add_suffix(&mut self, suffix: &str) -> PatchResult {
318+
for ftag in self.fields_mut() {
319+
ftag.name.push_str(suffix);
320+
}
321+
Ok(())
322+
}
323+
291324
fn modify_field(&mut self, fspec: &str, fmod: &Hash, rpath: &RegisterPath) -> PatchResult {
292325
let (fspec, ignore) = fspec.spec();
293326
let ftags = self.iter_fields(fspec).collect::<Vec<_>>();

0 commit comments

Comments
 (0)