foundry_config/
lint.rs

1//! Configuration specific to the `forge lint` command and the `forge_lint` package
2
3use clap::ValueEnum;
4use core::fmt;
5use serde::{Deserialize, Deserializer, Serialize};
6use solar_interface::diagnostics::Level;
7use std::str::FromStr;
8use yansi::Paint;
9
10/// Contains the config and rule set
11#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
12pub struct LinterConfig {
13    /// Specifies which lints to run based on severity.
14    ///
15    /// If uninformed, all severities are checked.
16    pub severity: Vec<Severity>,
17
18    /// Deny specific lints based on their ID (e.g. "mixed-case-function").
19    pub exclude_lints: Vec<String>,
20
21    /// Globs to ignore
22    pub ignore: Vec<String>,
23
24    /// Whether to run linting during `forge build`.
25    ///
26    /// Defaults to true. Set to false to disable automatic linting during builds.
27    pub lint_on_build: bool,
28}
29impl Default for LinterConfig {
30    fn default() -> Self {
31        Self {
32            lint_on_build: true,
33            severity: Vec::new(),
34            exclude_lints: Vec::new(),
35            ignore: Vec::new(),
36        }
37    }
38}
39
40/// Severity of a lint
41#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Serialize)]
42pub enum Severity {
43    High,
44    Med,
45    Low,
46    Info,
47    Gas,
48}
49
50impl Severity {
51    pub fn color(&self, message: &str) -> String {
52        match self {
53            Self::High => Paint::red(message).bold().to_string(),
54            Self::Med => Paint::rgb(message, 255, 135, 61).bold().to_string(),
55            Self::Low => Paint::yellow(message).bold().to_string(),
56            Self::Info => Paint::cyan(message).bold().to_string(),
57            Self::Gas => Paint::green(message).bold().to_string(),
58        }
59    }
60}
61
62impl From<Severity> for Level {
63    fn from(severity: Severity) -> Self {
64        match severity {
65            Severity::High | Severity::Med | Severity::Low => Self::Warning,
66            Severity::Info | Severity::Gas => Self::Note,
67        }
68    }
69}
70
71impl fmt::Display for Severity {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        let colored = match self {
74            Self::High => self.color("High"),
75            Self::Med => self.color("Med"),
76            Self::Low => self.color("Low"),
77            Self::Info => self.color("Info"),
78            Self::Gas => self.color("Gas"),
79        };
80        write!(f, "{colored}")
81    }
82}
83
84// Custom deserialization to make `Severity` parsing case-insensitive
85impl<'de> Deserialize<'de> for Severity {
86    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
87    where
88        D: Deserializer<'de>,
89    {
90        let s = String::deserialize(deserializer)?;
91        FromStr::from_str(&s).map_err(serde::de::Error::custom)
92    }
93}
94
95impl FromStr for Severity {
96    type Err = String;
97
98    fn from_str(s: &str) -> Result<Self, Self::Err> {
99        match s.to_lowercase().as_str() {
100            "high" => Ok(Self::High),
101            "med" | "medium" => Ok(Self::Med),
102            "low" => Ok(Self::Low),
103            "info" => Ok(Self::Info),
104            "gas" => Ok(Self::Gas),
105            _ => Err(format!("unknown variant: found `{s}`, expected `one of `High`, `Med`, `Low`, `Info`, `Gas``")),
106        }
107    }
108}