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    pub root: PathBuf,
10    pub rust_files: Vec<PathBuf>,
11    pub markdown_files: Vec<PathBuf>,
12    pub yaml_files: Vec<PathBuf>,
13    pub makefile_files: Vec<PathBuf>,
14    pub code_facts: Vec<CodeFact>,
15}
16
17impl ProjectContext {
18    pub fn new(root: impl Into<PathBuf>) -> Self {
19        Self {
20            root: root.into(),
21            ..Self::default()
22        }
23    }
24
25    pub fn facts_named<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a CodeFact> + 'a {
26        self.code_facts.iter().filter(move |f| f.name == name)
27    }
28
29    pub fn rel<'a>(&self, path: &'a Path) -> &'a Path {
30        path.strip_prefix(&self.root).unwrap_or(path)
31    }
32}