@@ -42,8 +42,7 @@ pub mod report;
4242
4343/// Updates to be applied to the sources.
4444/// source_path -> (start, end, new_value)
45- pub type Update = ( usize , usize , String ) ;
46- pub type Updates = HashMap < PathBuf , BTreeSet < Update > > ;
45+ pub type Updates = HashMap < PathBuf , BTreeSet < ( usize , usize , String ) > > ;
4746
4847/// Utilities for creating, mocking and testing of (temporary) projects
4948#[ cfg( feature = "project-util" ) ]
@@ -69,7 +68,9 @@ use semver::Version;
6968use solc:: SolcSettings ;
7069use std:: {
7170 collections:: { BTreeMap , BTreeSet , HashMap , HashSet } ,
71+ ops:: Range ,
7272 path:: { Path , PathBuf } ,
73+ sync:: Arc ,
7374} ;
7475
7576/// Represents a project workspace and handles `solc` compiling of all contracts in that workspace.
@@ -889,13 +890,28 @@ fn rebase_path(base: &Path, path: &Path) -> PathBuf {
889890 new_path. to_slash_lossy ( ) . into_owned ( ) . into ( )
890891}
891892
893+ /// Utility function to apply a set of updates to provided sources.
894+ fn apply_updates ( sources : & mut Sources , updates : Updates ) {
895+ for ( path, source) in sources {
896+ if let Some ( updates) = updates. get ( path) {
897+ source. content = Arc :: new ( replace_source_content (
898+ source. content . as_str ( ) ,
899+ updates. iter ( ) . map ( |( start, end, update) | ( ( * start..* end) , update. as_str ( ) ) ) ,
900+ ) ) ;
901+ }
902+ }
903+ }
904+
892905/// Utility function to change source content ranges with provided updates.
893- fn replace_source_content ( source : & str , updates : impl Iterator < Item = Update > ) -> String {
906+ fn replace_source_content < ' a > (
907+ source : & str ,
908+ updates : impl IntoIterator < Item = ( Range < usize > , & ' a str ) > ,
909+ ) -> String {
894910 let mut offset = 0 ;
895911 let mut content = source. as_bytes ( ) . to_vec ( ) ;
896- for ( start , end , new_value) in updates {
897- let start = ( start as isize + offset) as usize ;
898- let end = ( end as isize + offset) as usize ;
912+ for ( range , new_value) in updates {
913+ let start = ( range . start as isize + offset) as usize ;
914+ let end = ( range . end as isize + offset) as usize ;
899915
900916 content. splice ( start..end, new_value. bytes ( ) ) ;
901917 offset += new_value. len ( ) as isize - ( end - start) as isize ;
@@ -1076,15 +1092,14 @@ contract A {
10761092
10771093 let updates = vec ! [
10781094 // Replace function libFn() visibility to external
1079- ( 36 , 44 , "external" . to_string ( ) ) ,
1095+ ( 36 .. 44 , "external" ) ,
10801096 // Replace contract A name to contract B
1081- ( 80 , 90 , "contract B" . to_string ( ) ) ,
1097+ ( 80 .. 90 , "contract B" ) ,
10821098 // Remove function c()
1083- ( 159 , 222 , String :: new ( ) ) ,
1099+ ( 159 .. 222 , "" ) ,
10841100 // Replace function e() logic
1085- ( 276 , 296 , "// no logic" . to_string( ) ) ,
1086- ]
1087- . into_iter ( ) ;
1101+ ( 276 ..296 , "// no logic" ) ,
1102+ ] ;
10881103
10891104 assert_eq ! (
10901105 replace_source_content( original_content, updates) ,
0 commit comments