Skip to content

Commit

Permalink
feat: add Node::get_prop for find prop
Browse files Browse the repository at this point in the history
Signed-off-by: Woshiluo Luo <[email protected]>
  • Loading branch information
woshiluo committed Nov 26, 2024
1 parent 0b0e315 commit 097e294
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/de_mut/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ impl<'de> Node<'de> {
i: 0,
}
}

/// 尝试获得指定属性
pub fn get_prop<'b>(&'b self, name: &str) -> Option<PropItem<'b>> {
self.props().find(|prop| prop.get_name() == name)
}
}

impl Debug for Node<'_> {
Expand Down Expand Up @@ -283,3 +288,28 @@ impl<'de> PropItem<'de> {
.unwrap()
}
}

#[cfg(test)]
mod tests {
use crate::{buildin::Node, from_raw_mut, Dtb, DtbPtr};
const RAW_DEVICE_TREE: &[u8] = include_bytes!("../../examples/hifive-unmatched-a00.dtb");
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
#[repr(align(8))]
struct AlignedBuffer {
pub data: [u8; RAW_DEVICE_TREE.len()],
}
#[test]
fn test_find_prop() {
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
data: [0; BUFFER_SIZE],
});
aligned_data.data[..BUFFER_SIZE].clone_from_slice(RAW_DEVICE_TREE);
let mut slice = aligned_data.data.to_vec();
let ptr = DtbPtr::from_raw(slice.as_mut_ptr()).unwrap();
let dtb = Dtb::from(ptr).share();

let node: Node = from_raw_mut(&dtb).unwrap();
let prop = node.get_prop("compatible");
assert!(prop.is_some());
}
}

0 comments on commit 097e294

Please sign in to comment.