@@ -11,6 +11,7 @@ use gix_attributes::parse;
11
11
use gix_attributes:: parse:: Kind ;
12
12
13
13
const KEY_TYPE : & str = "type" ;
14
+ const KEY_CRCS : & str = "crcs" ;
14
15
const VAL_EXTERNAL : & str = "external" ;
15
16
16
17
pub struct Parser { }
@@ -24,6 +25,7 @@ pub struct Item {
24
25
#[ derive( Clone , Debug , Eq , PartialEq , Default ) ]
25
26
pub struct Attributes {
26
27
pub items : HashMap < PathBuf , HashMap < String , String > > ,
28
+ pub crcs : HashMap < PathBuf , Vec < u32 > > ,
27
29
}
28
30
29
31
impl Attributes {
@@ -33,6 +35,7 @@ impl Attributes {
33
35
let _items = parse ( & content) ;
34
36
35
37
let mut items = HashMap :: new ( ) ;
38
+ let mut crcs = HashMap :: new ( ) ;
36
39
for _item in _items {
37
40
let _item = _item?;
38
41
if let Kind :: Pattern ( pattern) = _item. 0 {
@@ -43,15 +46,32 @@ impl Attributes {
43
46
let mut current_path = path. clone ( ) ;
44
47
let mut attributes = HashMap :: new ( ) ;
45
48
let mut _type = String :: new ( ) ;
49
+ let mut crc_arr = vec ! [ ] ;
46
50
for line in _item. 1 {
47
51
let line = line?;
48
52
let name = line. name . as_str ( ) ;
49
53
let state = line. state . as_bstr ( ) . unwrap_or_default ( ) ;
50
54
if name == KEY_TYPE {
51
55
_type = state. to_string ( ) ;
52
56
}
57
+ if name == KEY_CRCS {
58
+ crc_arr = state
59
+ . to_string ( )
60
+ . split ( ',' )
61
+ . map ( |s| {
62
+ let trimmed = s. trim ( ) ;
63
+ let hex_str = if trimmed. starts_with ( "0x" ) {
64
+ & trimmed[ 2 ..]
65
+ } else {
66
+ trimmed
67
+ } ;
68
+ u32:: from_str_radix ( hex_str, 16 ) . map_err ( |e| anyhow:: anyhow!( e) )
69
+ } )
70
+ . collect :: < Result < Vec < u32 > , _ > > ( ) ?;
71
+ }
53
72
attributes. insert ( name. to_string ( ) , state. to_string ( ) ) ;
54
73
}
74
+ crcs. insert ( path. clone ( ) , crc_arr) ;
55
75
items. insert ( path, attributes) ;
56
76
57
77
// process parent directory
@@ -69,7 +89,7 @@ impl Attributes {
69
89
}
70
90
}
71
91
72
- Ok ( Attributes { items } )
92
+ Ok ( Attributes { items, crcs } )
73
93
}
74
94
75
95
fn check_external ( & self , attributes : & HashMap < String , String > ) -> bool {
@@ -99,6 +119,10 @@ impl Attributes {
99
119
pub fn get_values < P : AsRef < Path > > ( & self , path : P ) -> Option < & HashMap < String , String > > {
100
120
self . items . get ( path. as_ref ( ) )
101
121
}
122
+
123
+ pub fn get_crcs < P : AsRef < Path > > ( & self , path : P ) -> Option < & Vec < u32 > > {
124
+ self . crcs . get ( path. as_ref ( ) )
125
+ }
102
126
}
103
127
104
128
#[ cfg( test) ]
@@ -113,17 +137,25 @@ mod tests {
113
137
let file = TempFile :: new ( ) . unwrap ( ) ;
114
138
fs:: write (
115
139
file. as_path ( ) ,
116
- "/foo type=external
117
- /bar type=external
140
+ "/foo type=external crcs=0x1234,0x5678
141
+ /bar type=external crcs=0x1234,0x5678
118
142
/models/foo/bar type=external" ,
119
143
)
120
144
. unwrap ( ) ;
121
145
122
146
let attributes = Attributes :: from ( file. as_path ( ) ) . unwrap ( ) ;
123
- let _attributes: HashMap < String , String > = [ ( "type" . to_string ( ) , "external" . to_string ( ) ) ]
124
- . iter ( )
125
- . cloned ( )
126
- . collect ( ) ;
147
+ let _attributes_base: HashMap < String , String > =
148
+ [ ( "type" . to_string ( ) , "external" . to_string ( ) ) ]
149
+ . iter ( )
150
+ . cloned ( )
151
+ . collect ( ) ;
152
+ let _attributes: HashMap < String , String > = [
153
+ ( "type" . to_string ( ) , "external" . to_string ( ) ) ,
154
+ ( "crcs" . to_string ( ) , "0x1234,0x5678" . to_string ( ) ) ,
155
+ ]
156
+ . iter ( )
157
+ . cloned ( )
158
+ . collect ( ) ;
127
159
128
160
let items_map: HashMap < PathBuf , HashMap < String , String > > = vec ! [
129
161
Item {
@@ -136,21 +168,22 @@ mod tests {
136
168
} ,
137
169
Item {
138
170
pattern: PathBuf :: from( "/models" ) ,
139
- attributes: _attributes . clone( ) ,
171
+ attributes: _attributes_base . clone( ) ,
140
172
} ,
141
173
Item {
142
174
pattern: PathBuf :: from( "/models/foo" ) ,
143
- attributes: _attributes . clone( ) ,
175
+ attributes: _attributes_base . clone( ) ,
144
176
} ,
145
177
Item {
146
178
pattern: PathBuf :: from( "/models/foo/bar" ) ,
147
- attributes: _attributes . clone( ) ,
179
+ attributes: _attributes_base . clone( ) ,
148
180
} ,
149
181
]
150
182
. into_iter ( )
151
183
. map ( |item| ( item. pattern , item. attributes ) )
152
184
. collect ( ) ;
153
185
154
- assert_eq ! ( attributes, Attributes { items: items_map } ) ;
186
+ assert_eq ! ( attributes. items, items_map) ;
187
+ assert_eq ! ( attributes. get_crcs( "/foo" ) , Some ( & vec![ 0x1234 , 0x5678 ] ) )
155
188
}
156
189
}
0 commit comments