Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add get_register, etc, release 0.12.1 #179

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion svd-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

- Rename `reg_iter` to `all_registers`, add `registers`, `clusters` methods that create iterators
## [v0.12.1] - 2021-12-08

- Rename `reg_iter` to `all_registers`,
- Add `registers`, `clusters`, `fields` methods that create iterators
- Add `get_register`, `get_cluster`, `get_field` (also `mut`) which take child by name

## [v0.12.0] - 2021-11-11

Expand Down
2 changes: 1 addition & 1 deletion svd-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0"
name = "svd-rs"
repository = "https://github.com/rust-embedded/svd"
edition = "2018"
version = "0.12.0"
version = "0.12.1"
readme = "README.md"

[features]
Expand Down
24 changes: 22 additions & 2 deletions svd-rs/src/clusterinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use super::{
AllRegistersIter, AllRegistersIterMut, ClusterIter, ClusterIterMut, RegisterIter,
RegisterIterMut,
},
BuildError, Cluster, DimElement, EmptyToNone, RegisterCluster, RegisterProperties, SvdError,
ValidateLevel,
BuildError, Cluster, DimElement, EmptyToNone, Register, RegisterCluster, RegisterProperties,
SvdError, ValidateLevel,
};

/// Errors from [`ClusterInfo::validate`]
Expand Down Expand Up @@ -286,4 +286,24 @@ impl ClusterInfo {
all: self.children.iter_mut(),
}
}

/// Get register by name
pub fn get_register(&self, name: &str) -> Option<&Register> {
self.registers().find(|f| f.name == name)
}

/// Get mutable register by name
pub fn get_mut_register(&mut self, name: &str) -> Option<&mut Register> {
self.registers_mut().find(|f| f.name == name)
}

/// Get cluster by name
pub fn get_cluster(&self, name: &str) -> Option<&Cluster> {
self.clusters().find(|f| f.name == name)
}

/// Get mutable cluster by name
pub fn get_mut_cluster(&mut self, name: &str) -> Option<&mut Cluster> {
self.clusters_mut().find(|f| f.name == name)
}
}
25 changes: 25 additions & 0 deletions svd-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,28 @@ struct DeserArray<T> {
#[serde(flatten)]
info: T,
}

/// Iterates over optional iterator
pub struct OptIter<I>(Option<I>)
where
I: Iterator;

impl<I> OptIter<I>
where
I: Iterator,
{
/// Create new optional iterator
pub fn new(o: Option<I>) -> Self {
Self(o)
}
}

impl<'a, I> Iterator for OptIter<I>
where
I: Iterator,
{
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
self.0.as_mut().and_then(I::next)
}
}
26 changes: 23 additions & 3 deletions svd-rs/src/peripheralinfo.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::{
registercluster::{
AllRegistersIter, AllRegistersIterMut, ClusterIter, ClusterIterMut, OptIter, RegisterIter,
AllRegistersIter, AllRegistersIterMut, ClusterIter, ClusterIterMut, RegisterIter,
RegisterIterMut,
},
AddressBlock, BuildError, DimElement, EmptyToNone, Interrupt, Peripheral, RegisterCluster,
RegisterProperties, SvdError, ValidateLevel,
AddressBlock, BuildError, Cluster, DimElement, EmptyToNone, Interrupt, OptIter, Peripheral,
Register, RegisterCluster, RegisterProperties, SvdError, ValidateLevel,
};

/// Errors from [Peripheral::validate]
Expand Down Expand Up @@ -429,4 +429,24 @@ impl PeripheralInfo {
AllRegistersIterMut { rem: Vec::new() }
}
}

/// Get register by name
pub fn get_register(&self, name: &str) -> Option<&Register> {
self.registers().find(|f| f.name == name)
}

/// Get mutable register by name
pub fn get_mut_register(&mut self, name: &str) -> Option<&mut Register> {
self.registers_mut().find(|f| f.name == name)
}

/// Get cluster by name
pub fn get_cluster(&self, name: &str) -> Option<&Cluster> {
self.clusters().find(|f| f.name == name)
}

/// Get mutable cluster by name
pub fn get_mut_cluster(&mut self, name: &str) -> Option<&mut Cluster> {
self.clusters_mut().find(|f| f.name == name)
}
}
25 changes: 0 additions & 25 deletions svd-rs/src/registercluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,3 @@ impl<'a> std::iter::Iterator for AllRegistersIterMut<'a> {
None
}
}

/// Iterates over optional iterator
pub struct OptIter<I>(Option<I>)
where
I: Iterator;

impl<I> OptIter<I>
where
I: Iterator,
{
/// Create new optional iterator
pub fn new(o: Option<I>) -> Self {
Self(o)
}
}

impl<'a, I> Iterator for OptIter<I>
where
I: Iterator,
{
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
self.0.as_mut().and_then(I::next)
}
}
24 changes: 22 additions & 2 deletions svd-rs/src/registerinfo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
Access, BuildError, DimElement, EmptyToNone, Field, ModifiedWriteValues, ReadAction, Register,
RegisterProperties, SvdError, ValidateLevel, WriteConstraint,
Access, BuildError, DimElement, EmptyToNone, Field, ModifiedWriteValues, OptIter, ReadAction,
Register, RegisterProperties, SvdError, ValidateLevel, WriteConstraint,
};

/// Errors from [`RegisterInfo::validate`]
Expand Down Expand Up @@ -328,4 +328,24 @@ impl RegisterInfo {
}
Ok(())
}

/// Returns iterator over child fields
pub fn fields(&self) -> OptIter<std::slice::Iter<Field>> {
OptIter::new(self.fields.as_ref().map(|fields| fields.iter()))
}

/// Returns mutable iterator over child fields
pub fn fields_mut(&mut self) -> OptIter<std::slice::IterMut<Field>> {
OptIter::new(self.fields.as_mut().map(|fields| fields.iter_mut()))
}

/// Get field by name
pub fn get_field(&self, name: &str) -> Option<&Field> {
self.fields().find(|f| f.name == name)
}

/// Get mutable field by name
pub fn get_mut_field(&mut self, name: &str) -> Option<&mut Field> {
self.fields_mut().find(|f| f.name == name)
}
}