-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
128 lines (113 loc) · 3.77 KB
/
mod.rs
File metadata and controls
128 lines (113 loc) · 3.77 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! Provides colors for filepaths.
use crate::color::Color;
use crate::ext::PathExt as _;
use owo_colors::AnsiColors::{Black, Blue, Cyan, Green, Red, Yellow};
use std::path::Path;
use std::sync::LazyLock;
/// Gets a color for a path.
pub fn for_path<P>(path: P) -> Option<Color>
where
P: AsRef<Path>,
{
let path = path.as_ref();
path.file_name()
.and_then(|s| s.to_str())
.and_then(for_filename)
.or_else(|| {
path.double_extension()
.and_then(|(prefix, suffix)| {
prefix
.to_str()
.and_then(|prefix| suffix.to_str().map(|suffix| (prefix, suffix)))
})
.and_then(for_double_extension)
})
.or_else(|| {
path.extension()
.and_then(|extension| extension.to_str())
.and_then(for_extension)
})
.or_else(|| for_filename_glob(path))
}
/// Gets a color for a filename.
fn for_filename(filename: &str) -> Option<Color> {
// NOTE These should be in alphabetical order and ignoring any leading `.` for
// easier code review.
let color = match filename {
".git" | ".gitattributes" | ".gitignore" | ".gitmodules" | ".git-blame-ignore-revs" => {
Red.into()
}
".github" => Black.into(),
"LICENCE" | "LICENSE" | "licence" | "license" => shared::LICENSE,
".vscode" => Blue.into(),
_ => return None,
};
Some(color)
}
/// Gets a color for a file extension.
fn for_extension(extension: &str) -> Option<Color> {
// NOTE These should be in alphabetical order for easier code review.
let color = match extension {
"7z" => Black.into(),
"gif" => Green.into(),
"jpeg" | "jpg" => Yellow.into(),
"png" => Cyan.into(),
"sqlite" | "sqlite3" => Blue.into(),
"tar" => Green.into(),
"zip" => Blue.into(),
_ => return None,
};
Some(color)
}
/// Gets a color for the double extension.
fn for_double_extension(double_extension: (&str, &str)) -> Option<Color> {
let color = match double_extension {
("tar", "gz") => Green.into(),
_ => return None,
};
Some(color)
}
/// Gets a color based on a matching glob for a path.
fn for_filename_glob(path: &Path) -> Option<Color> {
use glob::{MatchOptions, Pattern};
/// Maps a raw glob pattern to a color with `(glob, color)` tuples.
const RAW_MAPPINGS: &[(&str, Color)] = &[("LICEN[CS]E-*", shared::LICENSE)];
const OPTIONS: MatchOptions = MatchOptions {
case_sensitive: false,
require_literal_separator: false,
require_literal_leading_dot: false,
};
/// The compiled glob-to-color mappings.
static COMPILED_MAPPINGS: LazyLock<Vec<(Pattern, Color)>> = LazyLock::new(|| {
RAW_MAPPINGS
.iter()
.map(|(raw, color)| (Pattern::new(raw).expect("Pattern should be valid"), *color))
.collect()
});
// NOTE This may receive a path with `./`, so we'll clean to just the prefix.
path.file_name().and_then(|s| s.to_str()).and_then(|path| {
COMPILED_MAPPINGS
.iter()
.find_map(|(glob, color)| glob.matches_with(path, OPTIONS).then_some(*color))
})
}
/// Colors that represent one file type, but have multiple filenames and/or extensions
/// for that file type.
mod shared {
use super::*;
/// Color for license files.
pub const LICENSE: Color = Color::Ansi(Yellow);
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
#[case("foo.tar.gz", Some(Green.into()))]
fn test_for_path<P>(#[case] path: P, #[case] expected: Option<Color>)
where
P: AsRef<Path>,
{
assert_eq!(expected, for_path(path));
}
}