Skip to main content

specfold_core/check/
context.rs

1//! Everything a rule needs: the corpus, its graph, the scope of the run and the options.
2
3use super::scope::Scope;
4use crate::corpus::Corpus;
5use crate::graph::Graph;
6use crate::workspace::vcs;
7
8#[derive(Debug, Clone, Default)]
9pub struct Options {
10    /// Run sot `verify` and `generated_by` commands.
11    pub run_commands: bool,
12    /// Report broken citations in external roots (Claude memory, notes …).
13    pub external: bool,
14    /// Ask stores whether attachments exist (network, slow).
15    pub online: bool,
16    /// Run guards over the whole tree in a full run; staged and ranged runs always run them.
17    pub guards: bool,
18    /// The spec commands this version has, for checking commands named in agent assets; empty
19    /// skips that check.
20    pub commands: Vec<String>,
21}
22
23pub struct Ctx<'a> {
24    pub corpus: &'a Corpus,
25    pub graph: &'a Graph,
26    pub scope: &'a Scope,
27    pub opts: &'a Options,
28}
29
30impl Ctx<'_> {
31    /// The content of a path at the base of the run (HEAD for staged runs).
32    pub fn previous(&self, path: &str) -> Option<String> {
33        let base = self.scope.base.as_deref()?;
34        vcs::show(&self.corpus.cfg.root, base, path)
35    }
36
37    /// The content being checked: the index for staged runs, the working tree otherwise.
38    pub fn current(&self, path: &str) -> Option<String> {
39        let staged = matches!(self.scope.range, super::scope::Range::Staged);
40        if staged && !self.scope.in_submodules.contains(path) {
41            return vcs::show_staged(&self.corpus.cfg.root, path);
42        }
43        crate::corpus::overlay::read(&self.corpus.cfg.root.join(path)).ok()
44    }
45}