blob: 9234d0a3f92fdba4ec249304ec31da538cd9daa4 (
plain)
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 std::{
borrow::Borrow,
path::{Path, PathBuf},
};
use anyhow::Result;
use rustc_hash::FxHashSet;
use crate::db::Language;
#[derive(Debug)]
pub struct DistroFile(PathBuf);
impl DistroFile {
pub fn path(&self) -> &Path {
&self.0
}
pub fn name(&self) -> &str {
self.0.file_name().unwrap().to_str().unwrap()
}
}
impl PartialEq for DistroFile {
fn eq(&self, other: &Self) -> bool {
self.name() == other.name()
}
}
impl Eq for DistroFile {}
impl std::hash::Hash for DistroFile {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name().hash(state)
}
}
impl Borrow<str> for DistroFile {
fn borrow(&self) -> &str {
self.name()
}
}
#[derive(Debug, Default)]
pub struct FileNameDB {
files: FxHashSet<DistroFile>,
}
impl FileNameDB {
pub fn get(&self, name: &str) -> Option<&Path> {
self.files.get(name).map(|file| file.path())
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &Path)> + '_ {
self.files.iter().map(|file| (file.name(), file.path()))
}
pub fn parse(
root_dirs: &[PathBuf],
reader: &mut dyn FnMut(&Path) -> Result<Vec<PathBuf>>,
) -> Result<Self> {
let files = root_dirs
.iter()
.flat_map(|dir| reader(dir))
.flatten()
.filter_map(|rel_path| {
Language::from_path(&rel_path)?;
rel_path.file_name()?.to_str()?;
let abs_path = root_dirs
.iter()
.rev()
.map(|dir| dir.join(&rel_path))
.find_map(|path| std::fs::canonicalize(path).ok())?;
Some(DistroFile(abs_path))
})
.collect();
Ok(Self { files })
}
}
|