@@ -5,18 +5,23 @@ use super::string_block::StringBlock;
55use crate :: common:: * ;
66
77#[ derive( Clone , Copy ) ]
8+ // The enum for current parsing type.
89enum ValueType {
910 Node ,
1011 Prop ,
1112}
1213
14+ /// Serializer
15+ /// - `dst`: Pointer of distance &[u8] and the ref of &[u8].
16+ /// - `current_value_type`, `current_name`, `current_dep`: For recursive.
1317pub struct Serializer < ' se > {
1418 pub dst : & ' se mut Pointer < ' se > ,
1519 string_block : & ' se mut StringBlock < ' se > ,
16- value_type : ValueType ,
20+ patch_list : & ' se mut PatchList < ' se > ,
21+
22+ current_value_type : ValueType ,
1723 current_name : & ' se str ,
1824 current_dep : usize ,
19- patch_list : & ' se mut PatchList < ' se > ,
2025}
2126
2227impl < ' se > Serializer < ' se > {
@@ -31,7 +36,7 @@ impl<'se> Serializer<'se> {
3136 string_block : cache,
3237 current_dep : 0 ,
3338 current_name : "" ,
34- value_type : ValueType :: Node ,
39+ current_value_type : ValueType :: Node ,
3540 patch_list,
3641 }
3742 }
@@ -49,7 +54,9 @@ impl<'se> SerializeDynamicField<'se> for &mut Serializer<'se> {
4954 T : serde:: ser:: Serialize + ?Sized ,
5055 {
5156 let prop_header_offset = self . dst . step_by_prop ( ) ;
52- let prev_type = self . value_type ;
57+
58+ // Save prev
59+ let prev_type = self . current_value_type ;
5360 let prev_name = self . current_name ;
5461 self . current_dep += 1 ;
5562 self . current_name = key;
@@ -66,7 +73,7 @@ impl<'se> SerializeDynamicField<'se> for &mut Serializer<'se> {
6673
6774 // We now know how long the prop value.
6875 // TODO: make we have some better way than put nop, like move this block ahead.
69- if let ValueType :: Node = self . value_type {
76+ if let ValueType :: Node = self . current_value_type {
7077 self . dst
7178 . write_to_offset_u32 ( prop_header_offset - 4 , FDT_NOP ) ;
7279 } else {
@@ -80,10 +87,12 @@ impl<'se> SerializeDynamicField<'se> for &mut Serializer<'se> {
8087 ) ;
8188 }
8289
83- self . value_type = prev_type;
84- self . current_name = prev_name;
8590 self . dst . step_align ( ) ;
91+
92+ // Load prev
8693 self . patch_list . step_back ( self . current_dep ) ;
94+ self . current_value_type = prev_type;
95+ self . current_name = prev_name;
8796 self . current_dep -= 1 ;
8897
8998 Ok ( ( ) )
@@ -258,7 +267,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
258267 }
259268
260269 fn serialize_u32 ( self , v : u32 ) -> Result < Self :: Ok , Self :: Error > {
261- self . value_type = ValueType :: Prop ;
270+ self . current_value_type = ValueType :: Prop ;
262271 self . dst . step_by_u32 ( v) ;
263272 Ok ( ( ) )
264273 }
@@ -280,7 +289,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
280289 }
281290
282291 fn serialize_str ( self , v : & str ) -> Result < Self :: Ok , Self :: Error > {
283- self . value_type = ValueType :: Prop ;
292+ self . current_value_type = ValueType :: Prop ;
284293 v. bytes ( ) . for_each ( |x| {
285294 self . dst . step_by_u8 ( x) ;
286295 } ) ;
@@ -289,7 +298,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
289298 }
290299
291300 fn serialize_bytes ( self , v : & [ u8 ] ) -> Result < Self :: Ok , Self :: Error > {
292- self . value_type = ValueType :: Prop ;
301+ self . current_value_type = ValueType :: Prop ;
293302 v. iter ( ) . for_each ( |x| self . dst . step_by_u8 ( * x) ) ;
294303 Ok ( ( ) )
295304 }
@@ -347,7 +356,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
347356 }
348357
349358 fn serialize_seq ( self , _len : Option < usize > ) -> Result < Self :: SerializeSeq , Self :: Error > {
350- self . value_type = ValueType :: Prop ;
359+ self . current_value_type = ValueType :: Prop ;
351360 Ok ( self )
352361 }
353362
@@ -389,7 +398,7 @@ impl serde::ser::Serializer for &mut Serializer<'_> {
389398 } else {
390399 self . dst . step_by_name ( self . current_name ) ;
391400 }
392- self . value_type = ValueType :: Node ;
401+ self . current_value_type = ValueType :: Node ;
393402 Ok ( self )
394403 }
395404
0 commit comments