1
1
use super :: serializer:: Serializer ;
2
+ use super :: serializer:: ValueType ;
2
3
use core:: cell:: Cell ;
3
4
4
5
/// Since this crate is mostly work with `noalloc`, we use `Patch` and `PatchList` for change or
5
6
/// add on a dtb.
6
7
pub struct Patch < ' se > {
7
- pub data : & ' se dyn dyn_serde:: Serialize ,
8
8
name : & ' se str ,
9
+ pub data : & ' se dyn dyn_serde:: Serialize ,
10
+ pub patch_type : ValueType ,
9
11
10
12
/// This patch match how many item between its path and serializer.
11
13
matched_depth : Cell < usize > ,
12
14
/// Show this patch have been parsed.
13
15
parsed : Cell < bool > ,
14
16
}
15
17
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
+
16
29
impl < ' se > Patch < ' se > {
17
30
#[ 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 > {
19
36
Patch {
20
37
name,
21
38
data,
22
- matched_depth : Cell :: new ( 0 ) ,
39
+ patch_type,
40
+ matched_depth : Cell :: new ( 1 ) ,
23
41
parsed : Cell :: new ( false ) ,
24
42
}
25
43
}
26
44
27
45
#[ inline( always) ]
28
46
/// Reset the status of patch.
29
47
pub fn init ( & self ) {
30
- self . matched_depth . set ( 0 ) ;
48
+ self . matched_depth . set ( 1 ) ;
31
49
self . parsed . set ( false ) ;
32
50
}
33
51
@@ -38,17 +56,14 @@ impl<'se> Patch<'se> {
38
56
39
57
#[ inline( always) ]
40
58
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 ( )
45
60
}
46
61
47
62
// I hope to impl serde::ser::Serializer, but erase_serialize's return value is different from
48
63
// normal serialize, so we do this.
49
64
/// Serialize this patch with serializer.
50
65
#[ inline( always) ]
51
- pub fn serialize ( & self , serializer : & mut Serializer < ' se > ) {
66
+ pub fn serialize ( & self , serializer : Serializer < ' _ , ' se > ) {
52
67
self . parsed . set ( true ) ;
53
68
self . data
54
69
. serialize_dyn ( & mut <dyn dyn_serde:: Serializer >:: new ( serializer) )
@@ -57,18 +72,20 @@ impl<'se> Patch<'se> {
57
72
}
58
73
59
74
/// Here is a list of `Patch`, and have some methods for update `Patch` status.
75
+ #[ derive( Debug ) ]
60
76
pub struct PatchList < ' se > {
61
77
list : & ' se [ Patch < ' se > ] ,
62
78
}
63
79
64
80
impl < ' se > PatchList < ' se > {
65
81
#[ inline( always) ]
66
82
pub fn new ( list : & ' se [ Patch < ' se > ] ) -> PatchList < ' se > {
83
+ list. iter ( ) . for_each ( |x| x. init ( ) ) ;
67
84
PatchList { list }
68
85
}
69
86
70
87
#[ 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 > > {
72
89
let mut matched_patch = None ;
73
90
self . list . iter ( ) . for_each ( |patch| {
74
91
if patch. matched_depth . get ( ) == depth - 1 && patch. get_depth_path ( depth) == name {
0 commit comments