Skip to content

Commit

Permalink
feat: add deserialize for Node
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 6590276 commit 0b0e315
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/qemu-virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ fn main() -> Result<(), Error> {

{
// 解析!
let t: Tree = from_raw_mut(&dtb).unwrap();
let root: Node = from_raw_mut(&dtb).unwrap();
let t: Tree = root.deserialize();

println!("model = {:?}", t.model);
println!("compatible = {:?}", t.compatible);
Expand Down
18 changes: 18 additions & 0 deletions src/de_mut/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::{de, Deserialize};
pub struct Node<'de> {
dtb: RefDtb<'de>,
reg: RegConfig,
cursor: BodyCursor,
props_start: Option<BodyCursor>,
nodes_start: Option<BodyCursor>,
}
Expand Down Expand Up @@ -48,6 +49,15 @@ pub struct PropItem<'de> {
}

impl<'de> Node<'de> {
pub fn deserialize<T: Deserialize<'de>>(&self) -> T {
use super::ValueCursor;
T::deserialize(&mut ValueDeserializer {
dtb: self.dtb,
reg: self.reg,
cursor: ValueCursor::Body(self.cursor),
})
.unwrap()
}
// TODO: Maybe use BTreeMap when have alloc
/// 获得节点迭代器。
pub fn nodes<'b>(&'b self) -> NodeIter<'de, 'b> {
Expand Down Expand Up @@ -179,10 +189,17 @@ impl<'de> Deserialize<'de> for Node<'_> {
let mut reg: Option<RegConfig> = None;
let mut props_start: Option<BodyCursor> = None;
let mut nodes_start: Option<BodyCursor> = None;
let mut self_cursor: Option<BodyCursor> = None;
while let Some((key, value)) = access.next_entry::<&str, ValueDeserializer<'b>>()? {
dtb = Some(value.dtb);
reg = Some(value.reg);
if key == "/" {
self_cursor = match value.cursor {
ValueCursor::Body(cursor) => Some(cursor),
ValueCursor::Prop(_, _) => {
unreachable!("root of NodeSeq shouble be body cursor")
}
};
continue;
}
match value.cursor {
Expand All @@ -202,6 +219,7 @@ impl<'de> Deserialize<'de> for Node<'_> {
Ok(Node {
dtb: dtb.unwrap(),
reg: reg.unwrap(),
cursor: self_cursor.unwrap(),
nodes_start,
props_start,
})
Expand Down

0 comments on commit 0b0e315

Please sign in to comment.