@@ -11,23 +11,20 @@ custom_error! { pub ModelError
11
11
InvalidGradleSpecifier { specifier: String } = "Invalid Gradle specifier '{specifier}'" ,
12
12
}
13
13
14
+ static META_FORMAT_VERSION : i32 = 1 ;
15
+
14
16
/// A Gradle specifier.
15
- #[ derive( Debug , PartialEq , Eq , Clone , merge :: Merge , Default ) ]
17
+ #[ derive( Debug , PartialEq , Eq , Clone , Default ) ]
16
18
pub struct GradleSpecifier {
17
19
/// Group of the artifact.
18
- #[ merge( strategy = merge:: overwrite) ]
19
20
pub group : String ,
20
21
/// Artifact name.
21
- #[ merge( strategy = merge:: overwrite) ]
22
22
pub artifact : String ,
23
23
/// Version of the artifact.
24
- #[ merge( strategy = merge:: overwrite) ]
25
24
pub version : String ,
26
25
/// File extension of the artifact.
27
- #[ merge( strategy = merge:: overwrite) ]
28
26
pub extension : Option < String > ,
29
27
/// Classifier of the artifact.
30
- #[ merge( strategy = merge:: overwrite) ]
31
28
pub classifier : Option < String > ,
32
29
}
33
30
@@ -195,29 +192,42 @@ impl<'de> Deserialize<'de> for GradleSpecifier {
195
192
#[ derive( Deserialize , Serialize , Debug , Clone , Validate , merge:: Merge ) ]
196
193
#[ serde( rename_all = "camelCase" ) ]
197
194
pub struct MojangArtifactBase {
195
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
198
196
pub sha1 : Option < String > ,
197
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
199
198
pub size : Option < i32 > ,
199
+ #[ merge( strategy = merge:: overwrite) ]
200
200
pub url : String ,
201
201
#[ serde( flatten) ]
202
+ #[ merge( strategy = merge:: hashmap:: overwrite_key) ]
202
203
pub unknown : HashMap < String , serde_json:: Value > ,
203
204
}
204
205
205
206
#[ derive( Deserialize , Serialize , Debug , Clone , Validate , merge:: Merge ) ]
206
207
#[ serde( rename_all = "camelCase" , deny_unknown_fields) ]
207
208
pub struct MojangAssets {
209
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
208
210
pub sha1 : Option < String > ,
211
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
209
212
pub size : Option < i32 > ,
213
+ #[ merge( strategy = merge:: overwrite) ]
210
214
pub url : String ,
215
+ #[ merge( strategy = merge:: overwrite) ]
211
216
pub id : String ,
217
+ #[ merge( strategy = merge:: overwrite) ]
212
218
pub total_size : i32 ,
213
219
}
214
220
215
221
#[ derive( Deserialize , Serialize , Debug , Clone , Validate , merge:: Merge ) ]
216
222
#[ serde( rename_all = "camelCase" , deny_unknown_fields) ]
217
223
pub struct MojangArtifact {
224
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
218
225
pub sha1 : Option < String > ,
226
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
219
227
pub size : Option < i32 > ,
228
+ #[ merge( strategy = merge:: overwrite) ]
220
229
pub url : String ,
230
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
221
231
pub path : Option < String > ,
222
232
}
223
233
@@ -260,7 +270,7 @@ pub struct OSRule {
260
270
pub version : Option < String > ,
261
271
}
262
272
263
- fn os_rule_name_must_be_os ( name : String ) -> Result < ( ) , serde_valid:: validation:: Error > {
273
+ fn os_rule_name_must_be_os ( name : & String ) -> Result < ( ) , serde_valid:: validation:: Error > {
264
274
let valid_os_names = vec ! [
265
275
"osx" ,
266
276
"linux" ,
@@ -270,8 +280,11 @@ fn os_rule_name_must_be_os(name: String) -> Result<(), serde_valid::validation::
270
280
"linux-arm64" ,
271
281
"linux-arm32" ,
272
282
] ;
273
- if !valid_os_names. contains ( & name) {
274
- Err ( format ! ( "`{}` not a valid os name" , & name) )
283
+ if !valid_os_names. contains ( & name. as_str ( ) ) {
284
+ Err ( serde_valid:: validation:: Error :: Custom ( format ! (
285
+ "`{}` not a valid os name" ,
286
+ & name
287
+ ) ) )
275
288
} else {
276
289
Ok ( ( ) )
277
290
}
@@ -288,13 +301,13 @@ pub struct MojangRule {
288
301
}
289
302
290
303
fn mojang_rule_action_must_be_allow_disallow (
291
- action : String ,
304
+ action : & String ,
292
305
) -> Result < ( ) , serde_valid:: validation:: Error > {
293
- if !vec ! [ "allow" , "disallow" ] . contains ( & action) {
294
- Err ( format ! (
306
+ if !vec ! [ "allow" , "disallow" ] . contains ( & action. as_str ( ) ) {
307
+ Err ( serde_valid :: validation :: Error :: Custom ( format ! (
295
308
"`{}` not a valid action, must be `allow` or `disallow`" ,
296
309
& action
297
- ) )
310
+ ) ) )
298
311
} else {
299
312
Ok ( ( ) )
300
313
}
@@ -320,11 +333,11 @@ impl Deref for MojangRules {
320
333
pub struct MojangLibrary {
321
334
#[ merge( strategy = merge:: option:: recurse) ]
322
335
pub extract : Option < MojangLibraryExtractRules > ,
323
- #[ merge( strategy = merge:: option:: recurse ) ]
336
+ #[ merge( strategy = merge:: option:: overwrite_some ) ]
324
337
pub name : Option < GradleSpecifier > ,
325
338
#[ merge( strategy = merge:: option:: recurse) ]
326
339
pub downloads : Option < MojangLibraryDownloads > ,
327
- #[ merge( strategy = merge:: option :: recurse ) ]
340
+ #[ merge( strategy = merge:: option_hashmap :: overwrite_key_some ) ]
328
341
pub natives : Option < HashMap < String , String > > ,
329
342
#[ merge( strategy = merge:: option:: recurse) ]
330
343
pub rules : Option < MojangRules > ,
@@ -335,19 +348,71 @@ pub struct MojangLibrary {
335
348
pub struct Library {
336
349
#[ merge( strategy = merge:: option:: recurse) ]
337
350
pub extract : Option < MojangLibraryExtractRules > ,
338
- #[ merge( strategy = merge:: option:: recurse ) ]
351
+ #[ merge( strategy = merge:: option:: overwrite_some ) ]
339
352
pub name : Option < GradleSpecifier > ,
340
353
#[ merge( strategy = merge:: option:: recurse) ]
341
354
pub downloads : Option < MojangLibraryDownloads > ,
342
- #[ merge( strategy = merge:: option :: recurse ) ]
355
+ #[ merge( strategy = merge:: option_hashmap :: overwrite_key_some ) ]
343
356
pub natives : Option < HashMap < String , String > > ,
344
357
#[ merge( strategy = merge:: option:: recurse) ]
345
358
pub rules : Option < MojangRules > ,
346
359
#[ merge( strategy = merge:: option:: overwrite_some) ]
347
360
url : Option < String > ,
348
361
#[ serde( rename = "MMC-hint" ) ]
349
362
#[ merge( strategy = merge:: option:: overwrite_some) ]
350
- mmcHint : Option < String > ,
363
+ mmc_hint : Option < String > ,
364
+ }
365
+
366
+ impl From < MojangLibrary > for Library {
367
+ fn from ( item : MojangLibrary ) -> Self {
368
+ Self {
369
+ extract : item. extract ,
370
+ name : item. name ,
371
+ downloads : item. downloads ,
372
+ natives : item. natives ,
373
+ rules : item. rules ,
374
+ url : None ,
375
+ mmc_hint : None ,
376
+ }
377
+ }
378
+ }
379
+
380
+ impl From < Library > for MojangLibrary {
381
+ fn from ( item : Library ) -> Self {
382
+ Self {
383
+ extract : item. extract ,
384
+ name : item. name ,
385
+ downloads : item. downloads ,
386
+ natives : item. natives ,
387
+ rules : item. rules ,
388
+ }
389
+ }
390
+ }
391
+
392
+ impl From < & MojangLibrary > for Library {
393
+ fn from ( item : & MojangLibrary ) -> Self {
394
+ Self {
395
+ extract : item. extract . clone ( ) ,
396
+ name : item. name . clone ( ) ,
397
+ downloads : item. downloads . clone ( ) ,
398
+ natives : item. natives . clone ( ) ,
399
+ rules : item. rules . clone ( ) ,
400
+ url : None ,
401
+ mmc_hint : None ,
402
+ }
403
+ }
404
+ }
405
+
406
+ impl From < & Library > for MojangLibrary {
407
+ fn from ( item : & Library ) -> Self {
408
+ Self {
409
+ extract : item. extract . clone ( ) ,
410
+ name : item. name . clone ( ) ,
411
+ downloads : item. downloads . clone ( ) ,
412
+ natives : item. natives . clone ( ) ,
413
+ rules : item. rules . clone ( ) ,
414
+ }
415
+ }
351
416
}
352
417
353
418
#[ derive( Deserialize , Serialize , Debug , Clone , Validate , merge:: Merge , Default ) ]
@@ -364,30 +429,52 @@ pub struct Dependency {
364
429
#[ derive( Deserialize , Serialize , Debug , Clone , Validate , merge:: Merge , Default ) ]
365
430
#[ serde( rename_all = "camelCase" , deny_unknown_fields) ]
366
431
pub struct MetaVersion {
432
+ #[ merge( strategy = merge:: overwrite) ]
367
433
pub format_version : i32 ,
434
+ #[ merge( strategy = merge:: overwrite) ]
368
435
pub name : String ,
436
+ #[ merge( strategy = merge:: overwrite) ]
369
437
pub version : String ,
438
+ #[ merge( strategy = merge:: overwrite) ]
370
439
pub uid : String ,
371
440
#[ serde( rename = "type" ) ]
441
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
372
442
pub version_type : Option < String > ,
443
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
373
444
pub order : Option < i32 > ,
445
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
374
446
pub volatile : Option < bool > ,
447
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
375
448
pub requires : Option < Vec < Dependency > > ,
449
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
376
450
pub conflicts : Option < Vec < Dependency > > ,
451
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
377
452
pub libraries : Option < Vec < Library > > ,
453
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
378
454
pub asset_index : Option < MojangAssets > ,
455
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
379
456
pub maven_files : Option < Vec < Library > > ,
457
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
380
458
pub main_jar : Option < Library > ,
459
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
381
460
pub jar_mods : Option < Vec < Library > > ,
461
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
382
462
pub main_class : Option < String > ,
463
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
383
464
pub applet_class : Option < String > ,
465
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
384
466
pub minecraft_arguments : Option < String > ,
467
+ #[ merge( strategy = merge:: option:: overwrite_some) ]
385
468
pub release_time : Option < String > ,
469
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
386
470
pub compatible_java_majors : Option < Vec < i32 > > ,
471
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
387
472
pub additional_traits : Option < Vec < String > > ,
388
473
#[ serde( rename = "+tweakers" ) ]
474
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
389
475
pub additional_tweakers : Option < Vec < String > > ,
390
476
#[ serde( rename = "+jvmArgs" ) ]
477
+ #[ merge( strategy = merge:: option_vec:: append_some) ]
391
478
pub additional_jvm_args : Option < Vec < String > > ,
392
479
}
393
480
@@ -460,6 +547,14 @@ pub mod merge {
460
547
}
461
548
}
462
549
}
550
+
551
+ pub fn overwrite_key < K : Eq + Hash , V > ( left : & mut HashMap < K , V > , right : HashMap < K , V > ) {
552
+ use std:: collections:: hash_map:: Entry ;
553
+
554
+ for ( k, v) in right {
555
+ left. insert ( k, v) ;
556
+ }
557
+ }
463
558
}
464
559
465
560
/// Merge strategies for `Option<HashMap>`
@@ -480,6 +575,20 @@ pub mod merge {
480
575
}
481
576
}
482
577
}
578
+
579
+ pub fn overwrite_key_some < K : Eq + Hash , V > (
580
+ left : & mut Option < HashMap < K , V > > ,
581
+ right : Option < HashMap < K , V > > ,
582
+ ) {
583
+ use std:: collections:: hash_map:: Entry ;
584
+ if let Some ( new) = right {
585
+ if let Some ( original) = left {
586
+ hashmap:: overwrite_key ( original, new) ;
587
+ } else {
588
+ * left = Some ( new) ;
589
+ }
590
+ }
591
+ }
483
592
}
484
593
485
594
/// Merge strategies for `Option<Vec>`
0 commit comments