22
33use foundry_compilers_artifacts:: Remapping ;
44use foundry_compilers_core:: error:: { Result , SolcError } ;
5- use rand:: {
6- distributions:: { Distribution , Uniform } ,
7- seq:: SliceRandom ,
8- Rng ,
9- } ;
5+ use rand:: { seq:: SliceRandom , Rng } ;
106use serde:: { Deserialize , Serialize } ;
117use std:: {
128 collections:: { BTreeSet , HashMap , HashSet , VecDeque } ,
@@ -70,11 +66,11 @@ impl MockProjectGenerator {
7066 }
7167
7268 let graph = Graph :: < MultiCompilerParsedSource > :: resolve ( paths) ?;
73- let mut gen = Self :: default ( ) ;
69+ let mut generated = Self :: default ( ) ;
7470 let ( _, edges) = graph. into_sources ( ) ;
7571
7672 // add all files as source files
77- gen . add_sources ( edges. files ( ) . count ( ) ) ;
73+ generated . add_sources ( edges. files ( ) . count ( ) ) ;
7874
7975 // stores libs and their files
8076 let libs = get_libs (
@@ -85,25 +81,25 @@ impl MockProjectGenerator {
8581
8682 // mark all files as libs
8783 for ( lib_id, lib_files) in libs. into_values ( ) . enumerate ( ) {
88- let lib_name = gen . name_strategy . new_lib_name ( lib_id) ;
89- let offset = gen . inner . files . len ( ) ;
84+ let lib_name = generated . name_strategy . new_lib_name ( lib_id) ;
85+ let offset = generated . inner . files . len ( ) ;
9086 let lib = MockLib { name : lib_name, id : lib_id, num_files : lib_files. len ( ) , offset } ;
9187 for lib_file in lib_files {
92- let file = & mut gen . inner . files [ lib_file] ;
88+ let file = & mut generated . inner . files [ lib_file] ;
9389 file. lib_id = Some ( lib_id) ;
94- file. name = gen . name_strategy . new_lib_name ( file. id ) ;
90+ file. name = generated . name_strategy . new_lib_name ( file. id ) ;
9591 }
96- gen . inner . libraries . push ( lib) ;
92+ generated . inner . libraries . push ( lib) ;
9793 }
9894
9995 for id in edges. files ( ) {
10096 for import in edges. imported_nodes ( id) . iter ( ) . copied ( ) {
101- let import = gen . get_import ( import) ;
102- gen . inner . files [ id] . imports . insert ( import) ;
97+ let import = generated . get_import ( import) ;
98+ generated . inner . files [ id] . imports . insert ( import) ;
10399 }
104100 }
105101
106- Ok ( gen )
102+ Ok ( generated )
107103 }
108104
109105 /// Consumes the type and returns the underlying skeleton
@@ -243,34 +239,36 @@ impl MockProjectGenerator {
243239 self
244240 }
245241
246- /// randomly assign empty file status so that mocked files don't emit artifacts
242+ /// Randomly assign empty file status so that mocked files don't emit artifacts.
247243 pub fn assign_empty_files ( & mut self ) -> & mut Self {
248- let mut rng = rand:: thread_rng ( ) ;
249- let die = Uniform :: from ( 0 ..self . inner . files . len ( ) ) ;
244+ let mut rng = rand:: rng ( ) ;
245+ let n = self . inner . files . len ( ) ;
246+
250247 for file in self . inner . files . iter_mut ( ) {
251- let throw = die . sample ( & mut rng ) ;
248+ let throw = rng . random_range ( 0 ..n ) ;
252249 if throw == 0 {
253- // give it a 1 in num(files) chance that the file will be empty
250+ // 1 in n chance that the file will be empty
254251 file. emit_artifacts = false ;
255252 }
256253 }
254+
257255 self
258256 }
259257
260258 /// Populates the imports of the project
261259 pub fn populate_imports ( & mut self , settings : & MockProjectSettings ) -> & mut Self {
262- let mut rng = rand:: thread_rng ( ) ;
260+ let mut rng = rand:: rng ( ) ;
263261
264262 // populate imports
265263 for id in 0 ..self . inner . files . len ( ) {
266264 let imports = if let Some ( lib) = self . inner . files [ id] . lib_id {
267265 let num_imports = rng
268- . gen_range ( settings. min_imports ..=settings. max_imports )
266+ . random_range ( settings. min_imports ..=settings. max_imports )
269267 . min ( self . inner . libraries [ lib] . num_files . saturating_sub ( 1 ) ) ;
270268 self . unique_imports_for_lib ( & mut rng, lib, id, num_imports)
271269 } else {
272270 let num_imports = rng
273- . gen_range ( settings. min_imports ..=settings. max_imports )
271+ . random_range ( settings. min_imports ..=settings. max_imports )
274272 . min ( self . inner . files . len ( ) . saturating_sub ( 1 ) ) ;
275273 self . unique_imports_for_source ( & mut rng, id, num_imports)
276274 } ;
@@ -437,11 +435,16 @@ impl MockFile {
437435
438436 pub fn target_path < L : Language > (
439437 & self ,
440- gen : & MockProjectGenerator ,
438+ generated : & MockProjectGenerator ,
441439 paths : & ProjectPathsConfig < L > ,
442440 ) -> PathBuf {
443441 let mut target = if let Some ( lib) = self . lib_id {
444- paths. root . join ( "lib" ) . join ( & gen. inner . libraries [ lib] . name ) . join ( "src" ) . join ( & self . name )
442+ paths
443+ . root
444+ . join ( "lib" )
445+ . join ( & generated. inner . libraries [ lib] . name )
446+ . join ( "src" )
447+ . join ( & self . name )
445448 } else {
446449 paths. sources . join ( & self . name )
447450 } ;
@@ -552,14 +555,14 @@ pub struct MockProjectSettings {
552555impl MockProjectSettings {
553556 /// Generates a new instance with random settings within an arbitrary range
554557 pub fn random ( ) -> Self {
555- let mut rng = rand:: thread_rng ( ) ;
558+ let mut rng = rand:: rng ( ) ;
556559 // arbitrary thresholds
557560 Self {
558- num_sources : rng. gen_range ( 2 ..25 ) ,
559- num_libs : rng. gen_range ( 0 ..5 ) ,
560- num_lib_files : rng. gen_range ( 1 ..10 ) ,
561- min_imports : rng. gen_range ( 0 ..3 ) ,
562- max_imports : rng. gen_range ( 4 ..10 ) ,
561+ num_sources : rng. random_range ( 2 ..25 ) ,
562+ num_libs : rng. random_range ( 0 ..5 ) ,
563+ num_lib_files : rng. random_range ( 1 ..10 ) ,
564+ min_imports : rng. random_range ( 0 ..3 ) ,
565+ max_imports : rng. random_range ( 4 ..10 ) ,
563566 allow_no_artifacts_files : true ,
564567 }
565568 }
0 commit comments