11use super :: serializer:: Serializer ;
2+ use super :: serializer:: ValueType ;
23use core:: cell:: Cell ;
34
45/// Since this crate is mostly work with `noalloc`, we use `Patch` and `PatchList` for change or
56/// add on a dtb.
67pub struct Patch < ' se > {
7- pub data : & ' se dyn dyn_serde:: Serialize ,
88 name : & ' se str ,
9+ pub data : & ' se dyn dyn_serde:: Serialize ,
10+ pub patch_type : ValueType ,
911
1012 /// This patch match how many item between its path and serializer.
1113 matched_depth : Cell < usize > ,
1214 /// Show this patch have been parsed.
1315 parsed : Cell < bool > ,
1416}
1517
18+ impl core:: fmt:: Debug for Patch < ' _ > {
19+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
20+ f. debug_struct ( "" )
21+ . field ( "name" , & self . name )
22+ . field ( "patch_type" , & self . patch_type )
23+ . field ( "matched_depth" , & self . matched_depth )
24+ . field ( "parsed" , & self . parsed )
25+ . finish ( )
26+ }
27+ }
28+
1629impl < ' se > Patch < ' se > {
1730 #[ inline( always) ]
18- pub fn new ( name : & ' se str , data : & ' se dyn dyn_serde:: Serialize ) -> Patch < ' se > {
31+ pub fn new (
32+ name : & ' se str ,
33+ data : & ' se dyn dyn_serde:: Serialize ,
34+ patch_type : ValueType ,
35+ ) -> Patch < ' se > {
1936 Patch {
2037 name,
2138 data,
22- matched_depth : Cell :: new ( 0 ) ,
39+ patch_type,
40+ matched_depth : Cell :: new ( 1 ) ,
2341 parsed : Cell :: new ( false ) ,
2442 }
2543 }
2644
2745 #[ inline( always) ]
2846 /// Reset the status of patch.
2947 pub fn init ( & self ) {
30- self . matched_depth . set ( 0 ) ;
48+ self . matched_depth . set ( 1 ) ;
3149 self . parsed . set ( false ) ;
3250 }
3351
@@ -38,17 +56,14 @@ impl<'se> Patch<'se> {
3856
3957 #[ inline( always) ]
4058 pub fn get_depth_path ( & self , x : usize ) -> & ' se str {
41- if x == 0 {
42- return "" ;
43- }
44- self . name . split ( '/' ) . nth ( x) . unwrap_or_default ( )
59+ self . name . split ( '/' ) . nth ( x - 1 ) . unwrap_or_default ( )
4560 }
4661
4762 // I hope to impl serde::ser::Serializer, but erase_serialize's return value is different from
4863 // normal serialize, so we do this.
4964 /// Serialize this patch with serializer.
5065 #[ inline( always) ]
51- pub fn serialize ( & self , serializer : & mut Serializer < ' se > ) {
66+ pub fn serialize ( & self , serializer : Serializer < ' _ , ' se > ) {
5267 self . parsed . set ( true ) ;
5368 self . data
5469 . serialize_dyn ( & mut <dyn dyn_serde:: Serializer >:: new ( serializer) )
@@ -57,18 +72,20 @@ impl<'se> Patch<'se> {
5772}
5873
5974/// Here is a list of `Patch`, and have some methods for update `Patch` status.
75+ #[ derive( Debug ) ]
6076pub struct PatchList < ' se > {
6177 list : & ' se [ Patch < ' se > ] ,
6278}
6379
6480impl < ' se > PatchList < ' se > {
6581 #[ inline( always) ]
6682 pub fn new ( list : & ' se [ Patch < ' se > ] ) -> PatchList < ' se > {
83+ list. iter ( ) . for_each ( |x| x. init ( ) ) ;
6784 PatchList { list }
6885 }
6986
7087 #[ inline( always) ]
71- pub fn step_forward ( & self , name : & ' se str , depth : usize ) -> Option < & ' se Patch < ' se > > {
88+ pub fn step_forward ( & self , name : & str , depth : usize ) -> Option < & ' se Patch < ' se > > {
7289 let mut matched_patch = None ;
7390 self . list . iter ( ) . for_each ( |patch| {
7491 if patch. matched_depth . get ( ) == depth - 1 && patch. get_depth_path ( depth) == name {
0 commit comments