@@ -69,7 +69,9 @@ use semver::Version;
6969use solc:: SolcSettings ;
7070use std:: {
7171 collections:: { BTreeMap , BTreeSet , HashMap , HashSet } ,
72+ ops:: Range ,
7273 path:: { Path , PathBuf } ,
74+ sync:: Arc ,
7375} ;
7476
7577/// Represents a project workspace and handles `solc` compiling of all contracts in that workspace.
@@ -889,13 +891,28 @@ fn rebase_path(base: &Path, path: &Path) -> PathBuf {
889891 new_path. to_slash_lossy ( ) . into_owned ( ) . into ( )
890892}
891893
894+ /// Utility function to apply a set of updates to provided sources.
895+ fn apply_updates ( sources : & mut Sources , updates : Updates ) {
896+ for ( path, source) in sources {
897+ if let Some ( updates) = updates. get ( path) {
898+ source. content = Arc :: new ( replace_source_content (
899+ source. content . as_str ( ) ,
900+ updates. iter ( ) . map ( |( start, end, update) | ( ( * start..* end) , update. as_str ( ) ) ) ,
901+ ) ) ;
902+ }
903+ }
904+ }
905+
892906/// Utility function to change source content ranges with provided updates.
893- fn replace_source_content ( source : & str , updates : impl Iterator < Item = Update > ) -> String {
907+ fn replace_source_content < ' a > (
908+ source : & str ,
909+ updates : impl IntoIterator < Item = ( Range < usize > , & ' a str ) > ,
910+ ) -> String {
894911 let mut offset = 0 ;
895912 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 ;
913+ for ( range , new_value) in updates {
914+ let start = ( range . start as isize + offset) as usize ;
915+ let end = ( range . end as isize + offset) as usize ;
899916
900917 content. splice ( start..end, new_value. bytes ( ) ) ;
901918 offset += new_value. len ( ) as isize - ( end - start) as isize ;
@@ -1076,15 +1093,14 @@ contract A {
10761093
10771094 let updates = vec ! [
10781095 // Replace function libFn() visibility to external
1079- ( 36 , 44 , "external" . to_string ( ) ) ,
1096+ ( 36 .. 44 , "external" ) ,
10801097 // Replace contract A name to contract B
1081- ( 80 , 90 , "contract B" . to_string ( ) ) ,
1098+ ( 80 .. 90 , "contract B" ) ,
10821099 // Remove function c()
1083- ( 159 , 222 , String :: new ( ) ) ,
1100+ ( 159 .. 222 , "" ) ,
10841101 // Replace function e() logic
1085- ( 276 , 296 , "// no logic" . to_string( ) ) ,
1086- ]
1087- . into_iter ( ) ;
1102+ ( 276 ..296 , "// no logic" ) ,
1103+ ] ;
10881104
10891105 assert_eq ! (
10901106 replace_source_content( original_content, updates) ,
0 commit comments