@@ -6,17 +6,24 @@ use std::collections::HashMap;
66use std:: process:: Command ;
77use std:: { fs:: write, path:: PathBuf } ;
88
9+ // crate data we stored in the toml, can have multiple versions.
10+ // if so, one TomlKrate maps to several KrateSources
11+ struct TomlKrate {
12+ name : String ,
13+ versions : Vec < String > ,
14+ }
15+
916// represents an archive we download from crates.io
1017#[ derive( Debug , Serialize , Deserialize , Eq , Hash , PartialEq ) ]
1118struct KrateSource {
12- version : String ,
1319 name : String ,
20+ version : String ,
1421}
1522
1623// use this to store the crates when interacting with the crates.toml file
1724#[ derive( Debug , Serialize , Deserialize ) ]
1825struct CrateList {
19- crates : HashMap < String , String > ,
26+ crates : HashMap < String , Vec < String > > ,
2027}
2128
2229// represents the extracted sourcecode of a crate
@@ -145,11 +152,24 @@ fn read_crates() -> Vec<KrateSource> {
145152 let crate_list: CrateList =
146153 toml:: from_str ( & toml_content) . unwrap_or_else ( |e| panic ! ( "Failed to parse {}: \n {}" , toml_path. display( ) , e) ) ;
147154 // parse the hashmap of the toml file into a list of crates
148- crate_list
155+ let tomlkrates : Vec < TomlKrate > = crate_list
149156 . crates
150- . iter ( )
151- . map ( |( name, version) | KrateSource :: new ( & name, & version) )
152- . collect ( )
157+ . into_iter ( )
158+ . map ( |( name, versions) | TomlKrate { name, versions } )
159+ . collect ( ) ;
160+
161+ // flatten TomlKrates into KrateSources (one TomlKrates may represent several versions of a crate =>
162+ // multiple kratesources)
163+ let mut krate_sources = Vec :: new ( ) ;
164+ tomlkrates. into_iter ( ) . for_each ( |tk| {
165+ tk. versions . iter ( ) . for_each ( |ver| {
166+ krate_sources. push ( KrateSource {
167+ name : tk. name . clone ( ) ,
168+ version : ver. to_string ( ) ,
169+ } ) ;
170+ } )
171+ } ) ;
172+ krate_sources
153173}
154174
155175// the main fn
0 commit comments