diff options
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r-- | bin/src/main.rs | 43 |
1 files changed, 6 insertions, 37 deletions
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 | |||
3 | mod config; | 1 | mod config; |
4 | mod err; | 2 | mod err; |
3 | mod lint; | ||
5 | mod traits; | 4 | mod traits; |
6 | 5 | ||
7 | use std::io; | 6 | use std::io; |
8 | 7 | ||
9 | use crate::{ | 8 | use crate::{err::StatixErr, traits::WriteDiagnostic}; |
10 | err::{LintErr, StatixErr}, | ||
11 | traits::{LintResult, WriteDiagnostic}, | ||
12 | }; | ||
13 | 9 | ||
14 | use clap::Clap; | 10 | use clap::Clap; |
15 | use config::{LintConfig, Opts, SubCommand}; | 11 | use config::{LintConfig, Opts, SubCommand}; |
16 | use lib::LINTS; | ||
17 | use rnix::WalkEvent; | ||
18 | use vfs::VfsEntry; | ||
19 | |||
20 | fn 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 | ||
45 | fn _main() -> Result<(), StatixErr> { | 13 | fn _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 | ||