aboutsummaryrefslogtreecommitdiff
path: root/bin/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-10-19 15:01:01 +0100
committerAkshay <[email protected]>2021-10-19 15:01:01 +0100
commite4c4a77d03ffe02ec5b3fda71eb4ccd97e9cdf58 (patch)
tree9d2e1604306c1b42998760d020f75130937e2ea5 /bin/src
parent33044d3f3b1d50148d3446e876688847f33897e9 (diff)
refactor out lint runner into lint module
Diffstat (limited to 'bin/src')
-rw-r--r--bin/src/lint.rs36
-rw-r--r--bin/src/main.rs43
-rw-r--r--bin/src/traits.rs18
3 files changed, 49 insertions, 48 deletions
diff --git a/bin/src/lint.rs b/bin/src/lint.rs
new file mode 100644
index 0000000..76b2b8c
--- /dev/null
+++ b/bin/src/lint.rs
@@ -0,0 +1,36 @@
1use crate::err::LintErr;
2
3use lib::{LINTS, Report};
4use rnix::WalkEvent;
5use vfs::{VfsEntry, FileId};
6
7#[derive(Debug)]
8pub struct LintResult {
9 pub file_id: FileId,
10 pub reports: Vec<Report>,
11}
12
13pub fn lint(vfs_entry: VfsEntry) -> Result<LintResult, LintErr> {
14 let source = vfs_entry.contents;
15 let parsed = rnix::parse(source)
16 .as_result()
17 .map_err(|e| LintErr::Parse(vfs_entry.file_path.to_path_buf(), e))?;
18 let reports = parsed
19 .node()
20 .preorder_with_tokens()
21 .filter_map(|event| match event {
22 WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| {
23 rules
24 .iter()
25 .filter_map(|rule| rule.validate(&child))
26 .collect::<Vec<_>>()
27 }),
28 _ => None,
29 })
30 .flatten()
31 .collect();
32 Ok(LintResult {
33 file_id: vfs_entry.file_id,
34 reports,
35 })
36}
diff --git a/bin/src/main.rs b/bin/src/main.rs
index b26151d..a3f04d7 100644
--- a/bin/src/main.rs
+++ b/bin/src/main.rs
@@ -1,57 +1,26 @@
1#![feature(path_try_exists)]
2
3mod config; 1mod config;
4mod err; 2mod err;
3mod lint;
5mod traits; 4mod traits;
6 5
7use std::io; 6use std::io;
8 7
9use crate::{ 8use crate::{err::StatixErr, traits::WriteDiagnostic};
10 err::{LintErr, StatixErr},
11 traits::{LintResult, WriteDiagnostic},
12};
13 9
14use clap::Clap; 10use clap::Clap;
15use config::{LintConfig, Opts, SubCommand}; 11use config::{LintConfig, Opts, SubCommand};
16use lib::LINTS;
17use rnix::WalkEvent;
18use vfs::VfsEntry;
19
20fn analyze<'ρ>(vfs_entry: VfsEntry<'ρ>) -> Result<LintResult, LintErr> {
21 let source = vfs_entry.contents;
22 let parsed = rnix::parse(source)
23 .as_result()
24 .map_err(|e| LintErr::Parse(vfs_entry.file_path.to_path_buf(), e))?;
25 let reports = parsed
26 .node()
27 .preorder_with_tokens()
28 .filter_map(|event| match event {
29 WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| {
30 rules
31 .iter()
32 .filter_map(|rule| rule.validate(&child))
33 .collect::<Vec<_>>()
34 }),
35 _ => None,
36 })
37 .flatten()
38 .collect();
39 Ok(LintResult {
40 file_id: vfs_entry.file_id,
41 reports,
42 })
43}
44 12
45fn _main() -> Result<(), StatixErr> { 13fn _main() -> Result<(), StatixErr> {
46 // TODO: accept cli args, construct a CLI config with a list of files to analyze
47 let opts = Opts::parse(); 14 let opts = Opts::parse();
48 match opts.subcmd { 15 match opts.subcmd {
49 Some(SubCommand::Fix(_)) => {} 16 Some(SubCommand::Fix(_)) => {
17 eprintln!("`fix` not yet supported");
18 }
50 None => { 19 None => {
51 let lint_config = LintConfig::from_opts(opts)?; 20 let lint_config = LintConfig::from_opts(opts)?;
52 let vfs = lint_config.vfs()?; 21 let vfs = lint_config.vfs()?;
53 let (reports, errors): (Vec<_>, Vec<_>) = 22 let (reports, errors): (Vec<_>, Vec<_>) =
54 vfs.iter().map(analyze).partition(Result::is_ok); 23 vfs.iter().map(lint::lint).partition(Result::is_ok);
55 let lint_results: Vec<_> = reports.into_iter().map(Result::unwrap).collect(); 24 let lint_results: Vec<_> = reports.into_iter().map(Result::unwrap).collect();
56 let errors: Vec<_> = errors.into_iter().map(Result::unwrap_err).collect(); 25 let errors: Vec<_> = errors.into_iter().map(Result::unwrap_err).collect();
57 26
diff --git a/bin/src/traits.rs b/bin/src/traits.rs
index 1807ad0..c3427df 100644
--- a/bin/src/traits.rs
+++ b/bin/src/traits.rs
@@ -3,19 +3,14 @@ use std::{
3 str, 3 str,
4}; 4};
5 5
6use crate::lint::LintResult;
7
6use ariadne::{ 8use ariadne::{
7 CharSet, Color, Config as CliConfig, Label, LabelAttach, Report as CliReport, 9 CharSet, Color, Config as CliConfig, Fmt, Label, LabelAttach, Report as CliReport,
8 ReportKind as CliReportKind, Source, Fmt 10 ReportKind as CliReportKind, Source,
9}; 11};
10use lib::Report;
11use rnix::TextRange; 12use rnix::TextRange;
12use vfs::{FileId, ReadOnlyVfs}; 13use vfs::ReadOnlyVfs;
13
14#[derive(Debug)]
15pub struct LintResult {
16 pub file_id: FileId,
17 pub reports: Vec<Report>,
18}
19 14
20pub trait WriteDiagnostic { 15pub trait WriteDiagnostic {
21 fn write(&mut self, report: &LintResult, vfs: &ReadOnlyVfs) -> io::Result<()>; 16 fn write(&mut self, report: &LintResult, vfs: &ReadOnlyVfs) -> io::Result<()>;
@@ -69,7 +64,8 @@ where
69 64
70// everything within backticks is colorized, backticks are removed 65// everything within backticks is colorized, backticks are removed
71fn colorize(message: &str) -> String { 66fn colorize(message: &str) -> String {
72 message.split('`') 67 message
68 .split('`')
73 .enumerate() 69 .enumerate()
74 .map(|(idx, part)| { 70 .map(|(idx, part)| {
75 if idx % 2 == 1 { 71 if idx % 2 == 1 {