aboutsummaryrefslogtreecommitdiff
path: root/bin/src/lint.rs
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/lint.rs
parent33044d3f3b1d50148d3446e876688847f33897e9 (diff)
refactor out lint runner into lint module
Diffstat (limited to 'bin/src/lint.rs')
-rw-r--r--bin/src/lint.rs36
1 files changed, 36 insertions, 0 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}