From 097e294b11be0d9da71beef6650278b0d78e10cc Mon Sep 17 00:00:00 2001 From: Woshiluo Luo Date: Tue, 26 Nov 2024 10:32:35 +0800 Subject: [PATCH] feat: add Node::get_prop for find prop Signed-off-by: Woshiluo Luo --- src/de_mut/node.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/de_mut/node.rs b/src/de_mut/node.rs index e130a8b..60667b6 100644 --- a/src/de_mut/node.rs +++ b/src/de_mut/node.rs @@ -76,6 +76,11 @@ impl<'de> Node<'de> { i: 0, } } + + /// 尝试获得指定属性 + pub fn get_prop<'b>(&'b self, name: &str) -> Option> { + self.props().find(|prop| prop.get_name() == name) + } } impl Debug for Node<'_> { @@ -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 = 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()); + } +}