1
1
use std:: cmp:: Ordering ;
2
2
3
3
use bstr:: { BString , ByteSlice } ;
4
+ use gix_object:: FindExt ;
4
5
5
6
use crate :: extension:: Tree ;
6
7
@@ -14,8 +15,8 @@ pub enum Error {
14
15
entry_id : gix_hash:: ObjectId ,
15
16
name : BString ,
16
17
} ,
17
- #[ error( "The tree with id {oid} wasn't found in the object database" ) ]
18
- TreeNodeNotFound { oid : gix_hash :: ObjectId } ,
18
+ #[ error( transparent ) ]
19
+ TreeNodeNotFound ( # [ from ] gix_object :: find :: existing_iter :: Error ) ,
19
20
#[ error( "The tree with id {oid} should have {expected_childcount} children, but its cached representation had {actual_childcount} of them" ) ]
20
21
TreeNodeChildcountMismatch {
21
22
oid : gix_hash:: ObjectId ,
@@ -39,20 +40,14 @@ pub enum Error {
39
40
}
40
41
41
42
impl Tree {
42
- ///
43
- pub fn verify < F > ( & self , use_find : bool , mut find : F ) -> Result < ( ) , Error >
44
- where
45
- F : for < ' a > FnMut ( & gix_hash:: oid , & ' a mut Vec < u8 > ) -> Option < gix_object:: TreeRefIter < ' a > > ,
46
- {
47
- fn verify_recursive < F > (
43
+ /// Validate the correctness of this instance. If `use_objects` is true, then `objects` will be used to access all objects.
44
+ pub fn verify ( & self , use_objects : bool , objects : impl gix_object:: Find ) -> Result < ( ) , Error > {
45
+ fn verify_recursive (
48
46
parent_id : gix_hash:: ObjectId ,
49
47
children : & [ Tree ] ,
50
- mut find_buf : Option < & mut Vec < u8 > > ,
51
- find : & mut F ,
52
- ) -> Result < Option < u32 > , Error >
53
- where
54
- F : for < ' a > FnMut ( & gix_hash:: oid , & ' a mut Vec < u8 > ) -> Option < gix_object:: TreeRefIter < ' a > > ,
55
- {
48
+ mut object_buf : Option < & mut Vec < u8 > > ,
49
+ objects : & impl gix_object:: Find ,
50
+ ) -> Result < Option < u32 > , Error > {
56
51
if children. is_empty ( ) {
57
52
return Ok ( None ) ;
58
53
}
@@ -71,8 +66,8 @@ impl Tree {
71
66
}
72
67
prev = Some ( child) ;
73
68
}
74
- if let Some ( buf) = find_buf . as_mut ( ) {
75
- let tree_entries = find ( & parent_id, buf) . ok_or ( Error :: TreeNodeNotFound { oid : parent_id } ) ?;
69
+ if let Some ( buf) = object_buf . as_mut ( ) {
70
+ let tree_entries = objects . find_tree_iter ( & parent_id, buf) ?;
76
71
let mut num_entries = 0 ;
77
72
for entry in tree_entries
78
73
. filter_map ( Result :: ok)
@@ -99,7 +94,8 @@ impl Tree {
99
94
for child in children {
100
95
// This is actually needed here as it's a mut ref, which isn't copy. We do a re-borrow here.
101
96
#[ allow( clippy:: needless_option_as_deref) ]
102
- let actual_num_entries = verify_recursive ( child. id , & child. children , find_buf. as_deref_mut ( ) , find) ?;
97
+ let actual_num_entries =
98
+ verify_recursive ( child. id , & child. children , object_buf. as_deref_mut ( ) , objects) ?;
103
99
if let Some ( ( actual, num_entries) ) = actual_num_entries. zip ( child. num_entries ) {
104
100
if actual > num_entries {
105
101
return Err ( Error :: EntriesCount {
@@ -120,7 +116,7 @@ impl Tree {
120
116
}
121
117
122
118
let mut buf = Vec :: new ( ) ;
123
- let declared_entries = verify_recursive ( self . id , & self . children , use_find . then_some ( & mut buf) , & mut find ) ?;
119
+ let declared_entries = verify_recursive ( self . id , & self . children , use_objects . then_some ( & mut buf) , & objects ) ?;
124
120
if let Some ( ( actual, num_entries) ) = declared_entries. zip ( self . num_entries ) {
125
121
if actual > num_entries {
126
122
return Err ( Error :: EntriesCount {
0 commit comments