foundry_config/
lint.rs
1use clap::ValueEnum;
4use core::fmt;
5use serde::{Deserialize, Deserializer, Serialize};
6use solar_interface::diagnostics::Level;
7use std::str::FromStr;
8use yansi::Paint;
9
10#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
12pub struct LinterConfig {
13 pub severity: Vec<Severity>,
17
18 pub exclude_lints: Vec<String>,
20
21 pub ignore: Vec<String>,
23
24 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#[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
84impl<'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}