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
use crate::MANIFEST_FILENAME;
use anyhow::bail;
use mun_paths::{AbsPath, AbsPathBuf};
use rustc_hash::FxHashSet;
use std::{convert::TryFrom, fs::read_dir, io};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ProjectManifest {
pub path: AbsPathBuf,
}
impl ProjectManifest {
pub fn from_manifest_path(path: impl AsRef<AbsPath>) -> anyhow::Result<Self> {
let path = path.as_ref();
if path.ends_with(MANIFEST_FILENAME) {
Ok(Self {
path: path.to_path_buf(),
})
} else {
bail!(
"project root must point to {}: {}",
MANIFEST_FILENAME,
path.display()
);
}
}
pub fn discover(path: impl AsRef<AbsPath>) -> io::Result<Vec<ProjectManifest>> {
Ok(read_dir(path.as_ref())?
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| {
path.is_file()
&& path
.file_name()
.map(|file_name| file_name == MANIFEST_FILENAME)
.unwrap_or(false)
})
.map(|path| ProjectManifest {
path: AbsPathBuf::try_from(path).expect(
"read_dir does not return absolute path when iterating an absolute path",
),
})
.collect())
}
pub fn discover_all(paths: impl Iterator<Item = impl AsRef<AbsPath>>) -> Vec<ProjectManifest> {
let mut project_manifests = paths
.filter_map(|path| ProjectManifest::discover(path).ok())
.flatten()
.collect::<FxHashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
project_manifests.sort();
project_manifests
}
}