diff --git a/svd-rs/CHANGELOG.md b/svd-rs/CHANGELOG.md index 6a19f959..83ad568a 100644 --- a/svd-rs/CHANGELOG.md +++ b/svd-rs/CHANGELOG.md @@ -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 diff --git a/svd-rs/Cargo.toml b/svd-rs/Cargo.toml index f16c0676..7fc1e24b 100644 --- a/svd-rs/Cargo.toml +++ b/svd-rs/Cargo.toml @@ -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] diff --git a/svd-rs/src/clusterinfo.rs b/svd-rs/src/clusterinfo.rs index c4bbbc7d..989af573 100644 --- a/svd-rs/src/clusterinfo.rs +++ b/svd-rs/src/clusterinfo.rs @@ -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`] @@ -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) + } } diff --git a/svd-rs/src/lib.rs b/svd-rs/src/lib.rs index 27e31c7c..c097a798 100644 --- a/svd-rs/src/lib.rs +++ b/svd-rs/src/lib.rs @@ -256,3 +256,28 @@ struct DeserArray { #[serde(flatten)] info: T, } + +/// Iterates over optional iterator +pub struct OptIter(Option) +where + I: Iterator; + +impl OptIter +where + I: Iterator, +{ + /// Create new optional iterator + pub fn new(o: Option) -> Self { + Self(o) + } +} + +impl<'a, I> Iterator for OptIter +where + I: Iterator, +{ + type Item = I::Item; + fn next(&mut self) -> Option { + self.0.as_mut().and_then(I::next) + } +} diff --git a/svd-rs/src/peripheralinfo.rs b/svd-rs/src/peripheralinfo.rs index 2133ae77..0c6457c3 100644 --- a/svd-rs/src/peripheralinfo.rs +++ b/svd-rs/src/peripheralinfo.rs @@ -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] @@ -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) + } } diff --git a/svd-rs/src/registercluster.rs b/svd-rs/src/registercluster.rs index e4b4e197..e8c796af 100644 --- a/svd-rs/src/registercluster.rs +++ b/svd-rs/src/registercluster.rs @@ -137,28 +137,3 @@ impl<'a> std::iter::Iterator for AllRegistersIterMut<'a> { None } } - -/// Iterates over optional iterator -pub struct OptIter(Option) -where - I: Iterator; - -impl OptIter -where - I: Iterator, -{ - /// Create new optional iterator - pub fn new(o: Option) -> Self { - Self(o) - } -} - -impl<'a, I> Iterator for OptIter -where - I: Iterator, -{ - type Item = I::Item; - fn next(&mut self) -> Option { - self.0.as_mut().and_then(I::next) - } -} diff --git a/svd-rs/src/registerinfo.rs b/svd-rs/src/registerinfo.rs index 28fcd05e..e606aec1 100644 --- a/svd-rs/src/registerinfo.rs +++ b/svd-rs/src/registerinfo.rs @@ -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`] @@ -328,4 +328,24 @@ impl RegisterInfo { } Ok(()) } + + /// Returns iterator over child fields + pub fn fields(&self) -> OptIter> { + OptIter::new(self.fields.as_ref().map(|fields| fields.iter())) + } + + /// Returns mutable iterator over child fields + pub fn fields_mut(&mut self) -> OptIter> { + 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) + } }