1- use std:: { collections :: HashMap , fs, path:: PathBuf } ;
2- use yaml_rust:: YamlLoader ;
1+ use std:: { fs, path:: PathBuf } ;
2+ use yaml_rust:: { Yaml , YamlLoader } ;
33
4- pub ( crate ) type ImageSetName = String ;
5- pub ( crate ) type BinaryList = Vec < String > ;
6-
7- #[ derive( Debug ) ]
8- pub ( crate ) struct ImageInfo {
9- pub ( crate ) name : String ,
4+ pub ( crate ) struct ImageConfig {
5+ pub ( crate ) img_files : String ,
6+ pub ( crate ) binaries : Vec < String > ,
7+ pub ( crate ) fs_type : String ,
108 pub ( crate ) fs_partition_name : String ,
11- pub ( crate ) part3_fs : String ,
12- }
13-
14- #[ derive( Debug ) ]
15- pub ( crate ) struct ImageSetConfig {
16- pub ( crate ) binaries : BinaryList ,
17- pub ( crate ) images : Vec < ImageInfo > ,
9+ pub ( crate ) filename : String ,
1810}
1911
2012pub ( crate ) struct ImagerConfig {
2113 img_out_dir : PathBuf ,
2214 static_files_dir : PathBuf ,
23- image_sets : HashMap < ImageSetName , ImageSetConfig > ,
15+ image : ImageConfig ,
2416}
2517
2618impl ImagerConfig {
2719 pub ( crate ) fn load ( path : & PathBuf ) -> Option < Self > {
20+ if !path. exists ( ) {
21+ // check for it on the imager folder
22+ let mut alt_path = get_crate_root ( ) ;
23+ alt_path. push ( path) ;
24+ if !alt_path. exists ( ) {
25+ return None ;
26+ }
27+ return Self :: load ( & alt_path) ;
28+ }
2829 let contents = fs:: read_to_string ( path) . ok ( ) ?;
2930 Some ( contents. into ( ) )
3031 }
@@ -37,20 +38,8 @@ impl ImagerConfig {
3738 & self . static_files_dir
3839 }
3940
40- pub ( crate ) fn image_sets ( & self ) -> & HashMap < ImageSetName , ImageSetConfig > {
41- & self . image_sets
42- }
43-
44- pub ( crate ) fn get_image_set ( & self , name : & str ) -> Option < & ImageSetConfig > {
45- self . image_sets . get ( name)
46- }
47-
48- pub ( crate ) fn get_binaries ( & self , name : & str ) -> Option < & BinaryList > {
49- self . image_sets . get ( name) . map ( |s| & s. binaries )
50- }
51-
52- pub ( crate ) fn get_images ( & self , name : & str ) -> Option < & Vec < ImageInfo > > {
53- self . image_sets . get ( name) . map ( |s| & s. images )
41+ pub ( crate ) fn image ( & self ) -> & ImageConfig {
42+ & self . image
5443 }
5544}
5645
@@ -59,80 +48,55 @@ impl From<String> for ImagerConfig {
5948 let docs = YamlLoader :: load_from_str ( & src) . unwrap ( ) ;
6049 let doc = & docs[ 0 ] ;
6150
62- // Parse paths
63- let paths = doc[ "paths" ] . as_hash ( ) . unwrap ( ) ;
64- let img_out_dir = paths[ & yaml_rust:: Yaml :: from_str ( "img_out_dir" ) ]
65- . as_str ( )
51+ // constants
52+ let constants = doc[ "constants" ] . as_hash ( ) . unwrap ( ) ;
53+ let img_out_dir = constants[ & Yaml :: String ( "img_out_dir" . into ( ) ) ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
54+ let static_files_dir = constants[ & Yaml :: String ( "static_files_dir" . into ( ) ) ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
55+
56+ // top-level name
57+ let img_files = doc[ "img_files" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
58+
59+ // binaries
60+ let binaries = doc[ "binaries" ]
61+ . as_vec ( )
6662 . unwrap ( )
67- . to_string ( ) ;
68- let static_files_dir = paths[ & yaml_rust:: Yaml :: from_str ( "static_files_dir" ) ]
69- . as_str ( )
70- . unwrap ( )
71- . to_string ( ) ;
72-
73- // Parse image_sets section
74- let image_sets_yaml = doc[ "image_sets" ] . as_hash ( ) . unwrap ( ) ;
75- let mut image_sets: HashMap < ImageSetName , ImageSetConfig > = HashMap :: new ( ) ;
76-
77- for ( set_name_yaml, set_value_yaml) in image_sets_yaml {
78- let set_name = set_name_yaml. as_str ( ) . unwrap ( ) . to_string ( ) ;
79- let inner = set_value_yaml. as_hash ( ) . unwrap ( ) ;
80-
81- // binaries
82- let binaries = inner[ & yaml_rust:: Yaml :: from_str ( "binaries" ) ]
83- . as_vec ( )
84- . unwrap ( )
85- . iter ( )
86- . map ( |v| v. as_str ( ) . unwrap ( ) . to_string ( ) )
87- . collect :: < BinaryList > ( ) ;
88-
89- let images = inner
90- . get ( & yaml_rust:: Yaml :: from_str ( "images" ) )
91- . and_then ( |i| i. as_vec ( ) )
92- . map ( |v| {
93- v. iter ( )
94- . map ( |entry| {
95- let e = entry. as_hash ( ) . unwrap ( ) ;
96- ImageInfo {
97- name : e[ & yaml_rust:: Yaml :: from_str ( "name" ) ]
98- . as_str ( )
99- . unwrap ( )
100- . to_string ( ) ,
101- fs_partition_name : e[ & yaml_rust:: Yaml :: from_str ( "fs_partition_name" ) ]
102- . as_str ( )
103- . unwrap ( )
104- . to_string ( ) ,
105- part3_fs : e[ & yaml_rust:: Yaml :: from_str ( "part3_fs" ) ]
106- . as_str ( )
107- . unwrap ( )
108- . to_string ( ) ,
109- }
110- } )
111- . collect :: < Vec < ImageInfo > > ( )
112- } )
113- . unwrap_or_default ( ) ;
114-
115- image_sets. insert ( set_name, ImageSetConfig { binaries, images } ) ;
116- }
63+ . iter ( )
64+ . map ( |v| v. as_str ( ) . unwrap ( ) . to_string ( ) )
65+ . collect :: < Vec < String > > ( ) ;
66+
67+ // filesystem block
68+ let fs = doc[ "filesystem" ] . as_hash ( ) . unwrap ( ) ;
69+
70+ let fs_type = fs[ & Yaml :: String ( "type" . into ( ) ) ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
71+ let filename = fs[ & Yaml :: String ( "filename" . into ( ) ) ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
72+ let fs_partition_name = fs[ & Yaml :: String ( "partition_name" . into ( ) ) ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
73+
74+ let image = ImageConfig {
75+ img_files,
76+ binaries,
77+ fs_type,
78+ fs_partition_name,
79+ filename,
80+ } ;
11781
11882 Self {
11983 img_out_dir : PathBuf :: from ( img_out_dir) ,
12084 static_files_dir : PathBuf :: from ( static_files_dir) ,
121- image_sets ,
85+ image ,
12286 }
12387 }
12488}
12589
90+ fn get_crate_root ( ) -> PathBuf {
91+ let file_path = PathBuf :: from ( file ! ( ) ) ;
92+ file_path. parent ( ) . unwrap ( ) . parent ( ) . unwrap ( ) . to_path_buf ( )
93+ }
94+
12695impl Default for ImagerConfig {
12796 fn default ( ) -> Self {
128- let file_path = PathBuf :: from ( file ! ( ) ) ;
129- let config_path = file_path
130- . parent ( )
131- . unwrap ( )
132- . parent ( )
133- . unwrap ( )
134- . join ( "config.yaml" ) ;
97+ let config_path = get_crate_root ( )
98+ . join ( "full.config.yaml" ) ;
13599
136- Self :: load ( & config_path) . expect ( "Expected config.yaml at the root of the imager crate" )
100+ Self :: load ( & config_path) . expect ( "Expected full. config.yaml at the root of the imager crate" )
137101 }
138102}
0 commit comments