Skip to content

Commit fcb3d0c

Browse files
committed
encode index ranges
1 parent 1a2e042 commit fcb3d0c

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

svd-encoder/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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.wrapping_sub(min).wrapping_add(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+
return 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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,69 @@ 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[..]);
21+
22+
let tests = vec![(
23+
DimElement::builder()
24+
.dim(3)
25+
.dim_increment(4)
26+
.dim_index(Some(vec!["3".to_string(), "4".to_string(), "5".to_string()]))
27+
.build(ValidateLevel::Strict)
28+
.unwrap(),
29+
"<dimElement>
30+
<dim>3</dim>
31+
<dimIncrement>0x4</dimIncrement>
32+
<dimIndex>3-5</dimIndex>
33+
</dimElement>
34+
",
35+
)];
36+
run_test::<DimElement>(&tests[..]);
37+
38+
let tests = vec![(
39+
DimElement::builder()
40+
.dim(3)
41+
.dim_increment(4)
42+
.dim_index(Some(vec!["3".to_string(), "5".to_string(), "4".to_string()]))
43+
.build(ValidateLevel::Strict)
44+
.unwrap(),
45+
"<dimElement>
46+
<dim>3</dim>
47+
<dimIncrement>0x4</dimIncrement>
48+
<dimIndex>3,5,4</dimIndex>
49+
</dimElement>
50+
",
51+
)];
52+
run_test::<DimElement>(&tests[..]);
2053

54+
let tests = vec![(
55+
DimElement::builder()
56+
.dim(1)
57+
.dim_increment(0)
58+
.dim_index(Some(vec!["3".to_string()]))
59+
.build(ValidateLevel::Strict)
60+
.unwrap(),
61+
"<dimElement>
62+
<dim>1</dim>
63+
<dimIncrement>0x0</dimIncrement>
64+
<dimIndex>3-3</dimIndex>
65+
</dimElement>
66+
",
67+
)];
2168
run_test::<DimElement>(&tests[..]);
2269
}
70+
71+
#[test]
72+
fn decode_encode_one_element() {
73+
}

0 commit comments

Comments
 (0)