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
29    /// Configurable patterns that should be excluded when performing `mixedCase` lint checks.
30    ///
31    /// Default's to ["ERC", "URI"] to allow common names like `rescueERC20`, `ERC721TokenReceiver`
32    /// or `tokenURI`.
33    pub mixed_case_exceptions: Vec<String>,
34}
35
36impl Default for LinterConfig {
37    fn default() -> Self {
38        Self {
39            lint_on_build: true,
40            severity: vec![Severity::High, Severity::Med, Severity::Low],
41            exclude_lints: Vec::new(),
42            ignore: Vec::new(),
43            mixed_case_exceptions: vec!["ERC".to_string(), "URI".to_string()],
44        }
45    }
46}
47
48/// Severity of a lint.
49#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
50pub enum Severity {
51    High,
52    Med,
53    Low,
54    Info,
55    Gas,
56    CodeSize,
57}
58
59impl Severity {
60    fn to_str(self) -> &'static str {
61        match self {
62            Self::High => "High",
63            Self::Med => "Med",
64            Self::Low => "Low",
65            Self::Info => "Info",
66            Self::Gas => "Gas",
67            Self::CodeSize => "CodeSize",
68        }
69    }
70
71    fn to_str_kebab(self) -> &'static str {
72        match self {
73            Self::High => "high",
74            Self::Med => "medium",
75            Self::Low => "low",
76            Self::Info => "info",
77            Self::Gas => "gas",
78            Self::CodeSize => "code-size",
79        }
80    }
81
82    pub fn color(&self, message: &str) -> String {
83        match self {
84            Self::High => Paint::red(message).bold().to_string(),
85            Self::Med => Paint::rgb(message, 255, 135, 61).bold().to_string(),
86            Self::Low => Paint::yellow(message).bold().to_string(),
87            Self::Info => Paint::cyan(message).bold().to_string(),
88            Self::Gas => Paint::green(message).bold().to_string(),
89            Self::CodeSize => Paint::green(message).bold().to_string(),
90        }
91    }
92}
93
94impl From<Severity> for Level {
95    fn from(severity: Severity) -> Self {
96        match severity {
97            Severity::High | Severity::Med | Severity::Low => Self::Warning,
98            Severity::Info | Severity::Gas | Severity::CodeSize => Self::Note,
99        }
100    }
101}
102
103impl fmt::Display for Severity {
104    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105        write!(f, "{}", self.color(self.to_str()))
106    }
107}
108
109impl Serialize for Severity {
110    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
111    where
112        S: serde::Serializer,
113    {
114        self.to_str_kebab().serialize(serializer)
115    }
116}
117
118// Custom deserialization to make `Severity` parsing case-insensitive
119impl<'de> Deserialize<'de> for Severity {
120    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
121    where
122        D: Deserializer<'de>,
123    {
124        let s = String::deserialize(deserializer)?;
125        FromStr::from_str(&s).map_err(serde::de::Error::custom)
126    }
127}
128
129impl FromStr for Severity {
130    type Err = String;
131
132    fn from_str(s: &str) -> Result<Self, Self::Err> {
133        match s.to_lowercase().as_str() {
134            "high" => Ok(Self::High),
135            "med" | "medium" => Ok(Self::Med),
136            "low" => Ok(Self::Low),
137            "info" => Ok(Self::Info),
138            "gas" => Ok(Self::Gas),
139            "size" | "codesize" | "code-size" => Ok(Self::CodeSize),
140            _ => Err(format!(
141                "unknown variant: found `{s}`, expected `one of `High`, `Med`, `Low`, `Info`, `Gas`, `CodeSize`"
142            )),
143        }
144    }
145}