@@ -42,8 +42,7 @@ pub mod report;
42
42
43
43
/// Updates to be applied to the sources.
44
44
/// 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 ) > > ;
47
46
48
47
/// Utilities for creating, mocking and testing of (temporary) projects
49
48
#[ cfg( feature = "project-util" ) ]
@@ -69,7 +68,9 @@ use semver::Version;
69
68
use solc:: SolcSettings ;
70
69
use std:: {
71
70
collections:: { BTreeMap , BTreeSet , HashMap , HashSet } ,
71
+ ops:: Range ,
72
72
path:: { Path , PathBuf } ,
73
+ sync:: Arc ,
73
74
} ;
74
75
75
76
/// 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 {
889
890
new_path. to_slash_lossy ( ) . into_owned ( ) . into ( )
890
891
}
891
892
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
+
892
905
/// 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 {
894
910
let mut offset = 0 ;
895
911
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 ;
899
915
900
916
content. splice ( start..end, new_value. bytes ( ) ) ;
901
917
offset += new_value. len ( ) as isize - ( end - start) as isize ;
@@ -1076,15 +1092,14 @@ contract A {
1076
1092
1077
1093
let updates = vec ! [
1078
1094
// Replace function libFn() visibility to external
1079
- ( 36 , 44 , "external" . to_string ( ) ) ,
1095
+ ( 36 .. 44 , "external" ) ,
1080
1096
// Replace contract A name to contract B
1081
- ( 80 , 90 , "contract B" . to_string ( ) ) ,
1097
+ ( 80 .. 90 , "contract B" ) ,
1082
1098
// Remove function c()
1083
- ( 159 , 222 , String :: new ( ) ) ,
1099
+ ( 159 .. 222 , "" ) ,
1084
1100
// Replace function e() logic
1085
- ( 276 , 296 , "// no logic" . to_string( ) ) ,
1086
- ]
1087
- . into_iter ( ) ;
1101
+ ( 276 ..296 , "// no logic" ) ,
1102
+ ] ;
1088
1103
1089
1104
assert_eq ! (
1090
1105
replace_source_content( original_content, updates) ,
0 commit comments