libfdt_rs/
iter.rs

1use crate::error::Error;
2use crate::{Fdt, FdtNode, FdtProperty};
3
4/// An iterator over the subnodes of a parent node.
5pub struct FdtNodeIter<'fdt> {
6    fdt: &'fdt Fdt,
7    next: Option<FdtNode<'fdt>>,
8}
9
10/// An iterator over the properties of a node.
11pub struct FdtPropertyIter<'fdt> {
12    fdt: &'fdt Fdt,
13    next: Option<FdtProperty<'fdt>>,
14}
15
16impl<'fdt> FdtNodeIter<'fdt> {
17    /// Create a new [`FdtNodeIter`] iterator, given the parent node.
18    pub fn new(node: &FdtNode<'fdt>) -> Result<Self, Error> {
19        Ok(Self {
20            fdt: node.fdt,
21            next: node.fdt.first_subnode(node)?,
22        })
23    }
24}
25
26impl<'fdt> FdtPropertyIter<'fdt> {
27    /// Create a new [`FdtPropertyIter`] iterator, given the parent node.
28    pub fn new(node: &FdtNode<'fdt>) -> Result<Self, Error> {
29        Ok(Self {
30            fdt: node.fdt,
31            next: node.fdt.first_property(node)?,
32        })
33    }
34}
35
36impl<'fdt> Iterator for FdtNodeIter<'fdt> {
37    type Item = FdtNode<'fdt>;
38
39    fn next(&mut self) -> Option<Self::Item> {
40        if let Some(current) = self.next.take() {
41            self.next = self.fdt.next_subnode(&current).unwrap();
42            Some(current)
43        } else {
44            None
45        }
46    }
47}
48
49impl<'fdt> Iterator for FdtPropertyIter<'fdt> {
50    type Item = FdtProperty<'fdt>;
51
52    fn next(&mut self) -> Option<Self::Item> {
53        if let Some(current) = self.next.take() {
54            self.next = self.fdt.next_property(&current).unwrap();
55            Some(current)
56        } else {
57            None
58        }
59    }
60}