Skip to content

Commit 097e294

Browse files
committed
feat: add Node::get_prop for find prop
Signed-off-by: Woshiluo Luo <woshiluo.luo@outlook.com>
1 parent 0b0e315 commit 097e294

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/de_mut/node.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ impl<'de> Node<'de> {
7676
i: 0,
7777
}
7878
}
79+
80+
/// 尝试获得指定属性
81+
pub fn get_prop<'b>(&'b self, name: &str) -> Option<PropItem<'b>> {
82+
self.props().find(|prop| prop.get_name() == name)
83+
}
7984
}
8085

8186
impl Debug for Node<'_> {
@@ -283,3 +288,28 @@ impl<'de> PropItem<'de> {
283288
.unwrap()
284289
}
285290
}
291+
292+
#[cfg(test)]
293+
mod tests {
294+
use crate::{buildin::Node, from_raw_mut, Dtb, DtbPtr};
295+
const RAW_DEVICE_TREE: &[u8] = include_bytes!("../../examples/hifive-unmatched-a00.dtb");
296+
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
297+
#[repr(align(8))]
298+
struct AlignedBuffer {
299+
pub data: [u8; RAW_DEVICE_TREE.len()],
300+
}
301+
#[test]
302+
fn test_find_prop() {
303+
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
304+
data: [0; BUFFER_SIZE],
305+
});
306+
aligned_data.data[..BUFFER_SIZE].clone_from_slice(RAW_DEVICE_TREE);
307+
let mut slice = aligned_data.data.to_vec();
308+
let ptr = DtbPtr::from_raw(slice.as_mut_ptr()).unwrap();
309+
let dtb = Dtb::from(ptr).share();
310+
311+
let node: Node = from_raw_mut(&dtb).unwrap();
312+
let prop = node.get_prop("compatible");
313+
assert!(prop.is_some());
314+
}
315+
}

0 commit comments

Comments
 (0)