diff options
author | Akshay <[email protected]> | 2021-09-21 12:11:47 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-09-21 12:12:30 +0100 |
commit | 3eec886fe83b30636622d6ba97880bf473de8a0a (patch) | |
tree | d456eac8a9a78bc211cdd1223dadcbdf647552b5 | |
parent | 40867728c023a9f95f346671b51be68002cc7552 (diff) |
use stderr by default, improved macro code
-rw-r--r-- | Cargo.lock | 8 | ||||
-rw-r--r-- | bin/src/main.rs | 41 | ||||
-rw-r--r-- | macros/src/lib.rs | 4 |
3 files changed, 39 insertions, 14 deletions
@@ -3,6 +3,12 @@ | |||
3 | version = 3 | 3 | version = 3 |
4 | 4 | ||
5 | [[package]] | 5 | [[package]] |
6 | name = "anyhow" | ||
7 | version = "1.0.44" | ||
8 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
9 | checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1" | ||
10 | |||
11 | [[package]] | ||
6 | name = "ariadne" | 12 | name = "ariadne" |
7 | version = "0.1.3" | 13 | version = "0.1.3" |
8 | source = "registry+https://github.com/rust-lang/crates.io-index" | 14 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -21,8 +27,10 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" | |||
21 | name = "bin" | 27 | name = "bin" |
22 | version = "0.1.0" | 28 | version = "0.1.0" |
23 | dependencies = [ | 29 | dependencies = [ |
30 | "anyhow", | ||
24 | "ariadne", | 31 | "ariadne", |
25 | "lib", | 32 | "lib", |
33 | "rnix", | ||
26 | ] | 34 | ] |
27 | 35 | ||
28 | [[package]] | 36 | [[package]] |
diff --git a/bin/src/main.rs b/bin/src/main.rs index ae45bfe..60afd5e 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs | |||
@@ -4,9 +4,12 @@ use std::{ | |||
4 | }; | 4 | }; |
5 | 5 | ||
6 | use anyhow::{Context, Result}; | 6 | use anyhow::{Context, Result}; |
7 | use ariadne::{Color, Fmt, Label, Report as CliReport, ReportKind as CliReportKind, Source}; | 7 | use ariadne::{ |
8 | CharSet, Color, Config as CliConfig, Label, Report as CliReport, | ||
9 | ReportKind as CliReportKind, Source, | ||
10 | }; | ||
8 | use lib::{Report, LINTS}; | 11 | use lib::{Report, LINTS}; |
9 | use rnix::WalkEvent; | 12 | use rnix::{TextRange, WalkEvent}; |
10 | 13 | ||
11 | fn analyze(file: &str) -> Result<Vec<Report>> { | 14 | fn analyze(file: &str) -> Result<Vec<Report>> { |
12 | let parsed = rnix::parse(file).as_result()?; | 15 | let parsed = rnix::parse(file).as_result()?; |
@@ -28,6 +31,7 @@ fn analyze(file: &str) -> Result<Vec<Report>> { | |||
28 | } | 31 | } |
29 | 32 | ||
30 | fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> { | 33 | fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> { |
34 | let range = |at: TextRange| at.start().into()..at.end().into(); | ||
31 | let src_id = file_path.to_str().unwrap_or("<unknown>"); | 35 | let src_id = file_path.to_str().unwrap_or("<unknown>"); |
32 | let offset = report | 36 | let offset = report |
33 | .diagnostics | 37 | .diagnostics |
@@ -39,20 +43,33 @@ fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> | |||
39 | .diagnostics | 43 | .diagnostics |
40 | .iter() | 44 | .iter() |
41 | .fold( | 45 | .fold( |
42 | CliReport::build(CliReportKind::Warning, src_id, offset), | 46 | CliReport::build(CliReportKind::Warning, src_id, offset) |
47 | .with_config( | ||
48 | CliConfig::default() | ||
49 | .with_cross_gap(true) | ||
50 | .with_char_set(CharSet::ExtendedAscii), | ||
51 | ) | ||
52 | .with_message(report.note), | ||
43 | |cli_report, diagnostic| { | 53 | |cli_report, diagnostic| { |
44 | let range = { | 54 | let cli_report = cli_report.with_label( |
45 | let at = diagnostic.at; | 55 | Label::new((src_id, range(diagnostic.at))) |
46 | at.start().into()..at.end().into() | 56 | .with_message(&diagnostic.message) |
47 | }; | 57 | .with_color(Color::Magenta), |
48 | cli_report.with_label( | 58 | ); |
49 | Label::new((src_id, range)) | 59 | if let Some(s) = &diagnostic.suggestion { |
50 | .with_message(diagnostic.message.as_str().fg(Color::Yellow)), | 60 | let replacement_msg = format!("Try: `{}`", s.fix); |
51 | ) | 61 | cli_report.with_label( |
62 | Label::new((src_id, range(s.at))) | ||
63 | .with_message(replacement_msg) | ||
64 | .with_color(Color::Yellow), | ||
65 | ) | ||
66 | } else { | ||
67 | cli_report | ||
68 | } | ||
52 | }, | 69 | }, |
53 | ) | 70 | ) |
54 | .finish() | 71 | .finish() |
55 | .print((src_id, Source::from(file_src))) | 72 | .eprint((src_id, Source::from(file_src))) |
56 | .context("failed to print report to stdout") | 73 | .context("failed to print report to stdout") |
57 | } | 74 | } |
58 | 75 | ||
diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 42f7565..36a8bb5 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs | |||
@@ -73,7 +73,7 @@ fn generate_name_fn(meta: &LintMeta) -> TokenStream2 { | |||
73 | if let syn::Expr::Lit(name_lit) = name { | 73 | if let syn::Expr::Lit(name_lit) = name { |
74 | if let Lit::Str(name_str) = &name_lit.lit { | 74 | if let Lit::Str(name_str) = &name_lit.lit { |
75 | return quote! { | 75 | return quote! { |
76 | fn name(&self) -> &str { | 76 | fn name() -> &'static str { |
77 | #name_str | 77 | #name_str |
78 | } | 78 | } |
79 | }; | 79 | }; |
@@ -90,7 +90,7 @@ fn generate_note_fn(meta: &LintMeta) -> TokenStream2 { | |||
90 | if let syn::Expr::Lit(note_lit) = note { | 90 | if let syn::Expr::Lit(note_lit) = note { |
91 | if let Lit::Str(note_str) = ¬e_lit.lit { | 91 | if let Lit::Str(note_str) = ¬e_lit.lit { |
92 | return quote! { | 92 | return quote! { |
93 | fn note(&self) -> &str { | 93 | fn note() -> &'static str { |
94 | #note_str | 94 | #note_str |
95 | } | 95 | } |
96 | }; | 96 | }; |