Skip to content

Commit

Permalink
refacto: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
flobz committed Nov 3, 2024
1 parent 6fde7d7 commit d2a1360
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
11 changes: 5 additions & 6 deletions src/atdf/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use crate::ElementExt;
pub fn parse(
bitfield_el: &xmltree::Element,
value_groups: &atdf::values::ValueGroups,
mode_name: Option<String>,
mode_name: Option<&str>,
) -> crate::Result<chip::Field> {
debug_assert!(bitfield_el.name == "bitfield");
let name;
if let Some(mode) = mode_name {
name = format!("{}_{}", mode, bitfield_el.attr("name")?.clone());
let name: String = if let Some(mode) = mode_name {
format!("{}_{}", mode, bitfield_el.attr("name")?)
} else {
name = bitfield_el.attr("name")?.clone();
}
bitfield_el.attr("name")?.clone()
};
let description = bitfield_el
.attributes
.get("caption")
Expand Down
56 changes: 32 additions & 24 deletions src/atdf/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ use crate::util;
use crate::ElementExt;
use std::collections::BTreeMap;

fn field_map_from_bitfield_children(
el: &xmltree::Element,
values: &atdf::values::ValueGroups,
mode_name: Option<&str>,
) -> crate::Result<BTreeMap<String, chip::Field>> {
el.children
.iter()
.filter_map(|node| node.as_element().filter(|e| e.name == "bitfield"))
.map(|e| atdf::field::parse(e, values, mode_name))
.map(|r| r.map(|f| (f.name.clone(), f)))
.collect::<Result<BTreeMap<_, _>, _>>()
}

pub fn parse(
el: &xmltree::Element,
offset: usize,
Expand Down Expand Up @@ -35,32 +48,27 @@ pub fn parse(
chip::AccessMode::ReadWrite
};

let mut fields: BTreeMap<String, chip::Field> = el
.children
.iter()
.filter_map(|node| node.as_element().filter(|e| e.name == "bitfield"))
.map(|e| atdf::field::parse(e, values, None))
.map(|r| r.map(|f| (f.name.clone(), f)))
.collect::<Result<BTreeMap<_, _>, _>>()?;
// get bitfield under register
let mut fields: BTreeMap<String, chip::Field> =
field_map_from_bitfield_children(el, values, None)?;

let modes = el
.children
// get bitfield under register.mode
el.children
.iter()
.filter_map(|mode| mode.as_element().filter(|m| m.name == "mode"))
.into_iter();
for mode in modes {
if let Some(mode_name) = mode.attributes.get("name").cloned() {
let res = mode
.children
.clone()
.into_iter()
.filter_map(|el| el.as_element().filter(|e| e.name == "bitfield").cloned())
.map(|e| atdf::field::parse(&e, values, Some(mode_name.clone())))
.map(|r| r.map(|f| (f.name.clone(), f)))
.collect::<Result<BTreeMap<_, _>, _>>()?;
fields.append(&mut res.clone());
}
}
.filter_map(|mode| {
mode.as_element()
.filter(|m| m.name == "mode")
.and_then(|m| m.attributes.get("name").map(|mode_name| (mode_name, m)))
})
.try_for_each(|(mode_name, el)| {
fields.append(&mut field_map_from_bitfield_children(
el,
values,
Some(mode_name),
)?);
crate::Result::Ok(())
})?;

Ok(chip::Register {
name,
description,
Expand Down

0 comments on commit d2a1360

Please sign in to comment.