1- use serde_json:: json;
2- use clap:: ValueEnum ;
3- use serde:: Deserialize ;
41use crate :: logging:: ask_value;
52use crate :: mod_file:: { PlatformName , ToGeodeString } ;
63use crate :: util:: mod_file:: DependencyImportance ;
@@ -14,22 +11,25 @@ use crate::{
1411 } ,
1512} ;
1613use clap:: Subcommand ;
14+ use clap:: ValueEnum ;
1715use edit_distance:: edit_distance;
1816use semver:: Version ;
17+ use serde:: Deserialize ;
18+ use serde_json:: json;
19+ use serde_json:: Value ;
1920use std:: env;
2021use std:: {
2122 collections:: HashMap ,
2223 fs,
2324 path:: { Path , PathBuf } ,
2425} ;
25- use serde_json:: Value ;
2626
2727#[ derive( Debug , Deserialize , Hash , PartialEq , Eq , Clone , Copy , ValueEnum ) ]
2828#[ serde( rename_all = "lowercase" ) ]
2929pub enum ResourceType {
3030 Sprite ,
3131 Font ,
32- File
32+ File ,
3333}
3434
3535#[ derive( Subcommand , Debug ) ]
@@ -69,8 +69,8 @@ pub enum Project {
6969 Add {
7070 /// Type of resource to add
7171 resource : ResourceType ,
72- files : Vec < PathBuf >
73- }
72+ files : Vec < PathBuf > ,
73+ } ,
7474}
7575
7676fn find_build_directory ( root : & Path ) -> Option < PathBuf > {
@@ -534,75 +534,121 @@ pub fn check_dependencies(
534534}
535535
536536fn add_resource ( dir : & Path , resource : ResourceType , files : Vec < PathBuf > ) {
537- let mut mod_json: HashMap < String , Value > = serde_json:: from_reader ( fs:: File :: open ( dir. join ( "mod.json" ) ) . ok ( ) . nice_unwrap ( "Must be inside a project with a mod.json" ) )
538- . nice_unwrap ( "Unable to read mod.json" ) ;
539-
537+ let mut mod_json: HashMap < String , Value > = serde_json:: from_reader (
538+ fs:: File :: open ( dir. join ( "mod.json" ) )
539+ . ok ( )
540+ . nice_unwrap ( "Must be inside a project with a mod.json" ) ,
541+ )
542+ . nice_unwrap ( "Unable to read mod.json" ) ;
540543
541544 let mut do_thing = |name : & str , othername : & str | {
542- let resource = mod_json. get ( "resources" )
545+ let resource = mod_json
546+ . get ( "resources" )
543547 . and_then ( |x| x. get ( name) )
544548 . and_then ( |x| x. as_array ( ) )
545549 . unwrap_or ( & vec ! [ ] )
546550 . clone ( ) ;
547551
548- let mut new_resource: Vec < Value > = resource. into_iter ( ) . chain ( files. clone ( ) . into_iter ( ) . filter_map ( |x| {
549- if !x. exists ( ) {
550- warn ! ( "{} {} does not exist" , othername, x. display( ) ) ;
551- None
552- } else {
553- Some ( Value :: String ( x. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ) )
554- }
555- } ) ) . collect ( ) ;
552+ let mut new_resource: Vec < Value > = resource
553+ . into_iter ( )
554+ . chain ( files. clone ( ) . into_iter ( ) . filter_map ( |x| {
555+ if !x. exists ( ) {
556+ warn ! ( "{} {} does not exist" , othername, x. display( ) ) ;
557+ None
558+ } else {
559+ Some ( Value :: String ( x. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ) )
560+ }
561+ } ) )
562+ . collect ( ) ;
556563
557- let mut duplicates: Vec < _ > = new_resource. iter ( ) . filter ( |x| new_resource. iter ( ) . filter ( |y| y == x) . count ( ) > 1 ) . collect ( ) ;
564+ let mut duplicates: Vec < _ > = new_resource
565+ . iter ( )
566+ . filter ( |x| new_resource. iter ( ) . filter ( |y| y == x) . count ( ) > 1 )
567+ . collect ( ) ;
558568 duplicates. dedup ( ) ;
559- duplicates. into_iter ( ) . for_each ( |x| warn ! ( "Duplicate {}: {}" , othername, x) ) ;
569+ duplicates
570+ . into_iter ( )
571+ . for_each ( |x| warn ! ( "Duplicate {}: {}" , othername, x) ) ;
560572
561573 new_resource. dedup ( ) ;
562574
563- * mod_json. entry ( "resources" . to_string ( ) ) . or_insert ( json ! ( { } ) )
564- . as_object_mut ( ) . nice_unwrap ( "resources is not an object" )
565- . entry ( name. to_string ( ) ) . or_insert ( json ! ( [ ] ) )
566- . as_array_mut ( ) . nice_unwrap ( & format ! ( "{} is not an array" , name) ) = new_resource;
575+ * mod_json
576+ . entry ( "resources" . to_string ( ) )
577+ . or_insert ( json ! ( { } ) )
578+ . as_object_mut ( )
579+ . nice_unwrap ( "resources is not an object" )
580+ . entry ( name. to_string ( ) )
581+ . or_insert ( json ! ( [ ] ) )
582+ . as_array_mut ( )
583+ . nice_unwrap ( & format ! ( "{} is not an array" , name) ) = new_resource;
567584 } ;
568585
569586 match resource {
570587 ResourceType :: Sprite => do_thing ( "sprites" , "Sprite" ) ,
571588 ResourceType :: File => do_thing ( "files" , "File" ) ,
572589
573590 ResourceType :: Font => {
574- let fonts = mod_json. get ( "resources" )
591+ let fonts = mod_json
592+ . get ( "resources" )
575593 . and_then ( |x| x. get ( "fonts" ) )
576594 . and_then ( |x| x. as_array ( ) )
577595 . unwrap_or ( & vec ! [ ] )
578596 . clone ( ) ;
579597
580- let mut new_fonts: Vec < Value > = fonts. into_iter ( ) . chain ( files. into_iter ( ) . filter_map ( |x| {
581- if !x. exists ( ) {
582- warn ! ( "Font {} does not exist" , x. display( ) ) ;
583- None
584- } else {
585- Some ( json ! ( {
586- "path" : x. as_os_str( ) . to_str( ) . unwrap( ) . to_string( ) ,
587- "size" : ask_value( "Font Size" , None , true ) . parse:: <u32 >( ) . ok( ) . nice_unwrap( "Invalid font size!" )
588- } ) )
589- }
590- } ) ) . collect ( ) ;
591-
592- let mut duplicates: Vec < _ > = new_fonts. iter ( ) . filter_map ( |x| x. get ( "path" ) ) . filter ( |x| new_fonts. iter ( ) . filter ( |y| y. get ( "path" ) . map ( |y| y == * x) . unwrap_or ( false ) ) . count ( ) > 1 ) . collect ( ) ;
598+ let mut new_fonts: Vec < Value > = fonts
599+ . into_iter ( )
600+ . chain ( files. into_iter ( ) . filter_map ( |x| {
601+ if !x. exists ( ) {
602+ warn ! ( "Font {} does not exist" , x. display( ) ) ;
603+ None
604+ } else {
605+ let size = ask_value ( "Font Size" , None , true )
606+ . parse :: < u32 > ( )
607+ . ok ( )
608+ . nice_unwrap ( "Invalid font size!" ) ;
609+
610+ Some ( json ! ( {
611+ "path" : x. as_os_str( ) . to_str( ) . unwrap( ) . to_string( ) ,
612+ "size" : size
613+ } ) )
614+ }
615+ } ) )
616+ . collect ( ) ;
617+
618+ let mut duplicates: Vec < _ > = new_fonts
619+ . iter ( )
620+ . filter_map ( |x| x. get ( "path" ) )
621+ . filter ( |x| {
622+ new_fonts
623+ . iter ( )
624+ . filter ( |y| y. get ( "path" ) . map ( |y| y == * x) . unwrap_or ( false ) )
625+ . count ( ) > 1
626+ } )
627+ . collect ( ) ;
593628 duplicates. dedup ( ) ;
594- duplicates. into_iter ( ) . for_each ( |x| warn ! ( "Duplicate Font: {}" , x) ) ;
629+ duplicates
630+ . into_iter ( )
631+ . for_each ( |x| warn ! ( "Duplicate Font: {}" , x) ) ;
595632
596633 new_fonts. dedup ( ) ;
597634
598- * mod_json. entry ( "resources" . to_string ( ) ) . or_insert ( json ! ( { } ) )
599- . as_object_mut ( ) . nice_unwrap ( "resources is not an object" )
600- . entry ( "fonts" . to_string ( ) ) . or_insert ( json ! ( [ ] ) )
601- . as_array_mut ( ) . nice_unwrap ( "fonts is not an array" ) = new_fonts;
635+ * mod_json
636+ . entry ( "resources" . to_string ( ) )
637+ . or_insert ( json ! ( { } ) )
638+ . as_object_mut ( )
639+ . nice_unwrap ( "resources is not an object" )
640+ . entry ( "fonts" . to_string ( ) )
641+ . or_insert ( json ! ( [ ] ) )
642+ . as_array_mut ( )
643+ . nice_unwrap ( "fonts is not an array" ) = new_fonts;
602644 }
603- } ;
645+ } ;
604646
605- fs:: write ( dir. join ( "mod.json" ) , serde_json:: to_string_pretty ( & mod_json) . unwrap ( ) ) . nice_unwrap ( "Failed to save mod.json" ) ;
647+ fs:: write (
648+ dir. join ( "mod.json" ) ,
649+ serde_json:: to_string_pretty ( & mod_json) . unwrap ( ) ,
650+ )
651+ . nice_unwrap ( "Failed to save mod.json" ) ;
606652
607653 done ! ( "Resource added to mod.json" ) ;
608654}
@@ -621,10 +667,8 @@ pub fn subcommand(cmd: Project) {
621667 platform,
622668 externals,
623669 ) ,
624- Project :: Add { resource, files } => add_resource (
625- & std:: env:: current_dir ( ) . unwrap ( ) ,
626- resource,
627- files
628- ) ,
670+ Project :: Add { resource, files } => {
671+ add_resource ( & std:: env:: current_dir ( ) . unwrap ( ) , resource, files)
672+ }
629673 }
630674}
0 commit comments