Skip to main content

spec_drift/
context.rs

1use crate::domain::CodeFact;
2use std::path::{Path, PathBuf};
3
4/// Everything an analyzer needs to answer questions about the project.
5///
6/// Owns parsed artifacts so each file is parsed at most once per run.
7#[derive(Debug, Default)]
8pub struct ProjectContext {
9    /// Workspace/root directory used for config, suppression, blame, and
10    /// workspace-relative reporting.
11    pub root: PathBuf,
12    /// Directory used for package-local cargo operations. Usually the same as
13    /// `root`, but set to a workspace member root when `--package` is used.
14    pub analysis_root: PathBuf,
15    pub rust_files: Vec<PathBuf>,
16    pub markdown_files: Vec<PathBuf>,
17    pub yaml_files: Vec<PathBuf>,
18    pub makefile_files: Vec<PathBuf>,
19    pub code_facts: Vec<CodeFact>,
20}
21
22impl ProjectContext {
23    pub fn new(root: impl Into<PathBuf>) -> Self {
24        let root = root.into();
25        Self {
26            analysis_root: root.clone(),
27            root,
28            ..Self::default()
29        }
30    }
31
32    pub fn facts_named<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a CodeFact> + 'a {
33        self.code_facts.iter().filter(move |f| f.name == name)
34    }
35
36    pub fn rel<'a>(&self, path: &'a Path) -> &'a Path {
37        path.strip_prefix(&self.analysis_root)
38            .or_else(|_| path.strip_prefix(&self.root))
39            .unwrap_or(path)
40    }
41}