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
29 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::new(),
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#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Serialize)]
50pub enum Severity {
51 High,
52 Med,
53 Low,
54 Info,
55 Gas,
56 CodeSize,
57}
58
59impl Severity {
60 pub fn color(&self, message: &str) -> String {
61 match self {
62 Self::High => Paint::red(message).bold().to_string(),
63 Self::Med => Paint::rgb(message, 255, 135, 61).bold().to_string(),
64 Self::Low => Paint::yellow(message).bold().to_string(),
65 Self::Info => Paint::cyan(message).bold().to_string(),
66 Self::Gas => Paint::green(message).bold().to_string(),
67 Self::CodeSize => Paint::green(message).bold().to_string(),
68 }
69 }
70}
71
72impl From<Severity> for Level {
73 fn from(severity: Severity) -> Self {
74 match severity {
75 Severity::High | Severity::Med | Severity::Low => Self::Warning,
76 Severity::Info | Severity::Gas | Severity::CodeSize => Self::Note,
77 }
78 }
79}
80
81impl fmt::Display for Severity {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 let colored = match self {
84 Self::High => self.color("High"),
85 Self::Med => self.color("Med"),
86 Self::Low => self.color("Low"),
87 Self::Info => self.color("Info"),
88 Self::Gas => self.color("Gas"),
89 Self::CodeSize => self.color("CodeSize"),
90 };
91 write!(f, "{colored}")
92 }
93}
94
95impl<'de> Deserialize<'de> for Severity {
97 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
98 where
99 D: Deserializer<'de>,
100 {
101 let s = String::deserialize(deserializer)?;
102 FromStr::from_str(&s).map_err(serde::de::Error::custom)
103 }
104}
105
106impl FromStr for Severity {
107 type Err = String;
108
109 fn from_str(s: &str) -> Result<Self, Self::Err> {
110 match s.to_lowercase().as_str() {
111 "high" => Ok(Self::High),
112 "med" | "medium" => Ok(Self::Med),
113 "low" => Ok(Self::Low),
114 "info" => Ok(Self::Info),
115 "gas" => Ok(Self::Gas),
116 "size" | "codesize" | "code-size" => Ok(Self::CodeSize),
117 _ => Err(format!(
118 "unknown variant: found `{s}`, expected `one of `High`, `Med`, `Low`, `Info`, `Gas`, `CodeSize`"
119 )),
120 }
121 }
122}