mirror of
https://github.com/pezkuwichain/revive-differential-tests.git
synced 2026-05-01 01:47:59 +00:00
the mode mini parser
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::modes::Mode;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Case {
|
||||
#[serde(rename(deserialize = "modes"))]
|
||||
pub modes: Vec<Mode>,
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
//! The revive differential tests case format.
|
||||
|
||||
pub mod case;
|
||||
pub mod modes;
|
||||
@@ -0,0 +1,70 @@
|
||||
use serde::Deserialize;
|
||||
use serde::de::Deserializer;
|
||||
|
||||
/// Specifies the compilation mode of the test artifact.
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub enum Mode {
|
||||
Solidity(SolcMode),
|
||||
Unknown(String),
|
||||
}
|
||||
|
||||
/// Specify Solidity specific compiler options.
|
||||
#[derive(Debug, Default, Clone, Eq, PartialEq)]
|
||||
pub struct SolcMode {
|
||||
pub solc_version: Option<semver::VersionReq>,
|
||||
pub solc_optimize: Option<bool>,
|
||||
pub llvm_optimizer_settings: Vec<String>,
|
||||
}
|
||||
|
||||
impl SolcMode {
|
||||
/// Try to parse a mode string into a solc mode.
|
||||
/// Returns `None` if the string wasn't a solc YUL mode string.
|
||||
///
|
||||
/// The mode string is expected to start with the `Y` ID (YUL ID),
|
||||
/// optionally followed by `+` or `-` for the solc optimizer settings.
|
||||
///
|
||||
/// Options can be separated by a whitespace contain the following
|
||||
/// - A solc `SemVer version requirement` string
|
||||
/// - One or more `-OX` where X is a supposed to be an LLVM opt mode
|
||||
pub fn parse_from_mode_string(mode_string: &str) -> Option<Self> {
|
||||
let mut result = Self::default();
|
||||
|
||||
let mut parts = mode_string.trim().split(" ");
|
||||
|
||||
match parts.next()? {
|
||||
"Y" => {}
|
||||
"Y+" => result.solc_optimize = Some(true),
|
||||
"Y-" => result.solc_optimize = Some(false),
|
||||
_ => return None,
|
||||
}
|
||||
|
||||
while let Some(part) = parts.next() {
|
||||
if let Ok(solc_version) = semver::VersionReq::parse(part) {
|
||||
result.solc_version = Some(solc_version);
|
||||
continue;
|
||||
}
|
||||
if part.starts_with("-O") {
|
||||
result.llvm_optimizer_settings.push(part[2..].to_string());
|
||||
continue;
|
||||
}
|
||||
panic!("the YUL mode string {mode_string} failed to parse, invalid part: {part}")
|
||||
}
|
||||
|
||||
Some(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Mode {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let mode_string = String::deserialize(deserializer)?;
|
||||
|
||||
if let Some(solc_mode) = SolcMode::parse_from_mode_string(&mode_string) {
|
||||
return Ok(Self::Solidity(solc_mode));
|
||||
}
|
||||
|
||||
Ok(Self::Unknown(mode_string))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user