aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-09-21 12:11:47 +0100
committerAkshay <[email protected]>2021-09-21 12:12:30 +0100
commit3eec886fe83b30636622d6ba97880bf473de8a0a (patch)
treed456eac8a9a78bc211cdd1223dadcbdf647552b5
parent40867728c023a9f95f346671b51be68002cc7552 (diff)
use stderr by default, improved macro code
-rw-r--r--Cargo.lock8
-rw-r--r--bin/src/main.rs41
-rw-r--r--macros/src/lib.rs4
3 files changed, 39 insertions, 14 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 73188a4..f222d10 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,12 @@
3version = 3 3version = 3
4 4
5[[package]] 5[[package]]
6name = "anyhow"
7version = "1.0.44"
8source = "registry+https://github.com/rust-lang/crates.io-index"
9checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1"
10
11[[package]]
6name = "ariadne" 12name = "ariadne"
7version = "0.1.3" 13version = "0.1.3"
8source = "registry+https://github.com/rust-lang/crates.io-index" 14source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -21,8 +27,10 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
21name = "bin" 27name = "bin"
22version = "0.1.0" 28version = "0.1.0"
23dependencies = [ 29dependencies = [
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
6use anyhow::{Context, Result}; 6use anyhow::{Context, Result};
7use ariadne::{Color, Fmt, Label, Report as CliReport, ReportKind as CliReportKind, Source}; 7use ariadne::{
8 CharSet, Color, Config as CliConfig, Label, Report as CliReport,
9 ReportKind as CliReportKind, Source,
10};
8use lib::{Report, LINTS}; 11use lib::{Report, LINTS};
9use rnix::WalkEvent; 12use rnix::{TextRange, WalkEvent};
10 13
11fn analyze(file: &str) -> Result<Vec<Report>> { 14fn 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
30fn print_report(report: Report, file_src: &str, file_path: &Path) -> Result<()> { 33fn 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) = &note_lit.lit { 91 if let Lit::Str(note_str) = &note_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 };