diff --git a/src/field.rs b/src/field.rs index 201b92e..ea9b669 100644 --- a/src/field.rs +++ b/src/field.rs @@ -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, + /// Define the indices to use, if not numeric. Comma separated (e.g. `A,B`). + #[serde(default)] + pub dim_index: Option, /// Name string used to identify the field. pub name: String, /// String describing the details of the register. @@ -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 { + &self.dim_index + } + fn name(&self) -> &String { &self.name } diff --git a/src/lib.rs b/src/lib.rs index 5f5dd6e..a67292b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; + 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::>()) + .unwrap_or_else(|| (0..count).map(|i| format!("{}", i)).collect::>()); + return indices + .into_iter() + .enumerate() + .map(|(i, idx)| { + ( + self.name().replace("[%s]", &format!("_{}", idx)).replace("%s", &idx), + i as u32 * step, + ) + }) .collect(); } } diff --git a/src/peripheral.rs b/src/peripheral.rs index 0272d24..43262d3 100644 --- a/src/peripheral.rs +++ b/src/peripheral.rs @@ -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, + /// Define the indices to use, if not numeric. Comma separated (e.g. `A,B`). + #[serde(default)] + pub dim_index: Option, /// The string identifies the peripheral. pub name: String, /// The string provides an overview of the purpose and functionality of the peripheral. @@ -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 { + &self.dim_index + } + fn name(&self) -> &String { &self.name } diff --git a/src/register.rs b/src/register.rs index 93ef1dc..3b9d5f5 100644 --- a/src/register.rs +++ b/src/register.rs @@ -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, + /// Define the indices to use, if not numeric. Comma separated (e.g. `A,B`). + #[serde(default)] + pub dim_index: Option, /// String to identify the cluster. pub name: String, /// String describing the details of the register cluster. @@ -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, + /// Define the indices to use, if not numeric. Comma separated (e.g. `A,B`). + #[serde(default)] + pub dim_index: Option, /// String to identify the register. pub name: String, /// String describing the details of the register. @@ -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 { + &self.dim_index + } + fn name(&self) -> &String { &self.name } @@ -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 { + &self.dim_index + } + fn name(&self) -> &String { &self.name }