Skip to content

Commit

Permalink
collect_in_array descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 30, 2023
1 parent 79959a7 commit d1aaca8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 18 deletions.
45 changes: 45 additions & 0 deletions src/patch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod patch_cli;

use globset::Glob;
use std::borrow::Cow;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -637,3 +638,47 @@ fn check_offsets(offsets: &[u32], dim_increment: u32) -> bool {
}
true
}

fn first_diff(s1: &str, s2: &str) -> usize {
let mut i = 0;
for (b1, b2) in s1.bytes().zip(s2.bytes()) {
if b1 != b2 {
break;
}
i += 1;
}
i
}
fn replacefirstfrom<'a>(s: &'a str, pat: &str, to: &str, i: usize) -> Cow<'a, str> {
if i >= s.len() {
s.into()
} else {
let (s1, s2) = s.split_at(i);
format!("{s1}{}", &s2.replacen(pat, to, 1)).into()
}
}
fn common_description(descs: &[Option<&str>], dim_index: &[String]) -> Option<Option<String>> {
let mut start = 0;
if descs.len() > 1 {
if let Some((d1, d2)) = descs[0].zip(descs[1]) {
start = first_diff(d1, d2);
}
}
let mut dsc = None;
let mut first = true;
for (d, idx) in descs.into_iter().zip(dim_index) {
if first {
dsc = d.map(|desc| replacefirstfrom(desc, idx, "%s", start));
} else {
if d != &dsc
.as_ref()
.map(|desc| desc.replacen("%s", idx, 1))
.as_deref()
{
return None;
}
}
first = false;
}
Some(dsc.map(Into::into))
}
38 changes: 30 additions & 8 deletions src/patch/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use super::iterators::{MatchIter, Matched};
use super::register::{RegisterExt, RegisterInfoExt};
use super::yaml_ext::{AsType, GetVal, ToYaml};
use super::{
check_offsets, make_dim_element, matchname, matchsubspec, modify_dim_element, spec_ind,
PatchResult, VAL_LVL,
check_offsets, common_description, make_dim_element, matchname, matchsubspec,
modify_dim_element, spec_ind, PatchResult, VAL_LVL,
};
use super::{make_cluster, make_interrupt, make_register};

Expand Down Expand Up @@ -1207,21 +1207,43 @@ fn collect_in_array(
path
));
}
let mut rinfo = registers.swap_remove(0);
rinfo.name = if let Some(name) = rmod.get_str("name")? {

registers[0].name = if let Some(name) = rmod.get_str("name")? {
name.into()
} else {
format!("{}%s{}", &rspec[..li], &rspec[rspec.len() - ri..])
};

if let Some(desc) = rmod.get_str("description")? {
if desc != "_original" {
rinfo.description = Some(desc.into());
registers[0].description = Some(desc.into());
}
} else if dim_index[0] == "0" {
if let Some(desc) = rinfo.description.as_mut() {
*desc = desc.replace('0', "%s");
} else {
let descs: Vec<_> = registers.iter().map(|r| r.description.as_deref()).collect();
registers[0].description = common_description(&descs, &dim_index).ok_or_else(|| {
anyhow!(
"{}: registers cannot be collected into {rspec} array. Please, specify description",
path
)
})?;
}
if let Some(dname) = rmod.get_str("displayName")? {
if dname != "_original" {
registers[0].display_name = Some(dname.into());
}
} else {
let names: Vec<_> = registers
.iter()
.map(|r| r.display_name.as_deref())
.collect();
registers[0].description = common_description(&names, &dim_index).ok_or_else(|| {
anyhow!(
"{}: registers cannot be collected into {rspec} array. Please, specify displayName",
path
)
})?;
}
let rinfo = registers.swap_remove(0);
let mut reg = rinfo.array(
DimElement::builder()
.dim(dim as u32)
Expand Down
27 changes: 17 additions & 10 deletions src/patch/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use yaml_rust::{yaml::Hash, Yaml};
use super::iterators::{MatchIter, Matched};
use super::yaml_ext::{AsType, GetVal, ToYaml};
use super::{
check_offsets, make_dim_element, matchname, modify_dim_element, spec_ind, PatchResult, VAL_LVL,
check_offsets, common_description, make_dim_element, matchname, modify_dim_element, spec_ind,
PatchResult, VAL_LVL,
};
use super::{make_derived_enumerated_values, make_ev_array, make_ev_name, make_field};

Expand Down Expand Up @@ -404,21 +405,27 @@ impl RegisterExt for Register {
self.name
));
}
let mut finfo = fields.swap_remove(0);
if let Some(name) = fmod.get_str("name")? {
finfo.name = name.into();
fields[0].name = if let Some(name) = fmod.get_str("name")? {
name.into()
} else {
finfo.name = format!("{}%s{}", &fspec[..li], &fspec[fspec.len() - ri..]);
}
format!("{}%s{}", &fspec[..li], &fspec[fspec.len() - ri..])
};
if let Some(desc) = fmod.get_str("description")? {
if desc != "_original" {
finfo.description = Some(desc.into());
fields[0].description = Some(desc.into());
}
} else if dim_index[0] == "0" {
if let Some(desc) = finfo.description.as_mut() {
*desc = desc.replace('0', "%s");
} else {
let descs: Vec<_> = fields.iter().map(|r| r.description.as_deref()).collect();
if let Some(desc) = common_description(&descs, &dim_index) {
fields[0].description = desc;
} else {
return Err(anyhow!(
"{}: fields cannot be collected into {fspec} array. Please, specify description",
self.name
));
}
}
let finfo = fields.swap_remove(0);
let field = finfo.array(
DimElement::builder()
.dim(dim as u32)
Expand Down

0 comments on commit d1aaca8

Please sign in to comment.