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![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#[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
118impl<'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}