specfold_core/check/
context.rs1use super::scope::Scope;
4use crate::corpus::Corpus;
5use crate::corpus::graph::Graph;
6use crate::workspace::vcs;
7
8#[derive(Debug, Clone, Default)]
9pub struct Options {
10 pub run_commands: bool,
12 pub external: bool,
14 pub online: bool,
16 pub guards: bool,
18 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 pub fn previous(&self, path: &str) -> Option<String> {
33 let base = self.scope.base.as_deref()?;
34 vcs::show(&self.corpus.cfg.spec_home, base, path)
35 }
36
37 pub fn current(&self, path: &str) -> Option<String> {
40 let staged = matches!(self.scope.range, super::scope::Range::Staged);
41 if staged {
42 let text = vcs::show_staged(&self.corpus.cfg.spec_home, path);
43 if text.is_some() || !self.scope.in_submodules.contains(path) {
44 return text;
45 }
46 }
47 crate::corpus::overlay::read(&self.corpus.cfg.spec_home.join(path)).ok()
48 }
49}
50
51pub fn key_line(ctx: &Ctx, path: &str, key: &str) -> usize {
53 let Some(text) = ctx.current(path) else { return 1 };
54 let prefix = format!("{key}:");
55 text.lines().position(|l| l.starts_with(&prefix)).map(|i| i + 1).unwrap_or(1)
56}