Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the <dimIndex> tag for custom string indices #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub struct Field {
/// Specify the address increment, in Bytes, between two neighboring array members in the address map.
#[serde(default, deserialize_with = "deserialize_int_opt")]
pub dim_increment: Option<u32>,
/// Define the indices to use, if not numeric. Comma separated (e.g. `A,B`).
#[serde(default)]
pub dim_index: Option<String>,
/// Name string used to identify the field.
pub name: String,
/// String describing the details of the register.
Expand Down Expand Up @@ -53,6 +56,10 @@ impl DimGroup for Field {
self.dim.and_then(|dim| self.dim_increment.map(|dim_increment| (dim, dim_increment)))
}

fn dim_index(&self) -> &Option<String> {
&self.dim_index
}

fn name(&self) -> &String {
&self.name
}
Expand Down
18 changes: 16 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,27 @@ pub fn rerun_if_env_changed() {
pub(crate) trait DimGroup {
fn dim(&self) -> Option<(u32, u32)>;

fn dim_index(&self) -> &Option<String>;

fn name(&self) -> &String;

fn dim_group(&self) -> Vec<(String, u32)> {
if let Some((count, step)) = self.dim() {
if count > 1 {
return (0..count)
.map(|i| (self.name().replace("[%s]", &format!("_{}", i)), i * step))
let indices = self
.dim_index()
.as_ref()
.map(|idx| idx.split(',').into_iter().map(|s| s.to_owned()).collect::<Vec<_>>())
.unwrap_or_else(|| (0..count).map(|i| format!("{}", i)).collect::<Vec<_>>());
return indices
.into_iter()
.enumerate()
.map(|(i, idx)| {
(
self.name().replace("[%s]", &format!("_{}", idx)).replace("%s", &idx),
i as u32 * step,
)
})
.collect();
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub struct Peripheral {
/// Specify the address increment, in Bytes, between two neighboring array members in the address map.
#[serde(default, deserialize_with = "deserialize_int_opt")]
pub dim_increment: Option<u32>,
/// Define the indices to use, if not numeric. Comma separated (e.g. `A,B`).
#[serde(default)]
pub dim_index: Option<String>,
/// The string identifies the peripheral.
pub name: String,
/// The string provides an overview of the purpose and functionality of the peripheral.
Expand Down Expand Up @@ -310,6 +313,10 @@ impl DimGroup for Peripheral {
self.dim.and_then(|dim| self.dim_increment.map(|dim_increment| (dim, dim_increment)))
}

fn dim_index(&self) -> &Option<String> {
&self.dim_index
}

fn name(&self) -> &String {
&self.name
}
Expand Down
14 changes: 14 additions & 0 deletions src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub struct Cluster {
/// Specify the address increment, in Bytes, between two neighboring array members in the address map.
#[serde(default, deserialize_with = "deserialize_int_opt")]
pub dim_increment: Option<u32>,
/// Define the indices to use, if not numeric. Comma separated (e.g. `A,B`).
#[serde(default)]
pub dim_index: Option<String>,
/// String to identify the cluster.
pub name: String,
/// String describing the details of the register cluster.
Expand All @@ -44,6 +47,9 @@ pub struct Register {
/// Specify the address increment, in Bytes, between two neighboring array members in the address map.
#[serde(default, deserialize_with = "deserialize_int_opt")]
pub dim_increment: Option<u32>,
/// Define the indices to use, if not numeric. Comma separated (e.g. `A,B`).
#[serde(default)]
pub dim_index: Option<String>,
/// String to identify the register.
pub name: String,
/// String describing the details of the register.
Expand Down Expand Up @@ -202,6 +208,10 @@ impl DimGroup for Cluster {
self.dim.and_then(|dim| self.dim_increment.map(|dim_increment| (dim, dim_increment)))
}

fn dim_index(&self) -> &Option<String> {
&self.dim_index
}

fn name(&self) -> &String {
&self.name
}
Expand All @@ -212,6 +222,10 @@ impl DimGroup for Register {
self.dim.and_then(|dim| self.dim_increment.map(|dim_increment| (dim, dim_increment)))
}

fn dim_index(&self) -> &Option<String> {
&self.dim_index
}

fn name(&self) -> &String {
&self.name
}
Expand Down