Skip to content

Commit 3ac31f0

Browse files
bors[bot]burrbull
andauthored
Merge #169
169: add dim_name, dim_array_index r=Emilgardis a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents b971202 + 01cb8e6 commit 3ac31f0

File tree

7 files changed

+136
-14
lines changed

7 files changed

+136
-14
lines changed

svd-encoder/src/dimelement.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::{new_node, Element, Encode, EncodeError};
2-
use crate::svd::DimElement;
32

4-
impl Encode for DimElement {
3+
impl Encode for crate::svd::DimElement {
54
type Error = EncodeError;
65

76
fn encode(&self) -> Result<Element, EncodeError> {
@@ -17,6 +16,32 @@ impl Encode for DimElement {
1716
e.children.push(new_node("dimIndex", di.join(",")));
1817
}
1918

19+
if let Some(dim_name) = &self.dim_name {
20+
e.children.push(new_node("dimName", dim_name.clone()))
21+
}
22+
23+
if let Some(v) = &self.dim_array_index {
24+
e.children.push(v.encode_node()?);
25+
}
26+
2027
Ok(e)
2128
}
2229
}
30+
31+
impl Encode for crate::svd::DimArrayIndex {
32+
type Error = EncodeError;
33+
34+
fn encode(&self) -> Result<Element, EncodeError> {
35+
let mut base = Element::new("dimArrayIndex");
36+
37+
if let Some(d) = &self.header_enum_name {
38+
base.children.push(new_node("headerEnumName", d.clone()));
39+
}
40+
41+
for v in &self.values {
42+
base.children.push(v.encode_node()?);
43+
}
44+
45+
Ok(base)
46+
}
47+
}

svd-encoder/src/enumeratedvalues.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl Encode for EnumeratedValues {
99
let mut base = Element::new("enumeratedValues");
1010

1111
if let Some(d) = &self.name {
12-
base.children.push(new_node("name", (*d).clone()));
12+
base.children.push(new_node("name", d.clone()));
1313
};
1414

1515
if let Some(v) = &self.usage {
@@ -18,7 +18,7 @@ impl Encode for EnumeratedValues {
1818

1919
if let Some(v) = &self.derived_from {
2020
base.attributes
21-
.insert(String::from("derivedFrom"), (*v).clone());
21+
.insert(String::from("derivedFrom"), v.clone());
2222
}
2323

2424
for v in &self.values {

svd-parser/src/dimelement.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::types::DimIndex;
22
use super::*;
3-
use crate::svd::DimElement;
3+
use crate::svd::{DimArrayIndex, DimElement, EnumeratedValue};
44

55
impl Parse for DimElement {
66
type Object = Self;
@@ -12,7 +12,35 @@ impl Parse for DimElement {
1212
.dim(tree.get_child_u32("dim")?)
1313
.dim_increment(tree.get_child_u32("dimIncrement")?)
1414
.dim_index(optional::<DimIndex>("dimIndex", tree, config)?)
15+
.dim_name(tree.get_child_text_opt("dimName")?)
16+
.dim_array_index(optional::<DimArrayIndex>("dimArrayIndex", tree, config)?)
1517
.build(config.validate_level)
1618
.map_err(|e| SVDError::from(e).at(tree.id()))
1719
}
1820
}
21+
22+
impl Parse for DimArrayIndex {
23+
type Object = Self;
24+
type Error = SVDErrorAt;
25+
type Config = Config;
26+
27+
fn parse(tree: &Node, config: &Self::Config) -> Result<Self, Self::Error> {
28+
Ok(Self {
29+
header_enum_name: tree.get_child_text_opt("headerEnumName")?,
30+
values: {
31+
let values: Result<Vec<_>, _> = tree
32+
.children()
33+
.filter(|t| t.is_element() && !matches!(t.tag_name().name(), "headerEnumName"))
34+
.map(|t| {
35+
if t.has_tag_name("enumeratedValue") {
36+
EnumeratedValue::parse(&t, config)
37+
} else {
38+
Err(SVDError::NotExpectedTag("enumeratedValue".to_string()).at(t.id()))
39+
}
40+
})
41+
.collect();
42+
values?
43+
},
44+
})
45+
}
46+
}

svd-rs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10+
- Add `dim_name` and `dim_array_index` to `DimElement`
1011
- Add `alternate_peripheral`, `prepend_to_name`, `append_to_name`,
1112
`header_struct_name` to `PeripheralInfo`, `alternate_cluster` to `ClusterInfo`
1213
- Add `protection` to `RegisterProperties` and `AddressBlock`

svd-rs/src/derive_from.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ impl DeriveFrom for Peripheral {
114114
Self::Single(info.derive_from(other_info))
115115
}
116116
(Self::Single(info), Self::Array(other_info, other_dim)) => {
117-
Self::Array(info.derive_from(other_info), other_dim.clone())
117+
let mut dim = other_dim.clone();
118+
dim.dim_name = None;
119+
Self::Array(info.derive_from(other_info), dim)
118120
}
119121
(Self::Array(info, dim), Self::Single(other_info))
120122
| (Self::Array(info, dim), Self::Array(other_info, _)) => {
@@ -131,7 +133,9 @@ impl DeriveFrom for Cluster {
131133
Self::Single(info.derive_from(other_info))
132134
}
133135
(Self::Single(info), Self::Array(other_info, other_dim)) => {
134-
Self::Array(info.derive_from(other_info), other_dim.clone())
136+
let mut dim = other_dim.clone();
137+
dim.dim_name = None;
138+
Self::Array(info.derive_from(other_info), dim)
135139
}
136140
(Self::Array(info, dim), Self::Single(other_info))
137141
| (Self::Array(info, dim), Self::Array(other_info, _)) => {
@@ -148,7 +152,9 @@ impl DeriveFrom for Register {
148152
Self::Single(info.derive_from(other_info))
149153
}
150154
(Self::Single(info), Self::Array(other_info, other_dim)) => {
151-
Self::Array(info.derive_from(other_info), other_dim.clone())
155+
let mut dim = other_dim.clone();
156+
dim.dim_name = None;
157+
Self::Array(info.derive_from(other_info), dim)
152158
}
153159
(Self::Array(info, dim), Self::Single(other_info))
154160
| (Self::Array(info, dim), Self::Array(other_info, _)) => {
@@ -165,7 +171,9 @@ impl DeriveFrom for Field {
165171
Self::Single(info.derive_from(other_info))
166172
}
167173
(Self::Single(info), Self::Array(other_info, other_dim)) => {
168-
Self::Array(info.derive_from(other_info), other_dim.clone())
174+
let mut dim = other_dim.clone();
175+
dim.dim_name = None;
176+
Self::Array(info.derive_from(other_info), dim)
169177
}
170178
(Self::Array(info, dim), Self::Single(other_info))
171179
| (Self::Array(info, dim), Self::Array(other_info, _)) => {

svd-rs/src/dimelement.rs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{BuildError, EmptyToNone, SvdError, ValidateLevel};
1+
use super::{BuildError, EmptyToNone, EnumeratedValue, SvdError, ValidateLevel};
22
use std::borrow::Cow;
33

44
/// Defines arrays and lists.
@@ -23,6 +23,44 @@ pub struct DimElement {
2323
serde(default, skip_serializing_if = "Option::is_none")
2424
)]
2525
pub dim_index: Option<Vec<String>>,
26+
27+
/// Specify the name of the structure. If not defined, then the entry of the `name` element is used
28+
#[cfg_attr(
29+
feature = "serde",
30+
serde(default, skip_serializing_if = "Option::is_none")
31+
)]
32+
pub dim_name: Option<String>,
33+
34+
/// Grouping element to create enumerations in the header file
35+
#[cfg_attr(
36+
feature = "serde",
37+
serde(default, skip_serializing_if = "Option::is_none")
38+
)]
39+
pub dim_array_index: Option<DimArrayIndex>,
40+
}
41+
42+
/// Grouping element to create enumerations in the header file
43+
///
44+
/// This information is used for generating an enum in the device header file.
45+
/// The debugger may use this information to display the identifier string
46+
/// as well as the description. Just like symbolic constants making source
47+
/// code more readable, the system view in the debugger becomes more instructive
48+
#[cfg_attr(
49+
feature = "serde",
50+
derive(serde::Deserialize, serde::Serialize),
51+
serde(rename_all = "camelCase")
52+
)]
53+
#[derive(Clone, Debug, PartialEq)]
54+
pub struct DimArrayIndex {
55+
/// Specify the base name of enumerations
56+
#[cfg_attr(
57+
feature = "serde",
58+
serde(default, skip_serializing_if = "Option::is_none")
59+
)]
60+
pub header_enum_name: Option<String>,
61+
62+
/// Specify the values contained in the enumeration
63+
pub values: Vec<EnumeratedValue>,
2664
}
2765

2866
/// Builder for [`DimElement`]
@@ -31,6 +69,8 @@ pub struct DimElementBuilder {
3169
dim: Option<u32>,
3270
dim_increment: Option<u32>,
3371
dim_index: Option<Vec<String>>,
72+
dim_name: Option<String>,
73+
dim_array_index: Option<DimArrayIndex>,
3474
}
3575

3676
impl From<DimElement> for DimElementBuilder {
@@ -39,26 +79,38 @@ impl From<DimElement> for DimElementBuilder {
3979
dim: Some(d.dim),
4080
dim_increment: Some(d.dim_increment),
4181
dim_index: d.dim_index,
82+
dim_name: d.dim_name,
83+
dim_array_index: d.dim_array_index,
4284
}
4385
}
4486
}
4587

4688
impl DimElementBuilder {
47-
/// set the dim of the elements
89+
/// Set the dim of the elements
4890
pub fn dim(mut self, value: u32) -> Self {
4991
self.dim = Some(value);
5092
self
5193
}
52-
/// set the dim increment of the elements
94+
/// Set the dim increment of the elements
5395
pub fn dim_increment(mut self, value: u32) -> Self {
5496
self.dim_increment = Some(value);
5597
self
5698
}
57-
/// set the dim index of the elements
99+
/// Set the dim index of the elements
58100
pub fn dim_index(mut self, value: Option<Vec<String>>) -> Self {
59101
self.dim_index = value;
60102
self
61103
}
104+
/// Set the dim name of the elements
105+
pub fn dim_name(mut self, value: Option<String>) -> Self {
106+
self.dim_name = value;
107+
self
108+
}
109+
/// Set the dim_array_index of the elements
110+
pub fn dim_array_index(mut self, value: Option<DimArrayIndex>) -> Self {
111+
self.dim_array_index = value;
112+
self
113+
}
62114
/// Validate and build a [`DimElement`].
63115
pub fn build(self, lvl: ValidateLevel) -> Result<DimElement, SvdError> {
64116
let mut de = DimElement {
@@ -69,6 +121,8 @@ impl DimElementBuilder {
69121
.dim_increment
70122
.ok_or_else(|| BuildError::Uninitialized("dim_increment".to_string()))?,
71123
dim_index: self.dim_index.empty_to_none(),
124+
dim_name: self.dim_name.empty_to_none(),
125+
dim_array_index: self.dim_array_index,
72126
};
73127
if !lvl.is_disabled() {
74128
de.validate(lvl)?;
@@ -97,6 +151,12 @@ impl DimElement {
97151
if builder.dim_index.is_some() {
98152
self.dim_index = builder.dim_index.empty_to_none();
99153
}
154+
if builder.dim_name.is_some() {
155+
self.dim_name = builder.dim_name.empty_to_none();
156+
}
157+
if builder.dim_array_index.is_some() {
158+
self.dim_array_index = builder.dim_array_index;
159+
}
100160
if !lvl.is_disabled() {
101161
self.validate(lvl)
102162
} else {

svd-rs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub use self::registercluster::RegisterCluster;
7676

7777
/// Dimelement objects
7878
pub mod dimelement;
79-
pub use self::dimelement::{DimElement, DimElementBuilder};
79+
pub use self::dimelement::{DimArrayIndex, DimElement, DimElementBuilder};
8080

8181
/// Peripheral objects
8282
pub mod peripheral;

0 commit comments

Comments
 (0)