1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::{prelude::*, LintContext};
use camino::Utf8Path;
use std::{fs, io};
pub trait FilePathLinter: Linter {
fn run<'l>(
&self,
ctx: &FilePathContext<'l>,
out: &mut LintFormatter<'l, '_>,
) -> Result<RunStatus<'l>>;
}
#[derive(Clone, Debug)]
pub struct FilePathContext<'l> {
project_ctx: &'l ProjectContext<'l>,
file_path: &'l Utf8Path,
}
impl<'l> FilePathContext<'l> {
pub fn new(project_ctx: &'l ProjectContext<'l>, file_path: &'l Utf8Path) -> Self {
Self {
project_ctx,
file_path,
}
}
pub fn project_ctx(&self) -> &'l ProjectContext<'l> {
self.project_ctx
}
pub fn file_path(&self) -> &'l Utf8Path {
self.file_path
}
pub fn extension(&self) -> Option<&'l str> {
self.file_path.extension()
}
pub(super) fn load(self) -> Result<Option<ContentContext<'l>>> {
let full_path = self.project_ctx.full_path(self.file_path);
let contents_opt = read_file(&full_path)
.map_err(|err| SystemError::io(format!("loading {}", full_path), err))?;
Ok(contents_opt.map(|content| ContentContext::new(self, content)))
}
}
impl<'l> LintContext<'l> for FilePathContext<'l> {
fn kind(&self) -> LintKind<'l> {
LintKind::FilePath(self.file_path)
}
}
fn read_file(full_path: &Utf8Path) -> io::Result<Option<Vec<u8>>> {
match fs::read(full_path) {
Ok(bytes) => Ok(Some(bytes)),
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
Ok(None)
} else {
Err(err)
}
}
}
}