@@ -2,8 +2,11 @@ use std::collections::BTreeMap;
2
2
use std:: collections:: BTreeSet ;
3
3
use std:: fmt:: Write as _;
4
4
use std:: fs;
5
+ use std:: fs:: File ;
5
6
use std:: path:: Path ;
6
7
8
+ use serde_json;
9
+
7
10
pub type ArtifactId = String ;
8
11
pub type FileContent = String ;
9
12
@@ -29,11 +32,6 @@ pub struct FakeAggregatorData {
29
32
30
33
impl FakeAggregatorData {
31
34
pub fn load_from_folder ( folder : & Path ) -> Self {
32
- fn extract_artifact_id ( filename : & str , prefix : & str ) -> String {
33
- let id_with_extension = filename. strip_prefix ( prefix) . unwrap ( ) ;
34
- id_with_extension. strip_suffix ( ".json" ) . unwrap ( ) . to_string ( )
35
- }
36
-
37
35
let mut data = FakeAggregatorData :: default ( ) ;
38
36
39
37
for entry in list_json_files_in_folder ( folder) {
@@ -49,41 +47,32 @@ impl FakeAggregatorData {
49
47
"epoch-settings.json" => {
50
48
data. epoch_settings = file_content;
51
49
}
52
- "mithril-stake-distributions.json" => {
50
+ "mithril-stake-distributions-list .json" => {
53
51
data. msds_list = file_content;
54
52
}
55
- "snapshots.json" => {
53
+ "snapshots-list .json" => {
56
54
data. snapshots_list = file_content;
57
55
}
58
- "certificates.json" => {
56
+ "certificates-list .json" => {
59
57
data. certificates_list = file_content;
60
58
}
61
- "ctx-snapshots.json" => {
59
+ "ctx-snapshots-list .json" => {
62
60
data. ctx_snapshots_list = file_content;
63
61
}
64
- _ if filename. starts_with ( "mithril-stake-distribution-" ) => {
65
- data. individual_msds . insert (
66
- extract_artifact_id ( & filename, "mithril-stake-distribution-" ) ,
67
- file_content,
68
- ) ;
62
+ "mithril-stake-distributions.json" => {
63
+ data. individual_msds = Self :: read_artifacts_json_file ( & entry. path ( ) ) ;
69
64
}
70
- _ if filename. starts_with ( "snapshot-" ) => {
71
- data. individual_snapshots
72
- . insert ( extract_artifact_id ( & filename, "snapshot-" ) , file_content) ;
65
+ "snapshots.json" => {
66
+ data. individual_snapshots = Self :: read_artifacts_json_file ( & entry. path ( ) ) ;
73
67
}
74
- _ if filename. starts_with ( "certificate-" ) => {
75
- data. individual_certificates
76
- . insert ( extract_artifact_id ( & filename, "certificate-" ) , file_content) ;
68
+ "certificates.json" => {
69
+ data. individual_certificates = Self :: read_artifacts_json_file ( & entry. path ( ) ) ;
77
70
}
78
- _ if filename. starts_with ( "ctx-snapshot-" ) => {
79
- data. individual_ctx_snapshots . insert (
80
- extract_artifact_id ( & filename, "ctx-snapshot-" ) ,
81
- file_content,
82
- ) ;
71
+ "ctx-snapshots.json" => {
72
+ data. individual_ctx_snapshots = Self :: read_artifacts_json_file ( & entry. path ( ) ) ;
83
73
}
84
- _ if filename. starts_with ( "ctx-proof-" ) => {
85
- data. ctx_proofs
86
- . insert ( extract_artifact_id ( & filename, "ctx-proof-" ) , file_content) ;
74
+ "ctx-proofs.json" => {
75
+ data. ctx_proofs = Self :: read_artifacts_json_file ( & entry. path ( ) ) ;
87
76
}
88
77
// unknown file
89
78
_ => { }
@@ -177,9 +166,31 @@ impl FakeAggregatorData {
177
166
)
178
167
)
179
168
}
169
+
170
+ fn read_artifacts_json_file ( json_file : & Path ) -> BTreeMap < ArtifactId , FileContent > {
171
+ let file = File :: open ( json_file) . unwrap ( ) ;
172
+ let parsed_json: serde_json:: Value = serde_json:: from_reader ( & file) . unwrap ( ) ;
173
+
174
+ let json_object = parsed_json. as_object ( ) . unwrap ( ) ;
175
+ let res: Result < Vec < _ > , _ > = json_object
176
+ . iter ( )
177
+ . map ( |( key, value) | extract_artifact_id_and_content ( key, value) )
178
+ . collect ( ) ;
179
+
180
+ let tree = BTreeMap :: from_iter ( res. unwrap ( ) ) ;
181
+ tree
182
+ }
183
+ }
184
+
185
+ fn extract_artifact_id_and_content (
186
+ key : & String ,
187
+ value : & serde_json:: Value ,
188
+ ) -> Result < ( ArtifactId , FileContent ) , String > {
189
+ let json_content = serde_json:: to_string ( value) . map_err ( |e| e. to_string ( ) ) ?;
190
+ Ok ( ( key. to_owned ( ) , json_content) )
180
191
}
181
192
182
- pub fn list_json_files_in_folder ( folder : & Path ) -> impl Iterator < Item = std :: fs:: DirEntry > + ' _ {
193
+ pub fn list_json_files_in_folder ( folder : & Path ) -> impl Iterator < Item = fs:: DirEntry > + ' _ {
183
194
crate :: list_files_in_folder ( folder)
184
195
. filter ( |e| e. file_name ( ) . to_string_lossy ( ) . ends_with ( ".json" ) )
185
196
}
@@ -253,6 +264,8 @@ pub fn generate_ids_array(array_name: &str, ids: BTreeSet<ArtifactId>) -> String
253
264
254
265
#[ cfg( test) ]
255
266
mod tests {
267
+ use crate :: get_temp_dir;
268
+
256
269
use super :: * ;
257
270
258
271
#[ test]
@@ -312,4 +325,24 @@ fn b() {}
312
325
)
313
326
)
314
327
}
328
+
329
+ #[ test]
330
+ fn read_artifacts_json_file ( ) {
331
+ let dir = get_temp_dir ( "read_artifacts_json_file" ) ;
332
+ let file = dir. join ( "test.json" ) ;
333
+ let json_content = r#"{
334
+ "hash1": { "name": "artifact1" },
335
+ "hash2": { "name": "artifact2" }
336
+ }"# ;
337
+ let expected = BTreeMap :: from ( [
338
+ ( "hash1" . to_string ( ) , r#"{"name":"artifact1"}"# . to_string ( ) ) ,
339
+ ( "hash2" . to_string ( ) , r#"{"name":"artifact2"}"# . to_string ( ) ) ,
340
+ ] ) ;
341
+
342
+ fs:: write ( & file, json_content) . unwrap ( ) ;
343
+
344
+ let id_per_json = FakeAggregatorData :: read_artifacts_json_file ( & file) ;
345
+
346
+ assert_eq ! ( expected, id_per_json) ;
347
+ }
315
348
}
0 commit comments