Skip to content

Commit

Permalink
Merge #169
Browse files Browse the repository at this point in the history
169: add dim_name, dim_array_index r=Emilgardis a=burrbull



Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
  • Loading branch information
bors[bot] and burrbull authored Nov 8, 2021
2 parents b971202 + 01cb8e6 commit 3ac31f0
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 14 deletions.
29 changes: 27 additions & 2 deletions svd-encoder/src/dimelement.rs
Original file line number Diff line number Diff line change
@@ -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<Element, EncodeError> {
Expand All @@ -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<Element, EncodeError> {
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)
}
}
4 changes: 2 additions & 2 deletions svd-encoder/src/enumeratedvalues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
30 changes: 29 additions & 1 deletion svd-parser/src/dimelement.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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>("dimIndex", tree, config)?)
.dim_name(tree.get_child_text_opt("dimName")?)
.dim_array_index(optional::<DimArrayIndex>("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<Self, Self::Error> {
Ok(Self {
header_enum_name: tree.get_child_text_opt("headerEnumName")?,
values: {
let values: Result<Vec<_>, _> = 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?
},
})
}
}
1 change: 1 addition & 0 deletions svd-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
16 changes: 12 additions & 4 deletions svd-rs/src/derive_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, _)) => {
Expand All @@ -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, _)) => {
Expand All @@ -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, _)) => {
Expand All @@ -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, _)) => {
Expand Down
68 changes: 64 additions & 4 deletions svd-rs/src/dimelement.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,6 +23,44 @@ pub struct DimElement {
serde(default, skip_serializing_if = "Option::is_none")
)]
pub dim_index: Option<Vec<String>>,

/// 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<String>,

/// 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<DimArrayIndex>,
}

/// 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<String>,

/// Specify the values contained in the enumeration
pub values: Vec<EnumeratedValue>,
}

/// Builder for [`DimElement`]
Expand All @@ -31,6 +69,8 @@ pub struct DimElementBuilder {
dim: Option<u32>,
dim_increment: Option<u32>,
dim_index: Option<Vec<String>>,
dim_name: Option<String>,
dim_array_index: Option<DimArrayIndex>,
}

impl From<DimElement> for DimElementBuilder {
Expand All @@ -39,26 +79,38 @@ impl From<DimElement> 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<Vec<String>>) -> Self {
self.dim_index = value;
self
}
/// Set the dim name of the elements
pub fn dim_name(mut self, value: Option<String>) -> Self {
self.dim_name = value;
self
}
/// Set the dim_array_index of the elements
pub fn dim_array_index(mut self, value: Option<DimArrayIndex>) -> Self {
self.dim_array_index = value;
self
}
/// Validate and build a [`DimElement`].
pub fn build(self, lvl: ValidateLevel) -> Result<DimElement, SvdError> {
let mut de = DimElement {
Expand All @@ -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)?;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion svd-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3ac31f0

Please sign in to comment.