Skip to main content

vvv_core/resolve/
project.rs

1use std::collections::BTreeSet;
2use std::path::{Path, PathBuf};
3
4use serde::{Deserialize, Serialize};
5
6use crate::facts::Facts;
7use crate::import::ImportRef;
8use crate::resolve::Packages;
9use crate::text::SourceText;
10
11/// The workspace as data: which files exist and which packages they belong
12/// to. What a [`Layout`](crate::Layout) and a [`Surgery`](crate::Surgery)
13/// see instead of a file system.
14#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
15#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
16pub struct Project {
17    pub packages: Packages,
18    pub files: FileSet,
19}
20
21/// Every file of the workspace, workspace-relative.
22#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
23#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
24#[serde(transparent)]
25pub struct FileSet(BTreeSet<PathBuf>);
26
27impl FileSet {
28    pub fn contains(&self, path: &Path) -> bool {
29        self.0.contains(path)
30    }
31
32    /// Whether any file lies under `dir`.
33    pub fn has_under(&self, dir: &Path) -> bool {
34        self.under(dir).next().is_some()
35    }
36
37    /// The files under `dir`, in path order.
38    pub fn under<'a>(&'a self, dir: &'a Path) -> impl Iterator<Item = &'a Path> + 'a {
39        self.0
40            .range(dir.to_path_buf()..)
41            .map(PathBuf::as_path)
42            .take_while(move |p| p.starts_with(dir))
43    }
44
45    pub fn iter(&self) -> impl Iterator<Item = &Path> {
46        self.0.iter().map(PathBuf::as_path)
47    }
48
49    pub fn len(&self) -> usize {
50        self.0.len()
51    }
52
53    pub fn is_empty(&self) -> bool {
54        self.0.is_empty()
55    }
56}
57
58impl<P: Into<PathBuf>> FromIterator<P> for FileSet {
59    fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self {
60        Self(iter.into_iter().map(Into::into).collect())
61    }
62}
63
64/// One file the engine has read and parsed, as a surgery sees it.
65#[derive(Debug, Clone, Copy)]
66pub struct Parsed<'a> {
67    pub path: &'a Path,
68    pub source: &'a SourceText,
69    pub facts: &'a Facts,
70}
71
72impl Parsed<'_> {
73    /// Whether `import` is the prefix of a grouped statement (`std::path` in
74    /// `use std::path::{Path, PathBuf}`) rather than an import of its own:
75    /// an entry of the same statement is grouped under its path.
76    pub fn is_group_prefix(&self, import: &ImportRef) -> bool {
77        self.facts.imports.iter().any(|other| {
78            other.group.as_ref().is_some_and(|group| {
79                group.statement.contains(&import.span) && group.prefix == import.path
80            })
81        })
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn under_is_a_prefix_range() {
91        let files: FileSet = [
92            "src/a.rs",
93            "src/a/b.rs",
94            "src/a/c.rs",
95            "src/ab.rs",
96            "src/b.rs",
97        ]
98        .into_iter()
99        .collect();
100        let dir = Path::new("src/a");
101        assert_eq!(
102            files.under(dir).collect::<Vec<_>>(),
103            [Path::new("src/a/b.rs"), Path::new("src/a/c.rs")]
104        );
105        assert!(files.has_under(dir) && !files.has_under(Path::new("src/c")));
106        assert!(files.contains(Path::new("src/ab.rs")));
107    }
108}