Skip to content

Commit 0044fb7

Browse files
bors[bot]burrbull
andauthored
Merge #165
165: array constructors r=Emilgardis a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 224eb49 + 0fc103d commit 0044fb7

File tree

13 files changed

+127
-52
lines changed

13 files changed

+127
-52
lines changed

svd-parser/src/cluster.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ impl Parse for Cluster {
2525
.at(tree.id()));
2626
}
2727
}
28-
29-
Ok(Self::Array(info, array_info))
28+
Ok(info.array(array_info))
3029
} else {
31-
Ok(Self::Single(info))
30+
Ok(info.single())
3231
}
3332
}
3433
}

svd-parser/src/field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ impl Parse for Field {
2626
.at(tree.id()));
2727
}
2828
}
29-
Ok(Self::Array(info, array_info))
29+
Ok(info.array(array_info))
3030
} else {
31-
Ok(Self::Single(info))
31+
Ok(info.single())
3232
}
3333
}
3434
}

svd-parser/src/peripheral.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ impl Parse for Peripheral {
2626
.at(tree.id()));
2727
}
2828
}
29-
Ok(Self::Array(info, array_info))
29+
Ok(info.array(array_info))
3030
} else {
31-
Ok(Self::Single(info))
31+
Ok(info.single())
3232
}
3333
}
3434
}

svd-parser/src/register.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ impl Parse for Register {
2626
.at(tree.id()));
2727
}
2828
}
29-
Ok(Self::Array(info, array_info))
29+
Ok(info.array(array_info))
3030
} else {
31-
Ok(Self::Single(info))
31+
Ok(info.single())
3232
}
3333
}
3434
}

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 `single` and `array` for `Info` types,
11+
`is_single` and `is_array` for `Peripheral`, `Cluster`, `Register` and `Field`
1012
- Add array support for peripherals
1113

1214
## [v0.11.2] - 2021-11-04

svd-rs/src/cluster.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@ impl Deref for Cluster {
1616

1717
fn deref(&self) -> &ClusterInfo {
1818
match self {
19-
Cluster::Single(info) => info,
20-
Cluster::Array(info, _) => info,
19+
Self::Single(info) => info,
20+
Self::Array(info, _) => info,
2121
}
2222
}
2323
}
2424

2525
impl DerefMut for Cluster {
2626
fn deref_mut(&mut self) -> &mut ClusterInfo {
2727
match self {
28-
Cluster::Single(info) => info,
29-
Cluster::Array(info, _) => info,
28+
Self::Single(info) => info,
29+
Self::Array(info, _) => info,
3030
}
3131
}
3232
}
3333

34+
impl Cluster {
35+
/// Return `true` if cluster instance is single
36+
pub const fn is_single(&self) -> bool {
37+
matches!(self, Self::Single(_))
38+
}
39+
/// Return `true` if it is cluster array
40+
pub const fn is_array(&self) -> bool {
41+
matches!(self, Self::Array(_, _))
42+
}
43+
}
44+
3445
#[cfg(feature = "serde")]
3546
mod ser_de {
3647
use super::*;
@@ -53,8 +64,8 @@ mod ser_de {
5364
S: Serializer,
5465
{
5566
match self {
56-
Cluster::Single(info) => info.serialize(serializer),
57-
Cluster::Array(info, dim) => ClusterArray {
67+
Self::Single(info) => info.serialize(serializer),
68+
Self::Array(info, dim) => ClusterArray {
5869
dim: Some(dim.clone()),
5970
info: info.clone(),
6071
}
@@ -70,9 +81,9 @@ mod ser_de {
7081
{
7182
let ClusterArray { dim, info } = ClusterArray::deserialize(deserializer)?;
7283
if let Some(dim) = dim {
73-
Ok(Cluster::Array(info, dim))
84+
Ok(info.array(dim))
7485
} else {
75-
Ok(Cluster::Single(info))
86+
Ok(info.single())
7687
}
7788
}
7889
}

svd-rs/src/clusterinfo.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{
22
register::{RegIter, RegIterMut},
3-
BuildError, EmptyToNone, RegisterCluster, RegisterProperties, SvdError, ValidateLevel,
3+
BuildError, Cluster, DimElement, EmptyToNone, RegisterCluster, RegisterProperties, SvdError,
4+
ValidateLevel,
45
};
56

67
/// Errors from [`ClusterInfo::validate`]
@@ -149,7 +150,14 @@ impl ClusterInfo {
149150
pub fn builder() -> ClusterInfoBuilder {
150151
ClusterInfoBuilder::default()
151152
}
152-
153+
/// Construct single [`Cluster`]
154+
pub const fn single(self) -> Cluster {
155+
Cluster::Single(self)
156+
}
157+
/// Construct [`Cluster`] array
158+
pub const fn array(self, dim: DimElement) -> Cluster {
159+
Cluster::Array(self, dim)
160+
}
153161
/// Modify an existing [`ClusterInfo`] based on a [builder](ClusterInfoBuilder).
154162
pub fn modify_from(
155163
&mut self,
@@ -200,9 +208,7 @@ impl ClusterInfo {
200208
}
201209
Ok(())
202210
}
203-
}
204211

205-
impl ClusterInfo {
206212
/// returns a iterator over all registers the cluster contains
207213
pub fn reg_iter(&self) -> RegIter {
208214
let mut rem: Vec<&RegisterCluster> = Vec::with_capacity(self.children.len());

svd-rs/src/field.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,32 @@ impl Deref for Field {
1515

1616
fn deref(&self) -> &FieldInfo {
1717
match self {
18-
Field::Single(info) => info,
19-
Field::Array(info, _) => info,
18+
Self::Single(info) => info,
19+
Self::Array(info, _) => info,
2020
}
2121
}
2222
}
2323

2424
impl DerefMut for Field {
2525
fn deref_mut(&mut self) -> &mut FieldInfo {
2626
match self {
27-
Field::Single(info) => info,
28-
Field::Array(info, _) => info,
27+
Self::Single(info) => info,
28+
Self::Array(info, _) => info,
2929
}
3030
}
3131
}
3232

33+
impl Field {
34+
/// Return `true` if field instance is single
35+
pub const fn is_single(&self) -> bool {
36+
matches!(self, Self::Single(_))
37+
}
38+
/// Return `true` if it is field array
39+
pub const fn is_array(&self) -> bool {
40+
matches!(self, Self::Array(_, _))
41+
}
42+
}
43+
3344
#[cfg(feature = "serde")]
3445
mod ser_de {
3546
use super::*;
@@ -52,8 +63,8 @@ mod ser_de {
5263
S: Serializer,
5364
{
5465
match self {
55-
Field::Single(info) => info.serialize(serializer),
56-
Field::Array(info, dim) => FieldArray {
66+
Self::Single(info) => info.serialize(serializer),
67+
Self::Array(info, dim) => FieldArray {
5768
dim: Some(dim.clone()),
5869
info: info.clone(),
5970
}
@@ -69,9 +80,9 @@ mod ser_de {
6980
{
7081
let FieldArray { dim, info } = FieldArray::deserialize(deserializer)?;
7182
if let Some(dim) = dim {
72-
Ok(Field::Array(info, dim))
83+
Ok(info.array(dim))
7384
} else {
74-
Ok(Field::Single(info))
85+
Ok(info.single())
7586
}
7687
}
7788
}

svd-rs/src/fieldinfo.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
bitrange, Access, BitRange, BuildError, EmptyToNone, EnumeratedValues, ModifiedWriteValues,
3-
SvdError, Usage, ValidateLevel, WriteConstraint,
2+
bitrange, Access, BitRange, BuildError, DimElement, EmptyToNone, EnumeratedValues, Field,
3+
ModifiedWriteValues, SvdError, Usage, ValidateLevel, WriteConstraint,
44
};
55

66
/// Errors for [`FieldInfo::validate`]
@@ -198,6 +198,14 @@ impl FieldInfo {
198198
pub fn builder() -> FieldInfoBuilder {
199199
FieldInfoBuilder::default()
200200
}
201+
/// Construct single [`Field`]
202+
pub const fn single(self) -> Field {
203+
Field::Single(self)
204+
}
205+
/// Construct [`Field`] array
206+
pub const fn array(self, dim: DimElement) -> Field {
207+
Field::Array(self, dim)
208+
}
201209
/// Modify an existing [`FieldInfo`] based on a [builder](FieldInfoBuilder).
202210
pub fn modify_from(
203211
&mut self,

svd-rs/src/peripheral.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@ impl Deref for Peripheral {
1616

1717
fn deref(&self) -> &PeripheralInfo {
1818
match self {
19-
Peripheral::Single(info) => info,
20-
Peripheral::Array(info, _) => info,
19+
Self::Single(info) => info,
20+
Self::Array(info, _) => info,
2121
}
2222
}
2323
}
2424

2525
impl DerefMut for Peripheral {
2626
fn deref_mut(&mut self) -> &mut PeripheralInfo {
2727
match self {
28-
Peripheral::Single(info) => info,
29-
Peripheral::Array(info, _) => info,
28+
Self::Single(info) => info,
29+
Self::Array(info, _) => info,
3030
}
3131
}
3232
}
3333

34+
impl Peripheral {
35+
/// Return `true` if peripheral instance is single
36+
pub const fn is_single(&self) -> bool {
37+
matches!(self, Self::Single(_))
38+
}
39+
/// Return `true` if it is peripheral array
40+
pub const fn is_array(&self) -> bool {
41+
matches!(self, Self::Array(_, _))
42+
}
43+
}
44+
3445
#[cfg(feature = "serde")]
3546
mod ser_de {
3647
use super::*;
@@ -53,8 +64,8 @@ mod ser_de {
5364
S: Serializer,
5465
{
5566
match self {
56-
Peripheral::Single(info) => info.serialize(serializer),
57-
Peripheral::Array(info, dim) => PeripheralArray {
67+
Self::Single(info) => info.serialize(serializer),
68+
Self::Array(info, dim) => PeripheralArray {
5869
dim: Some(dim.clone()),
5970
info: info.clone(),
6071
}
@@ -70,9 +81,9 @@ mod ser_de {
7081
{
7182
let PeripheralArray { dim, info } = PeripheralArray::deserialize(deserializer)?;
7283
if let Some(dim) = dim {
73-
Ok(Peripheral::Array(info, dim))
84+
Ok(info.array(dim))
7485
} else {
75-
Ok(Peripheral::Single(info))
86+
Ok(info.single())
7687
}
7788
}
7889
}

0 commit comments

Comments
 (0)