Skip to content

Commit 2cc1ad5

Browse files
bors[bot]burrbull
andauthored
Merge #195
195: encode dimIndex ranges r=adamgreig a=burrbull cc `@adamgreig` Partially related to stm32-rs/stm32-rs#701 (comment) Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 1a2e042 + 980ae4c commit 2cc1ad5

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed

svd-encoder/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10+
- Encode "dimIndex" ranges
11+
1012
## [v0.13.0] - 2022-01-02
1113

1214
- Bump `svd-rs`

svd-encoder/src/dimelement.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ impl Encode for crate::svd::DimElement {
1313
));
1414

1515
if let Some(di) = &self.dim_index {
16-
e.children.push(new_node("dimIndex", di.join(",")));
16+
e.children
17+
.push(if let Some(range) = self.indexes_as_range() {
18+
new_node("dimIndex", format!("{}-{}", range.start(), range.end()))
19+
} else {
20+
new_node("dimIndex", di.join(","))
21+
});
1722
}
1823

1924
if let Some(dim_name) = &self.dim_name {

svd-rs/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10+
- add `indexes_as_range` for `DimElement`
11+
1012
## [v0.13.0] - 2022-01-04
1113

1214
- fixed `BitRange` deserializing

svd-rs/src/dimelement.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{BuildError, EmptyToNone, EnumeratedValue, SvdError, ValidateLevel};
22
use std::borrow::Cow;
3+
use std::ops::RangeInclusive;
34

45
/// Defines arrays and lists.
56
#[cfg_attr(
@@ -136,6 +137,24 @@ impl DimElement {
136137
pub fn builder() -> DimElementBuilder {
137138
DimElementBuilder::default()
138139
}
140+
/// Try to represent [`DimElement`] as range of integer indexes
141+
pub fn indexes_as_range(&self) -> Option<RangeInclusive<u32>> {
142+
let mut integers = Vec::with_capacity(self.dim as usize);
143+
for idx in self.indexes() {
144+
integers.push(idx.parse::<u32>().ok()?);
145+
}
146+
let min = *integers.iter().min()?;
147+
let max = *integers.iter().max()?;
148+
if max - min + 1 != self.dim {
149+
return None;
150+
}
151+
for (&i, r) in integers.iter().zip(min..=max) {
152+
if i != r {
153+
return None;
154+
}
155+
}
156+
Some(min..=max)
157+
}
139158
/// Modify an existing [`DimElement`] based on a [builder](DimElementBuilder).
140159
pub fn modify_from(
141160
&mut self,

tests/src/dimelement.rs

+60-2
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,76 @@ use crate::svd::{DimElement, ValidateLevel};
55
fn decode_encode() {
66
let tests = vec![(
77
DimElement::builder()
8-
.dim(100)
8+
.dim(2)
99
.dim_increment(4)
1010
.dim_index(Some(vec!["10".to_string(), "20".to_string()]))
1111
.build(ValidateLevel::Strict)
1212
.unwrap(),
1313
"<dimElement>
14-
<dim>100</dim>
14+
<dim>2</dim>
1515
<dimIncrement>0x4</dimIncrement>
1616
<dimIndex>10,20</dimIndex>
1717
</dimElement>
1818
",
1919
)];
20+
run_test::<DimElement>(&tests[..]);
2021

22+
let tests = vec![(
23+
DimElement::builder()
24+
.dim(3)
25+
.dim_increment(4)
26+
.dim_index(Some(vec![
27+
"3".to_string(),
28+
"4".to_string(),
29+
"5".to_string(),
30+
]))
31+
.build(ValidateLevel::Strict)
32+
.unwrap(),
33+
"<dimElement>
34+
<dim>3</dim>
35+
<dimIncrement>0x4</dimIncrement>
36+
<dimIndex>3-5</dimIndex>
37+
</dimElement>
38+
",
39+
)];
40+
run_test::<DimElement>(&tests[..]);
41+
42+
let tests = vec![(
43+
DimElement::builder()
44+
.dim(3)
45+
.dim_increment(4)
46+
.dim_index(Some(vec![
47+
"3".to_string(),
48+
"5".to_string(),
49+
"4".to_string(),
50+
]))
51+
.build(ValidateLevel::Strict)
52+
.unwrap(),
53+
"<dimElement>
54+
<dim>3</dim>
55+
<dimIncrement>0x4</dimIncrement>
56+
<dimIndex>3,5,4</dimIndex>
57+
</dimElement>
58+
",
59+
)];
60+
run_test::<DimElement>(&tests[..]);
61+
62+
let tests = vec![(
63+
DimElement::builder()
64+
.dim(1)
65+
.dim_increment(0)
66+
.dim_index(Some(vec!["3".to_string()]))
67+
.build(ValidateLevel::Strict)
68+
.unwrap(),
69+
"<dimElement>
70+
<dim>1</dim>
71+
<dimIncrement>0x0</dimIncrement>
72+
<dimIndex>3-3</dimIndex>
73+
</dimElement>
74+
",
75+
)];
2176
run_test::<DimElement>(&tests[..]);
2277
}
78+
79+
#[test]
80+
fn decode_encode_one_element() {}

0 commit comments

Comments
 (0)