Skip to content

Commit 5cb51e6

Browse files
committed
desription regex replace
1 parent cb60c01 commit 5cb51e6

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed

Diff for: CHANGELOG-rust.md

+2
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+
* `regex` replacements for descriptions during `_modify`
9+
810
## [v0.3.19] 2024-10-18
911

1012
* Fix deletion childrens on cluster modify

Diff for: src/patch/device.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{fs::File, io::Read, path::Path};
99
use super::iterators::{MatchIter, Matched};
1010
use super::peripheral::{PeripheralExt, RegisterBlockExt};
1111
use super::yaml_ext::{AsType, GetVal};
12-
use super::{abspath, matchname, Config, PatchResult, Spec, VAL_LVL};
12+
use super::{abspath, do_replacements, matchname, Config, PatchResult, Spec, VAL_LVL};
1313
use super::{make_address_block, make_address_blocks, make_cpu, make_interrupt, make_peripheral};
1414
use super::{make_dim_element, modify_dim_element, modify_register_properties};
1515

@@ -255,6 +255,10 @@ impl DeviceExt for Device {
255255

256256
modify_dim_element(ptag, &dim)?;
257257
ptag.modify_from(peripheral_builder.clone(), VAL_LVL)?;
258+
if let Ok(Some(h)) = pmod.get_hash("description") {
259+
ptag.description =
260+
Some(do_replacements(ptag.description.as_deref().unwrap_or(""), h)?.into());
261+
}
258262
if let Some(ints) = pmod.get_hash("interrupts")? {
259263
for (iname, val) in ints {
260264
let iname = iname.str()?;

Diff for: src/patch/mod.rs

+38-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod patch_cli;
22

3+
use regex::Regex;
34
use std::borrow::Cow;
45
use std::fs::File;
56
use std::io::{Read, Write};
@@ -476,7 +477,10 @@ fn modify_dim_element<T: Clone>(
476477

477478
fn make_field(fadd: &Hash, rpath: Option<&RegisterPath>) -> Result<FieldInfoBuilder> {
478479
let mut fnew = FieldInfo::builder()
479-
.description(opt_interpolate(&rpath, fadd.get_str("description")?))
480+
.description(opt_interpolate(
481+
&rpath,
482+
fadd.get_str("description").unwrap_or(None),
483+
))
480484
.derived_from(opt_interpolate(&rpath, fadd.get_str("derivedFrom")?))
481485
.access(fadd.get_str("access")?.and_then(Access::parse_str))
482486
.modified_write_values(
@@ -509,7 +513,10 @@ fn make_field(fadd: &Hash, rpath: Option<&RegisterPath>) -> Result<FieldInfoBuil
509513
fn make_register(radd: &Hash, path: Option<&BlockPath>) -> Result<RegisterInfoBuilder> {
510514
let mut rnew = RegisterInfo::builder()
511515
.display_name(radd.get_string("displayName")?)
512-
.description(opt_interpolate(&path, radd.get_str("description")?))
516+
.description(opt_interpolate(
517+
&path,
518+
radd.get_str("description").unwrap_or(None),
519+
))
513520
.derived_from(opt_interpolate(&path, radd.get_str("derivedFrom")?))
514521
.alternate_group(radd.get_string("alternateGroup")?)
515522
.alternate_register(radd.get_string("alternateRegister")?)
@@ -576,7 +583,10 @@ fn get_write_constraint(h: &Hash) -> Result<Option<WriteConstraint>> {
576583

577584
fn make_cluster(cadd: &Hash, path: Option<&BlockPath>) -> Result<ClusterInfoBuilder> {
578585
let mut cnew = ClusterInfo::builder()
579-
.description(opt_interpolate(&path, cadd.get_str("description")?))
586+
.description(opt_interpolate(
587+
&path,
588+
cadd.get_str("description").unwrap_or(None),
589+
))
580590
.derived_from(opt_interpolate(&path, cadd.get_str("derivedFrom")?))
581591
.default_register_properties(get_register_properties(cadd)?);
582592

@@ -622,7 +632,7 @@ fn make_peripheral(padd: &Hash, modify: bool) -> Result<PeripheralInfoBuilder> {
622632
let mut pnew = PeripheralInfo::builder()
623633
.display_name(padd.get_string("displayName")?)
624634
.version(padd.get_string("version")?)
625-
.description(padd.get_string("description")?)
635+
.description(padd.get_string("description").unwrap_or(None))
626636
.derived_from(padd.get_string("derivedFrom")?)
627637
.group_name(padd.get_string("groupName")?)
628638
.interrupt(if !modify {
@@ -767,6 +777,30 @@ fn check_offsets(offsets: &[u32], dim_increment: u32) -> bool {
767777
true
768778
}
769779

780+
fn do_replacements<'a>(s: &'a str, h: &Hash) -> Result<Cow<'a, str>> {
781+
let mut curr = Cow::Borrowed(s);
782+
for (pat, rep) in h {
783+
let pat = pat.str()?;
784+
let re = Regex::new(pat).unwrap();
785+
match rep {
786+
Yaml::String(rep) => {
787+
curr = re.replace_all(curr.as_ref(), rep).to_string().into();
788+
}
789+
Yaml::Array(v) if v.len() == 2 => {
790+
let n = v[0].i64()?;
791+
let rep = v[1].str()?;
792+
curr = re.replacen(curr.as_ref(), n as _, rep).to_string().into();
793+
}
794+
_ => {
795+
return Err(anyhow!(
796+
"Replacer should be string or number of replacements and string"
797+
))
798+
}
799+
}
800+
}
801+
Ok(curr)
802+
}
803+
770804
/// Tries to get common description (or displayNames) for register/field array with "%s" in index position.
771805
/// Returns `None` if incoming descriptions have more then 1 difference
772806
fn common_description(descs: &[Option<&str>], dim_index: &[String]) -> Option<Option<String>> {

Diff for: src/patch/peripheral.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{anyhow, Context, Ok};
1+
use anyhow::{anyhow, Context};
22
use itertools::Itertools;
33
use svd::Name;
44
use svd_parser::expand::BlockPath;
@@ -12,7 +12,7 @@ use super::iterators::{MatchIter, Matched};
1212
use super::register::{RegisterExt, RegisterInfoExt};
1313
use super::yaml_ext::{AsType, GetVal, ToYaml};
1414
use super::{
15-
check_offsets, common_description, make_dim_element, matchname, matchsubspec,
15+
check_offsets, common_description, do_replacements, make_dim_element, matchname, matchsubspec,
1616
modify_dim_element, spec_ind, Config, PatchResult, Spec, VAL_LVL,
1717
};
1818
use super::{make_cluster, make_interrupt, make_register};
@@ -843,6 +843,10 @@ fn modify_register(rtags: Vec<&mut Register>, rmod: &Hash, bpath: &BlockPath) ->
843843
for rtag in rtags {
844844
modify_dim_element(rtag, &dim)?;
845845
rtag.modify_from(register_builder.clone(), VAL_LVL)?;
846+
if let Ok(Some(h)) = rmod.get_hash("description") {
847+
rtag.description =
848+
Some(do_replacements(rtag.description.as_deref().unwrap_or(""), h)?.into());
849+
}
846850
if let Some("") = rmod.get_str("access")? {
847851
rtag.properties.access = None;
848852
}
@@ -856,6 +860,10 @@ fn modify_cluster(ctags: Vec<&mut Cluster>, cmod: &Hash, bpath: &BlockPath) -> P
856860
for ctag in ctags {
857861
modify_dim_element(ctag, &dim)?;
858862
ctag.modify_from(cluster_builder.clone(), VAL_LVL)?;
863+
if let Ok(Some(h)) = cmod.get_hash("description") {
864+
ctag.description =
865+
Some(do_replacements(ctag.description.as_deref().unwrap_or(""), h)?.into());
866+
}
859867
}
860868
Ok(())
861869
}

Diff for: src/patch/register.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::patch::EnumAutoDerive;
1414
use super::iterators::{MatchIter, Matched};
1515
use super::yaml_ext::{AsType, GetVal, ToYaml};
1616
use super::{
17-
check_offsets, common_description, make_dim_element, matchname, modify_dim_element, spec_ind,
18-
Config, PatchResult, Spec, VAL_LVL,
17+
check_offsets, common_description, do_replacements, make_dim_element, matchname,
18+
modify_dim_element, spec_ind, Config, PatchResult, Spec, VAL_LVL,
1919
};
2020
use super::{make_derived_enumerated_values, make_ev_array, make_ev_name, make_field};
2121

@@ -311,6 +311,10 @@ impl RegisterExt for Register {
311311
}
312312
// For all other tags, just set the value
313313
ftag.modify_from(field_builder.clone(), VAL_LVL)?;
314+
if let Ok(Some(h)) = fmod.get_hash("description") {
315+
ftag.description =
316+
Some(do_replacements(ftag.description.as_deref().unwrap_or(""), h)?.into());
317+
}
314318
if let Some("") = fmod.get_str("access")? {
315319
ftag.access = None;
316320
}

0 commit comments

Comments
 (0)