specfold_core/check/
scope.rs1use crate::corpus::Corpus;
5use crate::graph::Graph;
6use crate::workspace::vcs;
7use anyhow::{Result, anyhow};
8use std::collections::BTreeSet;
9
10#[derive(Debug, Clone)]
11pub enum Range {
12 All,
14 Staged,
16 Changed(String),
18}
19
20#[derive(Debug, Clone)]
21pub struct Scope {
22 pub range: Range,
23 pub paths: BTreeSet<String>,
25 pub in_submodules: BTreeSet<String>,
28 pub touched: BTreeSet<usize>,
30 pub with_neighbours: BTreeSet<usize>,
32 pub base: Option<String>,
34}
35
36impl Scope {
37 pub fn build(c: &Corpus, g: &Graph, range: Range) -> Result<Self> {
38 let root = &c.cfg.root;
39 let (mut paths, in_submodules, base): (BTreeSet<String>, BTreeSet<String>, _) = match &range {
40 Range::All => (BTreeSet::new(), BTreeSet::new(), None),
41 Range::Staged => {
42 let p = vcs::staged(root).ok_or_else(|| anyhow!("--staged needs a git repository"))?;
43 let sub = vcs::submodule_changes(root, vcs::Side::Staged);
44 (p.into_iter().collect(), sub.into_iter().collect(), Some("HEAD".to_string()))
45 }
46 Range::Changed(base) => {
47 let p = vcs::changed_since(root, base).ok_or_else(|| anyhow!("cannot diff against {base}"))?;
48 let sub = vcs::submodule_changes(root, vcs::Side::Since(base));
49 (p.into_iter().collect(), sub.into_iter().collect(), Some(base.clone()))
50 }
51 };
52 paths.extend(in_submodules.iter().cloned());
53 let mut touched = BTreeSet::new();
54 if !matches!(range, Range::All) {
55 for (i, d) in c.docs.iter().enumerate() {
56 let own = std::iter::once(&d.path).chain(&d.chapters).chain(&d.bundle_files);
57 if own.into_iter().any(|p| paths.contains(p)) {
58 touched.insert(i);
59 }
60 }
61 }
62 let mut with_neighbours = touched.clone();
63 for &i in &touched {
64 for e in g.edges_to(i) {
65 with_neighbours.insert(e.from);
66 }
67 }
68 Ok(Scope { range, paths, in_submodules, touched, with_neighbours, base })
69 }
70
71 pub fn is_all(&self) -> bool {
72 matches!(self.range, Range::All)
73 }
74
75 pub fn covers(&self, i: usize) -> bool {
77 self.is_all() || self.touched.contains(&i)
78 }
79
80 pub fn covers_links(&self, i: usize) -> bool {
82 self.is_all() || self.with_neighbours.contains(&i)
83 }
84
85 pub fn covers_path(&self, p: &str) -> bool {
86 self.is_all() || self.paths.contains(p)
87 }
88}