From 01cb8e6c372d36431cbf13744dc2c8e1d8705fbf Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Mon, 8 Nov 2021 23:58:10 +0300 Subject: [PATCH] add dim_name, dim_array_index --- svd-encoder/src/dimelement.rs | 29 +++++++++++- svd-encoder/src/enumeratedvalues.rs | 4 +- svd-parser/src/dimelement.rs | 30 ++++++++++++- svd-rs/CHANGELOG.md | 1 + svd-rs/src/derive_from.rs | 16 +++++-- svd-rs/src/dimelement.rs | 68 +++++++++++++++++++++++++++-- svd-rs/src/lib.rs | 2 +- 7 files changed, 136 insertions(+), 14 deletions(-) diff --git a/svd-encoder/src/dimelement.rs b/svd-encoder/src/dimelement.rs index 8fb6b9e8..cd277ba3 100644 --- a/svd-encoder/src/dimelement.rs +++ b/svd-encoder/src/dimelement.rs @@ -1,7 +1,6 @@ use super::{new_node, Element, Encode, EncodeError}; -use crate::svd::DimElement; -impl Encode for DimElement { +impl Encode for crate::svd::DimElement { type Error = EncodeError; fn encode(&self) -> Result { @@ -17,6 +16,32 @@ impl Encode for DimElement { e.children.push(new_node("dimIndex", di.join(","))); } + if let Some(dim_name) = &self.dim_name { + e.children.push(new_node("dimName", dim_name.clone())) + } + + if let Some(v) = &self.dim_array_index { + e.children.push(v.encode_node()?); + } + Ok(e) } } + +impl Encode for crate::svd::DimArrayIndex { + type Error = EncodeError; + + fn encode(&self) -> Result { + let mut base = Element::new("dimArrayIndex"); + + if let Some(d) = &self.header_enum_name { + base.children.push(new_node("headerEnumName", d.clone())); + } + + for v in &self.values { + base.children.push(v.encode_node()?); + } + + Ok(base) + } +} diff --git a/svd-encoder/src/enumeratedvalues.rs b/svd-encoder/src/enumeratedvalues.rs index 8cd71410..540e3981 100644 --- a/svd-encoder/src/enumeratedvalues.rs +++ b/svd-encoder/src/enumeratedvalues.rs @@ -9,7 +9,7 @@ impl Encode for EnumeratedValues { let mut base = Element::new("enumeratedValues"); if let Some(d) = &self.name { - base.children.push(new_node("name", (*d).clone())); + base.children.push(new_node("name", d.clone())); }; if let Some(v) = &self.usage { @@ -18,7 +18,7 @@ impl Encode for EnumeratedValues { if let Some(v) = &self.derived_from { base.attributes - .insert(String::from("derivedFrom"), (*v).clone()); + .insert(String::from("derivedFrom"), v.clone()); } for v in &self.values { diff --git a/svd-parser/src/dimelement.rs b/svd-parser/src/dimelement.rs index 4b5e3f9c..a6184aab 100644 --- a/svd-parser/src/dimelement.rs +++ b/svd-parser/src/dimelement.rs @@ -1,6 +1,6 @@ use super::types::DimIndex; use super::*; -use crate::svd::DimElement; +use crate::svd::{DimArrayIndex, DimElement, EnumeratedValue}; impl Parse for DimElement { type Object = Self; @@ -12,7 +12,35 @@ impl Parse for DimElement { .dim(tree.get_child_u32("dim")?) .dim_increment(tree.get_child_u32("dimIncrement")?) .dim_index(optional::("dimIndex", tree, config)?) + .dim_name(tree.get_child_text_opt("dimName")?) + .dim_array_index(optional::("dimArrayIndex", tree, config)?) .build(config.validate_level) .map_err(|e| SVDError::from(e).at(tree.id())) } } + +impl Parse for DimArrayIndex { + type Object = Self; + type Error = SVDErrorAt; + type Config = Config; + + fn parse(tree: &Node, config: &Self::Config) -> Result { + Ok(Self { + header_enum_name: tree.get_child_text_opt("headerEnumName")?, + values: { + let values: Result, _> = tree + .children() + .filter(|t| t.is_element() && !matches!(t.tag_name().name(), "headerEnumName")) + .map(|t| { + if t.has_tag_name("enumeratedValue") { + EnumeratedValue::parse(&t, config) + } else { + Err(SVDError::NotExpectedTag("enumeratedValue".to_string()).at(t.id())) + } + }) + .collect(); + values? + }, + }) + } +} diff --git a/svd-rs/CHANGELOG.md b/svd-rs/CHANGELOG.md index 3d5b4bb2..4361441e 100644 --- a/svd-rs/CHANGELOG.md +++ b/svd-rs/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +- Add `dim_name` and `dim_array_index` to `DimElement` - Add `alternate_peripheral`, `prepend_to_name`, `append_to_name`, `header_struct_name` to `PeripheralInfo`, `alternate_cluster` to `ClusterInfo` - Add `protection` to `RegisterProperties` and `AddressBlock` diff --git a/svd-rs/src/derive_from.rs b/svd-rs/src/derive_from.rs index 9e02c1bd..ce662513 100644 --- a/svd-rs/src/derive_from.rs +++ b/svd-rs/src/derive_from.rs @@ -114,7 +114,9 @@ impl DeriveFrom for Peripheral { Self::Single(info.derive_from(other_info)) } (Self::Single(info), Self::Array(other_info, other_dim)) => { - Self::Array(info.derive_from(other_info), other_dim.clone()) + let mut dim = other_dim.clone(); + dim.dim_name = None; + Self::Array(info.derive_from(other_info), dim) } (Self::Array(info, dim), Self::Single(other_info)) | (Self::Array(info, dim), Self::Array(other_info, _)) => { @@ -131,7 +133,9 @@ impl DeriveFrom for Cluster { Self::Single(info.derive_from(other_info)) } (Self::Single(info), Self::Array(other_info, other_dim)) => { - Self::Array(info.derive_from(other_info), other_dim.clone()) + let mut dim = other_dim.clone(); + dim.dim_name = None; + Self::Array(info.derive_from(other_info), dim) } (Self::Array(info, dim), Self::Single(other_info)) | (Self::Array(info, dim), Self::Array(other_info, _)) => { @@ -148,7 +152,9 @@ impl DeriveFrom for Register { Self::Single(info.derive_from(other_info)) } (Self::Single(info), Self::Array(other_info, other_dim)) => { - Self::Array(info.derive_from(other_info), other_dim.clone()) + let mut dim = other_dim.clone(); + dim.dim_name = None; + Self::Array(info.derive_from(other_info), dim) } (Self::Array(info, dim), Self::Single(other_info)) | (Self::Array(info, dim), Self::Array(other_info, _)) => { @@ -165,7 +171,9 @@ impl DeriveFrom for Field { Self::Single(info.derive_from(other_info)) } (Self::Single(info), Self::Array(other_info, other_dim)) => { - Self::Array(info.derive_from(other_info), other_dim.clone()) + let mut dim = other_dim.clone(); + dim.dim_name = None; + Self::Array(info.derive_from(other_info), dim) } (Self::Array(info, dim), Self::Single(other_info)) | (Self::Array(info, dim), Self::Array(other_info, _)) => { diff --git a/svd-rs/src/dimelement.rs b/svd-rs/src/dimelement.rs index e7e27512..116f0adb 100644 --- a/svd-rs/src/dimelement.rs +++ b/svd-rs/src/dimelement.rs @@ -1,4 +1,4 @@ -use super::{BuildError, EmptyToNone, SvdError, ValidateLevel}; +use super::{BuildError, EmptyToNone, EnumeratedValue, SvdError, ValidateLevel}; use std::borrow::Cow; /// Defines arrays and lists. @@ -23,6 +23,44 @@ pub struct DimElement { serde(default, skip_serializing_if = "Option::is_none") )] pub dim_index: Option>, + + /// Specify the name of the structure. If not defined, then the entry of the `name` element is used + #[cfg_attr( + feature = "serde", + serde(default, skip_serializing_if = "Option::is_none") + )] + pub dim_name: Option, + + /// Grouping element to create enumerations in the header file + #[cfg_attr( + feature = "serde", + serde(default, skip_serializing_if = "Option::is_none") + )] + pub dim_array_index: Option, +} + +/// Grouping element to create enumerations in the header file +/// +/// This information is used for generating an enum in the device header file. +/// The debugger may use this information to display the identifier string +/// as well as the description. Just like symbolic constants making source +/// code more readable, the system view in the debugger becomes more instructive +#[cfg_attr( + feature = "serde", + derive(serde::Deserialize, serde::Serialize), + serde(rename_all = "camelCase") +)] +#[derive(Clone, Debug, PartialEq)] +pub struct DimArrayIndex { + /// Specify the base name of enumerations + #[cfg_attr( + feature = "serde", + serde(default, skip_serializing_if = "Option::is_none") + )] + pub header_enum_name: Option, + + /// Specify the values contained in the enumeration + pub values: Vec, } /// Builder for [`DimElement`] @@ -31,6 +69,8 @@ pub struct DimElementBuilder { dim: Option, dim_increment: Option, dim_index: Option>, + dim_name: Option, + dim_array_index: Option, } impl From for DimElementBuilder { @@ -39,26 +79,38 @@ impl From for DimElementBuilder { dim: Some(d.dim), dim_increment: Some(d.dim_increment), dim_index: d.dim_index, + dim_name: d.dim_name, + dim_array_index: d.dim_array_index, } } } impl DimElementBuilder { - /// set the dim of the elements + /// Set the dim of the elements pub fn dim(mut self, value: u32) -> Self { self.dim = Some(value); self } - /// set the dim increment of the elements + /// Set the dim increment of the elements pub fn dim_increment(mut self, value: u32) -> Self { self.dim_increment = Some(value); self } - /// set the dim index of the elements + /// Set the dim index of the elements pub fn dim_index(mut self, value: Option>) -> Self { self.dim_index = value; self } + /// Set the dim name of the elements + pub fn dim_name(mut self, value: Option) -> Self { + self.dim_name = value; + self + } + /// Set the dim_array_index of the elements + pub fn dim_array_index(mut self, value: Option) -> Self { + self.dim_array_index = value; + self + } /// Validate and build a [`DimElement`]. pub fn build(self, lvl: ValidateLevel) -> Result { let mut de = DimElement { @@ -69,6 +121,8 @@ impl DimElementBuilder { .dim_increment .ok_or_else(|| BuildError::Uninitialized("dim_increment".to_string()))?, dim_index: self.dim_index.empty_to_none(), + dim_name: self.dim_name.empty_to_none(), + dim_array_index: self.dim_array_index, }; if !lvl.is_disabled() { de.validate(lvl)?; @@ -97,6 +151,12 @@ impl DimElement { if builder.dim_index.is_some() { self.dim_index = builder.dim_index.empty_to_none(); } + if builder.dim_name.is_some() { + self.dim_name = builder.dim_name.empty_to_none(); + } + if builder.dim_array_index.is_some() { + self.dim_array_index = builder.dim_array_index; + } if !lvl.is_disabled() { self.validate(lvl) } else { diff --git a/svd-rs/src/lib.rs b/svd-rs/src/lib.rs index db4ad33e..4d869a9a 100644 --- a/svd-rs/src/lib.rs +++ b/svd-rs/src/lib.rs @@ -76,7 +76,7 @@ pub use self::registercluster::RegisterCluster; /// Dimelement objects pub mod dimelement; -pub use self::dimelement::{DimElement, DimElementBuilder}; +pub use self::dimelement::{DimArrayIndex, DimElement, DimElementBuilder}; /// Peripheral objects pub mod peripheral;