Skip to content

Commit

Permalink
Merge pull request #18 from eZioPan/copy_variant_desc_to_field
Browse files Browse the repository at this point in the history
add `keep_desc` option to `DeleteEnums` and `MergeEnums` transform
  • Loading branch information
Dirbaio authored Jan 10, 2024
2 parents 419d5ac + d4efddc commit 3a854e8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/transform/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,60 @@ pub(crate) fn calc_array(mut offsets: Vec<u32>) -> (u32, Array) {
(start_offset, Array::Cursed(CursedArray { offsets }))
}
}

// filter enum by enum name, then copy variant description
pub(crate) fn extract_variant_desc(
ir: &IR,
enum_names: &str,
bit_size: Option<u32>,
) -> anyhow::Result<HashMap<String, String>> {
let re = make_regex(enum_names)?;

let mut enum_desc_pair: HashMap<String, String> = HashMap::new();
for (e_name, e_struct) in ir.enums.iter().filter(|(e_name, e_struct)| {
bit_size.map_or(true, |s| s == e_struct.bit_size) && re.is_match(e_name)
}) {
let variant_desc_str = e_struct.variants.iter().fold(String::new(), |mut acc, v| {
acc.push_str(
format!(
"{}: {}\n",
v.value,
v.description.clone().unwrap_or(String::new())
)
.as_str(),
);
acc
});

enum_desc_pair.insert(e_name.clone(), variant_desc_str);
}

Ok(enum_desc_pair)
}

// filter field by enum name, then append corresponding variant description
pub(crate) fn append_variant_desc_to_field(
ir: &mut IR,
enum_desc_pair: &HashMap<String, String>,
bit_size: Option<u32>,
) {
for fs in ir.fieldsets.values_mut() {
for f in fs
.fields
.iter_mut()
.filter(|f| bit_size.map_or(true, |s| s == f.bit_size) && f.enumm.is_some())
{
for (_, desc_string) in enum_desc_pair
.iter()
.filter(|(e_name, _)| **e_name == f.enumm.clone().unwrap())
{
match &f.description {
Some(desc) => {
f.description = Some(format!("{}\n{}", desc.clone(), desc_string.clone()))
}
None => f.description = Some(desc_string.clone()),
}
}
}
}
}
6 changes: 6 additions & 0 deletions src/transform/delete_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ pub struct DeleteEnums {
pub bit_size: Option<u32>,
#[serde(default)]
pub soft: bool,
pub keep_desc: Option<bool>,
}

impl DeleteEnums {
pub fn run(&self, ir: &mut IR) -> anyhow::Result<()> {
if self.keep_desc.unwrap_or(false) {
let variant_desc = extract_variant_desc(ir, &self.from, self.bit_size)?;
append_variant_desc_to_field(ir, &variant_desc, self.bit_size);
}

let re = make_regex(&self.from)?;

let mut ids: HashSet<String> = HashSet::new();
Expand Down
6 changes: 6 additions & 0 deletions src/transform/merge_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ pub struct MergeEnums {
pub check: CheckLevel,
#[serde(default)]
pub skip_unmergeable: bool,
pub keep_desc: Option<bool>,
}

impl MergeEnums {
pub fn run(&self, ir: &mut IR) -> anyhow::Result<()> {
if self.keep_desc.unwrap_or(false) {
let variant_desc = extract_variant_desc(ir, &self.from, None)?;
append_variant_desc_to_field(ir, &variant_desc, None);
}

let re = make_regex(&self.from)?;
let groups = match_groups(ir.enums.keys().cloned(), &re, &self.to);

Expand Down

0 comments on commit 3a854e8

Please sign in to comment.