foundry_cheatcodes/version.rs
1use crate::{Cheatcode, Cheatcodes, Result, Vm::*};
2use alloy_sol_types::SolValue;
3use foundry_common::version::SEMVER_VERSION;
4use semver::Version;
5use std::cmp::Ordering;
6
7impl Cheatcode for foundryVersionCmpCall {
8 fn apply(&self, _state: &mut Cheatcodes) -> Result {
9 let Self { version } = self;
10 foundry_version_cmp(version).map(|cmp| (cmp as i8).abi_encode())
11 }
12}
13
14impl Cheatcode for foundryVersionAtLeastCall {
15 fn apply(&self, _state: &mut Cheatcodes) -> Result {
16 let Self { version } = self;
17 foundry_version_cmp(version).map(|cmp| cmp.is_ge().abi_encode())
18 }
19}
20
21fn foundry_version_cmp(version: &str) -> Result<Ordering> {
22 version_cmp(SEMVER_VERSION.split('-').next().unwrap(), version)
23}
24
25fn version_cmp(version_a: &str, version_b: &str) -> Result<Ordering> {
26 let version_a = parse_version(version_a)?;
27 let version_b = parse_version(version_b)?;
28 Ok(version_a.cmp(&version_b))
29}
30
31fn parse_version(version: &str) -> Result<Version> {
32 let version =
33 Version::parse(version).map_err(|e| fmt_err!("invalid version `{version}`: {e}"))?;
34 if !version.pre.is_empty() {
35 return Err(fmt_err!("invalid version `{version}`: pre-release versions are not supported"));
36 }
37 if !version.build.is_empty() {
38 return Err(fmt_err!("invalid version `{version}`: build metadata is not supported"));
39 }
40 Ok(version)
41}