diff options
author | Akshay <[email protected]> | 2021-10-31 09:06:16 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-31 09:06:16 +0000 |
commit | 4567a246a0b63418f17ead4adc81f5ebab8bdd81 (patch) | |
tree | dc03049144d45bf8980e9213c399e7a334c1724b | |
parent | 1748d5f1546d2160ec8f949a56081403a3f9df62 (diff) |
add explain subcommand
-rw-r--r-- | bin/src/config.rs | 24 | ||||
-rw-r--r-- | bin/src/err.rs | 10 | ||||
-rw-r--r-- | bin/src/explain.rs | 15 | ||||
-rw-r--r-- | bin/src/main.rs | 5 |
4 files changed, 54 insertions, 0 deletions
diff --git a/bin/src/config.rs b/bin/src/config.rs index ef3a29d..7188c45 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 explaination 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)] | ||
94 | pub struct Explain { | ||
95 | /// Warning code to explain | ||
96 | #[clap(parse(try_from_str = parse_warning_code))] | ||
97 | pub target: u32, | ||
98 | } | ||
99 | |||
91 | mod dirs { | 100 | mod 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 | ||
172 | fn 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 | |||
163 | fn build_ignore_set(ignores: &[String]) -> Result<GlobSet, GlobError> { | 187 | fn 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)] |
46 | pub enum ExplainErr { | ||
47 | #[error("lint with code `{0}` not found")] | ||
48 | LintNotFound(u32), | ||
49 | } | ||
50 | |||
51 | #[derive(Error, Debug)] | ||
44 | pub enum StatixErr { | 52 | pub 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..c663427 --- /dev/null +++ b/bin/src/explain.rs | |||
@@ -0,0 +1,15 @@ | |||
1 | use crate::err::ExplainErr; | ||
2 | |||
3 | use lib::LINTS; | ||
4 | |||
5 | pub 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.explaination()) | ||
13 | .ok_or(ExplainErr::LintNotFound(code)), | ||
14 | } | ||
15 | } | ||
diff --git a/bin/src/main.rs b/bin/src/main.rs index 90b79ce..138b243 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs | |||
@@ -1,5 +1,6 @@ | |||
1 | mod config; | 1 | mod config; |
2 | mod err; | 2 | mod err; |
3 | mod explain; | ||
3 | mod fix; | 4 | mod fix; |
4 | mod lint; | 5 | mod lint; |
5 | mod traits; | 6 | mod 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 explaination = explain::explain(explain_config.target)?; | ||
92 | println!("{}", explaination); | ||
93 | } | ||
89 | } | 94 | } |
90 | Ok(()) | 95 | Ok(()) |
91 | } | 96 | } |