Skip to main content

detect_solidity_version

Function detect_solidity_version 

Source
pub fn detect_solidity_version(
    src: impl AsRef<str>,
    path: impl AsRef<Path>,
) -> Result<Version>
Available on crate feature slang only.
Expand description

Search for pragma solidity statements in the source and return the highest matching Solidity version.

If no pragma directive is found, the version defaults to 0.8.0. Only the first pragma directive is considered, other ones in the file are ignored. Multiple version specifiers separated by a space are taken as meaning “and”, specifiers separated by || are taken as meaning “or”. Spaces take precedence over double-pipes.

Example: 0.6.0 || >=0.7.0 <0.8.0 means “either 0.6.0 or 0.7.x”.

Within the specifiers’ constraints, the highest version that is supported by slang_solidity is returned. In the above example, version 0.7.6 would be used.

§Errors

This function errors if the found version string cannot be parsed to a VersionReq or if the version is not supported by slang_solidity.

§Panics

This function panics if the LanguageFacts::ALL_VERSIONS list is empty.

§Examples

assert_eq!(
    detect_solidity_version("pragma solidity >=0.8.4 <0.8.26;", PathBuf::from("./file.sol")).unwrap(),
    Version::new(0, 8, 25)
);
assert_eq!(
    detect_solidity_version("pragma solidity ^0.4.0 || 0.6.x;", PathBuf::from("./file.sol")).unwrap(),
    Version::new(0, 6, 12)
);
assert_eq!(
    detect_solidity_version("contract Foo {}", PathBuf::from("./file.sol")).unwrap(),
    Version::new(0, 8, 0)
);
// this version of Solidity does not exist
assert!(detect_solidity_version("pragma solidity 0.7.7;", PathBuf::from("./file.sol")).is_err());