Skip to content

Commit 846f863

Browse files
authored
chore: simplify pragma parsing (#260)
1 parent 33b3ca8 commit 846f863

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

crates/compilers/src/compilers/solc/compiler.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ impl Solc {
128128
/// Parses the given source looking for the `pragma` definition and
129129
/// returns the corresponding SemVer version requirement.
130130
pub fn source_version_req(source: &Source) -> Result<VersionReq> {
131-
let version =
132-
utils::find_version_pragma(&source.content).ok_or(SolcError::PragmaNotFound)?;
133-
Ok(SolData::parse_version_req(version.as_str())?)
131+
Ok(SolData::parse_version_pragma(&source.content).ok_or(SolcError::PragmaNotFound)??)
134132
}
135133

136134
/// Given a Solidity source, it detects the latest compiler version which can be used

crates/compilers/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl ProjectPathsConfig<SolcLanguage> {
211211
sources.push(content);
212212
}
213213

214-
if let Some(version) = combine_version_pragmas(version_pragmas) {
214+
if let Some(version) = combine_version_pragmas(&version_pragmas) {
215215
result.push_str(&version);
216216
result.push('\n');
217217
}

crates/compilers/src/flatten.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use foundry_compilers_core::{
1717
};
1818
use itertools::Itertools;
1919
use std::{
20-
collections::{HashMap, HashSet},
20+
collections::{BTreeSet, HashMap, HashSet},
2121
hash::Hash,
2222
path::{Path, PathBuf},
2323
};
@@ -724,7 +724,7 @@ impl Flattener {
724724

725725
let mut pragmas = Vec::new();
726726

727-
if let Some(version_pragma) = combine_version_pragmas(version_pragmas) {
727+
if let Some(version_pragma) = combine_version_pragmas(&version_pragmas) {
728728
pragmas.push(version_pragma);
729729
}
730730

@@ -881,26 +881,17 @@ pub fn collect_ordered_deps<D: ParsedSource + MaybeSolData>(
881881
Ok(ordered_deps)
882882
}
883883

884-
pub fn combine_version_pragmas(pragmas: Vec<&str>) -> Option<String> {
885-
let mut versions = pragmas
886-
.into_iter()
887-
.filter_map(|p| {
888-
SolData::parse_version_req(
889-
p.replace("pragma", "").replace("solidity", "").replace(';', "").trim(),
890-
)
891-
.ok()
892-
})
884+
pub fn combine_version_pragmas(pragmas: &[impl AsRef<str>]) -> Option<String> {
885+
let versions = pragmas
886+
.iter()
887+
.map(AsRef::as_ref)
888+
.filter_map(SolData::parse_version_pragma)
889+
.filter_map(Result::ok)
893890
.flat_map(|req| req.comparators)
894-
.collect::<HashSet<_>>()
895-
.into_iter()
896891
.map(|comp| comp.to_string())
897-
.collect::<Vec<_>>();
898-
899-
versions.sort();
900-
901-
if !versions.is_empty() {
902-
return Some(format!("pragma solidity {};", versions.iter().format(" ")));
892+
.collect::<BTreeSet<_>>();
893+
if versions.is_empty() {
894+
return None;
903895
}
904-
905-
None
896+
Some(format!("pragma solidity {};", versions.iter().format(" ")))
906897
}

crates/compilers/src/resolver/parse.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ impl SolData {
160160
}
161161
}
162162

163+
/// Parses the version pragma and returns the corresponding SemVer version requirement.
164+
///
165+
/// See [`parse_version_req`](Self::parse_version_req).
166+
pub fn parse_version_pragma(pragma: &str) -> Option<Result<VersionReq, semver::Error>> {
167+
let version = utils::find_version_pragma(pragma)?.as_str();
168+
Some(Self::parse_version_req(version))
169+
}
170+
163171
/// Returns the corresponding SemVer version requirement for the solidity version.
164172
///
165173
/// Note: This is a workaround for the fact that `VersionReq::parse` does not support whitespace

0 commit comments

Comments
 (0)