Skip to content

Commit 0b0e315

Browse files
committed
feat: add deserialize for Node
Signed-off-by: Woshiluo Luo <woshiluo.luo@outlook.com>
1 parent 6590276 commit 0b0e315

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

examples/qemu-virt.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ fn main() -> Result<(), Error> {
103103

104104
{
105105
// 解析!
106-
let t: Tree = from_raw_mut(&dtb).unwrap();
106+
let root: Node = from_raw_mut(&dtb).unwrap();
107+
let t: Tree = root.deserialize();
107108

108109
println!("model = {:?}", t.model);
109110
println!("compatible = {:?}", t.compatible);

src/de_mut/node.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use serde::{de, Deserialize};
1111
pub struct Node<'de> {
1212
dtb: RefDtb<'de>,
1313
reg: RegConfig,
14+
cursor: BodyCursor,
1415
props_start: Option<BodyCursor>,
1516
nodes_start: Option<BodyCursor>,
1617
}
@@ -48,6 +49,15 @@ pub struct PropItem<'de> {
4849
}
4950

5051
impl<'de> Node<'de> {
52+
pub fn deserialize<T: Deserialize<'de>>(&self) -> T {
53+
use super::ValueCursor;
54+
T::deserialize(&mut ValueDeserializer {
55+
dtb: self.dtb,
56+
reg: self.reg,
57+
cursor: ValueCursor::Body(self.cursor),
58+
})
59+
.unwrap()
60+
}
5161
// TODO: Maybe use BTreeMap when have alloc
5262
/// 获得节点迭代器。
5363
pub fn nodes<'b>(&'b self) -> NodeIter<'de, 'b> {
@@ -179,10 +189,17 @@ impl<'de> Deserialize<'de> for Node<'_> {
179189
let mut reg: Option<RegConfig> = None;
180190
let mut props_start: Option<BodyCursor> = None;
181191
let mut nodes_start: Option<BodyCursor> = None;
192+
let mut self_cursor: Option<BodyCursor> = None;
182193
while let Some((key, value)) = access.next_entry::<&str, ValueDeserializer<'b>>()? {
183194
dtb = Some(value.dtb);
184195
reg = Some(value.reg);
185196
if key == "/" {
197+
self_cursor = match value.cursor {
198+
ValueCursor::Body(cursor) => Some(cursor),
199+
ValueCursor::Prop(_, _) => {
200+
unreachable!("root of NodeSeq shouble be body cursor")
201+
}
202+
};
186203
continue;
187204
}
188205
match value.cursor {
@@ -202,6 +219,7 @@ impl<'de> Deserialize<'de> for Node<'_> {
202219
Ok(Node {
203220
dtb: dtb.unwrap(),
204221
reg: reg.unwrap(),
222+
cursor: self_cursor.unwrap(),
205223
nodes_start,
206224
props_start,
207225
})

0 commit comments

Comments
 (0)