1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use svd_rs::Name;

use super::matchname;

pub struct MatchIter<'b, I>
where
    I: Iterator,
    I::Item: Name,
{
    it: I,
    spec: &'b str,
}

impl<'b, I> Iterator for MatchIter<'b, I>
where
    I: Iterator,
    I::Item: Name,
{
    type Item = I::Item;
    fn next(&mut self) -> Option<Self::Item> {
        self.it
            .by_ref()
            .find(|next| matchname(next.name(), self.spec))
    }
}

pub trait Matched
where
    Self: Iterator + Sized,
    Self::Item: Name,
{
    fn matched(self, spec: &str) -> MatchIter<Self>;
}

impl<I> Matched for I
where
    Self: Iterator + Sized,
    Self::Item: Name,
{
    fn matched(self, spec: &str) -> MatchIter<Self> {
        MatchIter { it: self, spec }
    }
}

/// 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<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)
    }
}