Skip to content

Commit 08c8fe8

Browse files
bors[bot]burrbull
andauthored
Merge #189
189: rename SingleArray -> MaybeArray r=Emilgardis a=burrbull cc `@Emilgardis` What you think about this? Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents 1afd5cb + ae563b0 commit 08c8fe8

File tree

9 files changed

+38
-32
lines changed

9 files changed

+38
-32
lines changed

svd-parser/src/array.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
2-
use svd_rs::{DimElement, Name, SingleArray};
2+
use svd_rs::{DimElement, MaybeArray, Name};
33

4-
pub fn parse_array<T>(tag: &str, tree: &Node, config: &Config) -> Result<SingleArray<T>, SVDErrorAt>
4+
pub fn parse_array<T>(tag: &str, tree: &Node, config: &Config) -> Result<MaybeArray<T>, SVDErrorAt>
55
where
66
T: Parse<Object = T, Error = SVDErrorAt, Config = Config> + Name,
77
{
@@ -23,8 +23,8 @@ where
2323
.at(tree.id()));
2424
}
2525
}
26-
Ok(SingleArray::Array(info, array_info))
26+
Ok(MaybeArray::Array(info, array_info))
2727
} else {
28-
Ok(SingleArray::Single(info))
28+
Ok(MaybeArray::Single(info))
2929
}
3030
}

svd-rs/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## Unreleased
99

1010
- skip serializing `values` in `EnumeratedValues` if empty
11+
- add `names` function for arrays
1112
- add missing fields in `Device`, require `version`, `description`, `address_unit_bits` and `width`,
1213
also `schema_version` is required, but skipped during (de)serialization
1314
- merge `register` with `registerinfo` modules same as other `info`s
1415
- camelCase for WriteConstraint serialization
1516
- `EnumeratedValues.usage()` now return `None` if it is derived, fix bug in usage check
16-
- Use generic `SingleArray` enum for types which can be either collected into SVD arrays or have only one instance
17+
- Use generic `MaybeArray` enum for types which can be either collected into SVD arrays or have only one instance
1718
- `Name` trait for structures that has `name` field
1819
- improves in iterators
1920
- `get_enumerated_values` by usage

svd-rs/src/array.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use core::ops::{Deref, DerefMut};
33

44
/// A single SVD instance or array of instances
55
#[derive(Clone, Debug, PartialEq)]
6-
pub enum SingleArray<T> {
6+
pub enum MaybeArray<T> {
77
/// A single instance
88
Single(T),
99
/// An array of instances
1010
Array(T, DimElement),
1111
}
1212

13-
impl<T> Deref for SingleArray<T> {
13+
impl<T> Deref for MaybeArray<T> {
1414
type Target = T;
1515

1616
fn deref(&self) -> &T {
@@ -21,7 +21,7 @@ impl<T> Deref for SingleArray<T> {
2121
}
2222
}
2323

24-
impl<T> DerefMut for SingleArray<T> {
24+
impl<T> DerefMut for MaybeArray<T> {
2525
fn deref_mut(&mut self) -> &mut T {
2626
match self {
2727
Self::Single(info) => info,
@@ -30,7 +30,7 @@ impl<T> DerefMut for SingleArray<T> {
3030
}
3131
}
3232

33-
impl<T> SingleArray<T> {
33+
impl<T> MaybeArray<T> {
3434
/// Return `true` if instance is single
3535
pub const fn is_single(&self) -> bool {
3636
matches!(self, Self::Single(_))
@@ -41,7 +41,7 @@ impl<T> SingleArray<T> {
4141
}
4242
}
4343

44-
impl<T> Name for SingleArray<T>
44+
impl<T> Name for MaybeArray<T>
4545
where
4646
T: Name,
4747
{
@@ -50,6 +50,13 @@ where
5050
}
5151
}
5252

53+
/// Return list of names of instances in array
54+
pub fn names<'a, T: Name>(info: &'a T, dim: &'a DimElement) -> impl Iterator<Item = String> + 'a {
55+
let name = info.name();
56+
dim.indexes()
57+
.map(move |i| name.replace("[%s]", &i).replace("%s", &i))
58+
}
59+
5360
#[cfg(feature = "serde")]
5461
mod ser_de {
5562
use super::*;
@@ -71,7 +78,7 @@ mod ser_de {
7178
info: T,
7279
}
7380

74-
impl<T> Serialize for SingleArray<T>
81+
impl<T> Serialize for MaybeArray<T>
7582
where
7683
T: Serialize,
7784
{
@@ -86,7 +93,7 @@ mod ser_de {
8693
}
8794
}
8895

89-
impl<'de, T> Deserialize<'de> for SingleArray<T>
96+
impl<'de, T> Deserialize<'de> for MaybeArray<T>
9097
where
9198
T: Deserialize<'de>,
9299
{

svd-rs/src/cluster.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use super::{
2-
array::SingleArray,
32
registercluster::{
43
AllRegistersIter, AllRegistersIterMut, ClusterIter, ClusterIterMut, RegisterIter,
54
RegisterIterMut,
65
},
7-
BuildError, DimElement, EmptyToNone, Name, Register, RegisterCluster, RegisterProperties,
8-
SvdError, ValidateLevel,
6+
BuildError, DimElement, EmptyToNone, MaybeArray, Name, Register, RegisterCluster,
7+
RegisterProperties, SvdError, ValidateLevel,
98
};
109

1110
/// Cluster describes a sequence of neighboring registers within a peripheral.
12-
pub type Cluster = SingleArray<ClusterInfo>;
11+
pub type Cluster = MaybeArray<ClusterInfo>;
1312

1413
/// Errors from [`ClusterInfo::validate`]
1514
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]

svd-rs/src/derive_from.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Implementations of DeriveFrom, setting non-explicit fields.
22
use crate::{
3-
ClusterInfo, EnumeratedValues, FieldInfo, PeripheralInfo, RegisterInfo, RegisterProperties,
4-
SingleArray,
3+
ClusterInfo, EnumeratedValues, FieldInfo, MaybeArray, PeripheralInfo, RegisterInfo,
4+
RegisterProperties,
55
};
66

77
/// Fill empty fields of structure with values of other structure
@@ -107,7 +107,7 @@ impl DeriveFrom for FieldInfo {
107107
}
108108
}
109109

110-
impl<T> DeriveFrom for SingleArray<T>
110+
impl<T> DeriveFrom for MaybeArray<T>
111111
where
112112
T: DeriveFrom,
113113
{

svd-rs/src/field.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use super::{
2-
array::SingleArray, bitrange, Access, BitRange, BuildError, DimElement, EmptyToNone,
3-
EnumeratedValues, ModifiedWriteValues, Name, ReadAction, SvdError, Usage, ValidateLevel,
4-
WriteConstraint,
2+
bitrange, Access, BitRange, BuildError, DimElement, EmptyToNone, EnumeratedValues, MaybeArray,
3+
ModifiedWriteValues, Name, ReadAction, SvdError, Usage, ValidateLevel, WriteConstraint,
54
};
65

76
/// Describes a field or fields of a [register](crate::RegisterInfo).
8-
pub type Field = SingleArray<FieldInfo>;
7+
pub type Field = MaybeArray<FieldInfo>;
98

109
/// Errors for [`FieldInfo::validate`]
1110
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]

svd-rs/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
//! SVD objects.
33
//! This module defines components of an SVD along with parse and encode implementations
44
5-
mod array;
6-
pub use array::SingleArray;
5+
/// Common things for structures which can be collected in arrays
6+
pub mod array;
7+
pub use array::MaybeArray;
78

89
/// Endian objects
910
pub mod endian;

svd-rs/src/peripheral.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use super::{
2-
array::SingleArray,
32
registercluster::{
43
AllRegistersIter, AllRegistersIterMut, ClusterIter, ClusterIterMut, RegisterIter,
54
RegisterIterMut,
65
},
7-
AddressBlock, BuildError, Cluster, DimElement, EmptyToNone, Interrupt, Name, Register,
8-
RegisterCluster, RegisterProperties, SvdError, ValidateLevel,
6+
AddressBlock, BuildError, Cluster, DimElement, EmptyToNone, Interrupt, MaybeArray, Name,
7+
Register, RegisterCluster, RegisterProperties, SvdError, ValidateLevel,
98
};
109

1110
/// A single peripheral or array of peripherals
12-
pub type Peripheral = SingleArray<PeripheralInfo>;
11+
pub type Peripheral = MaybeArray<PeripheralInfo>;
1312

1413
/// Errors from [Peripheral::validate]
1514
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]

svd-rs/src/register.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::{
2-
array::SingleArray, Access, BuildError, DimElement, EmptyToNone, Field, ModifiedWriteValues,
3-
Name, ReadAction, RegisterProperties, SvdError, ValidateLevel, WriteConstraint,
2+
Access, BuildError, DimElement, EmptyToNone, Field, MaybeArray, ModifiedWriteValues, Name,
3+
ReadAction, RegisterProperties, SvdError, ValidateLevel, WriteConstraint,
44
};
55

66
/// A single register or array of registers. A register is a named, programmable resource that belongs to a [peripheral](crate::Peripheral).
7-
pub type Register = SingleArray<RegisterInfo>;
7+
pub type Register = MaybeArray<RegisterInfo>;
88

99
/// Errors from [`RegisterInfo::validate`]
1010
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]

0 commit comments

Comments
 (0)