Skip to content

Commit d37cafb

Browse files
committed
Change replace_source_content sig, move apply_updates
1 parent 79d0a0f commit d37cafb

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

crates/compilers/src/flatten.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
2+
apply_updates,
23
compilers::{Compiler, ParsedSource},
34
filter::MaybeSolData,
4-
replace_source_content,
55
resolver::parse::SolData,
66
ArtifactOutput, CompilerSettings, Graph, Project, ProjectPathsConfig, Updates,
77
};
@@ -899,12 +899,3 @@ pub fn combine_version_pragmas(pragmas: Vec<&str>) -> Option<String> {
899899

900900
None
901901
}
902-
903-
pub fn apply_updates(sources: &mut Sources, mut updates: Updates) {
904-
for (path, source) in sources {
905-
if let Some(updates) = updates.remove(path) {
906-
source.content =
907-
Arc::new(replace_source_content(source.content.as_str(), updates.into_iter()));
908-
}
909-
}
910-
}

crates/compilers/src/lib.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -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;
6968
use solc::SolcSettings;
7069
use 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),

crates/compilers/src/preprocessor.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::project::Preprocessor;
22
use crate::{
3-
flatten::apply_updates,
3+
apply_updates,
44
multi::{MultiCompiler, MultiCompilerInput, MultiCompilerLanguage},
55
replace_source_content,
66
solc::{SolcCompiler, SolcVersionedInput},
@@ -88,13 +88,9 @@ pub(crate) fn interface_representation(
8888
return Err(err);
8989
}
9090

91-
let content = replace_source_content(
92-
content,
93-
spans_to_remove
94-
.iter()
95-
.map(|span| (span.to_range().start, span.to_range().end, String::new())),
96-
)
97-
.replace("\n", "");
91+
let content =
92+
replace_source_content(content, spans_to_remove.iter().map(|span| (span.to_range(), "")))
93+
.replace("\n", "");
9894
Ok(utils::RE_TWO_OR_MORE_SPACES.replace_all(&content, "").to_string())
9995
}
10096

0 commit comments

Comments
 (0)