aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-10-31 09:05:26 +0000
committerAkshay <[email protected]>2021-10-31 16:05:15 +0000
commite8c955da4cbb042e6f9b89307d143f5bfa6779fa (patch)
tree0ae4ec11fd3dc0f8b69bc0f32c08858ef23a9485 /bin
parent246c69f8cfc74cf4c56fdaceaeb0562ed1f3dad5 (diff)
add `explain` subcommand and explanations to all lints
Diffstat (limited to 'bin')
-rw-r--r--bin/src/config.rs24
-rw-r--r--bin/src/err.rs10
-rw-r--r--bin/src/explain.rs15
-rw-r--r--bin/src/main.rs5
4 files changed, 54 insertions, 0 deletions
diff --git a/bin/src/config.rs b/bin/src/config.rs
index ef3a29d..1649017 100644
--- a/bin/src/config.rs
+++ b/bin/src/config.rs
@@ -26,6 +26,8 @@ pub enum SubCommand {
26 Fix(Fix), 26 Fix(Fix),
27 /// Fix exactly one issue at provided position 27 /// Fix exactly one issue at provided position
28 Single(Single), 28 Single(Single),
29 /// Print detailed explanation for a lint warning
30 Explain(Explain),
29} 31}
30 32
31#[derive(Clap, Debug)] 33#[derive(Clap, Debug)]
@@ -88,6 +90,13 @@ pub struct Single {
88 pub diff_only: bool, 90 pub diff_only: bool,
89} 91}
90 92
93#[derive(Clap, Debug)]
94pub struct Explain {
95 /// Warning code to explain
96 #[clap(parse(try_from_str = parse_warning_code))]
97 pub target: u32,
98}
99
91mod dirs { 100mod dirs {
92 use std::{ 101 use std::{
93 fs, 102 fs,
@@ -160,6 +169,21 @@ fn parse_line_col(src: &str) -> Result<(usize, usize), ConfigErr> {
160 } 169 }
161} 170}
162 171
172fn parse_warning_code(src: &str) -> Result<u32, ConfigErr> {
173 let mut char_stream = src.chars();
174 let severity = char_stream
175 .next()
176 .ok_or(ConfigErr::InvalidWarningCode(src.to_owned()))?
177 .to_ascii_lowercase();
178 match severity {
179 'w' => char_stream
180 .collect::<String>()
181 .parse::<u32>()
182 .map_err(|_| ConfigErr::InvalidWarningCode(src.to_owned())),
183 _ => Ok(0),
184 }
185}
186
163fn build_ignore_set(ignores: &[String]) -> Result<GlobSet, GlobError> { 187fn build_ignore_set(ignores: &[String]) -> Result<GlobSet, GlobError> {
164 let mut set = GlobSetBuilder::new(); 188 let mut set = GlobSetBuilder::new();
165 for pattern in ignores { 189 for pattern in ignores {
diff --git a/bin/src/err.rs b/bin/src/err.rs
index 4c16d69..1e52c2b 100644
--- a/bin/src/err.rs
+++ b/bin/src/err.rs
@@ -12,6 +12,8 @@ pub enum ConfigErr {
12 InvalidPath(#[from] io::Error), 12 InvalidPath(#[from] io::Error),
13 #[error("unable to parse `{0}` as line and column")] 13 #[error("unable to parse `{0}` as line and column")]
14 InvalidPosition(String), 14 InvalidPosition(String),
15 #[error("unable to parse `{0}` as warning code")]
16 InvalidWarningCode(String),
15} 17}
16 18
17// #[derive(Error, Debug)] 19// #[derive(Error, Debug)]
@@ -41,6 +43,12 @@ pub enum SingleFixErr {
41} 43}
42 44
43#[derive(Error, Debug)] 45#[derive(Error, Debug)]
46pub enum ExplainErr {
47 #[error("lint with code `{0}` not found")]
48 LintNotFound(u32),
49}
50
51#[derive(Error, Debug)]
44pub enum StatixErr { 52pub enum StatixErr {
45 // #[error("linter error: {0}")] 53 // #[error("linter error: {0}")]
46 // Lint(#[from] LintErr), 54 // Lint(#[from] LintErr),
@@ -50,4 +58,6 @@ pub enum StatixErr {
50 Single(#[from] SingleFixErr), 58 Single(#[from] SingleFixErr),
51 #[error("config error: {0}")] 59 #[error("config error: {0}")]
52 Config(#[from] ConfigErr), 60 Config(#[from] ConfigErr),
61 #[error("explain error: {0}")]
62 Explain(#[from] ExplainErr),
53} 63}
diff --git a/bin/src/explain.rs b/bin/src/explain.rs
new file mode 100644
index 0000000..6aefa7e
--- /dev/null
+++ b/bin/src/explain.rs
@@ -0,0 +1,15 @@
1use crate::err::ExplainErr;
2
3use lib::LINTS;
4
5pub fn explain(code: u32) -> Result<&'static str, ExplainErr> {
6 match code {
7 0 => Ok("syntax error"),
8 _ => LINTS
9 .values()
10 .flatten()
11 .find(|l| l.code() == code)
12 .map(|l| l.explanation())
13 .ok_or(ExplainErr::LintNotFound(code)),
14 }
15}
diff --git a/bin/src/main.rs b/bin/src/main.rs
index 90b79ce..31f6823 100644
--- a/bin/src/main.rs
+++ b/bin/src/main.rs
@@ -1,5 +1,6 @@
1mod config; 1mod config;
2mod err; 2mod err;
3mod explain;
3mod fix; 4mod fix;
4mod lint; 5mod lint;
5mod traits; 6mod traits;
@@ -86,6 +87,10 @@ fn _main() -> Result<(), StatixErr> {
86 print!("{}", &*single_fix_result.src) 87 print!("{}", &*single_fix_result.src)
87 } 88 }
88 } 89 }
90 SubCommand::Explain(explain_config) => {
91 let explanation = explain::explain(explain_config.target)?;
92 println!("{}", explanation)
93 }
89 } 94 }
90 Ok(()) 95 Ok(())
91} 96}