relate_executor/pattern_engine.rs
1//! The diagnostic pattern-engine switch (ADR-0519): the typed value the
2//! kernels' routing consults, never re-derived at a rung. Resolved once
3//! at `Database::open` from `RELATE_PATTERN_ENGINE` (the
4//! `RELATE_PARSER` precedent's resolution site and precedence,
5//! ADR-0277's history) and installed on the store-level service bundle
6//! (`Graph::configure_pattern_engine` in `graph/services`); the rungs
7//! read it back through `Graph::pattern_engine`.
8
9use std::str::FromStr;
10
11use relate_core::{Error, Result};
12
13/// Which pattern engine serves a clause (ADR-0519): a diagnostic-only
14/// switch for the pattern-engine spike — never Cypher syntax, no
15/// query-level spelling, and no stability promise. It dies with the old
16/// engines it switches between.
17///
18/// Read once at `Database::open` from `RELATE_PATTERN_ENGINE`:
19/// unset, blank, and `auto` are [`PatternEngine::Auto`] (today's routing
20/// exactly); `nfa` forces the frontier family's rung (ADR-0478) onto
21/// every frontier-compilable clause, bypassing that rung's cost gate —
22/// every structural, answer-shape, allocation and budget proof stands,
23/// and a decline still fails through; `walk` stands the traversal
24/// kernels down (the `with_kernels_disabled` differential behavior,
25/// env-reachable), so the batch scan-filter chain and the per-row walker
26/// serve every clause.
27#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
28pub enum PatternEngine {
29 /// Today's routing exactly: every rung's own proofs and cost gates
30 /// decide, unchanged.
31 #[default]
32 Auto,
33 /// Force the frontier family's rung for every frontier-compilable
34 /// clause (the cost gate `frontier_match::frontier_engages` is
35 /// bypassed); the batch/chain-kernel rungs stand down for the
36 /// clauses the frontier takes — they sit below it on both operator
37 /// ladders and decline the quantified shapes structurally.
38 Nfa,
39 /// Stand the traversal kernels down: the chain/hub-star kernels
40 /// (ADR-0473) and the frontier family (ADR-0478) decline every
41 /// clause, and the batch scan-filter chain plus the per-row walker
42 /// serve — the differential suites' `with_kernels_disabled`
43 /// behavior, reachable from a real process.
44 Walk,
45}
46
47impl PatternEngine {
48 /// Every rung, in the vocabulary's order — the parser's iteration
49 /// source, so the accepted names derive from [`Self::as_str`] instead
50 /// of a second hand-maintained match. Crate-private: the only readers
51 /// are this module's `from_str` and `expected_names`, so the table is
52 /// not part of the re-exported type's surface.
53 pub(crate) const ALL: [PatternEngine; 3] =
54 [PatternEngine::Auto, PatternEngine::Nfa, PatternEngine::Walk];
55
56 /// The rung's stable wire name — the same three values the env value
57 /// parses (`auto`, `nfa`, `walk`), single-sourced here for the plan
58 /// args.
59 pub const fn as_str(self) -> &'static str {
60 match self {
61 PatternEngine::Auto => "auto",
62 PatternEngine::Nfa => "nfa",
63 PatternEngine::Walk => "walk",
64 }
65 }
66
67 /// The authored rejection's expected-names clause, built from
68 /// [`Self::ALL`] so a renamed rung reaches the message too.
69 fn expected_names() -> String {
70 let names = Self::ALL.map(Self::as_str);
71 let (last, rest) = names.split_last().expect("three pattern engines");
72 format!("{}, or {last}", rest.join(", "))
73 }
74}
75
76impl FromStr for PatternEngine {
77 type Err = Error;
78
79 fn from_str(s: &str) -> Result<Self> {
80 let lower = s.trim().to_ascii_lowercase();
81 Self::ALL
82 .into_iter()
83 .find(|engine| engine.as_str() == lower)
84 .ok_or_else(|| {
85 Error::semantic(format!(
86 "invalid pattern engine {lower:?}; expected {}",
87 Self::expected_names()
88 ))
89 })
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 /// The value vocabulary, one parse each: the three valid values
98 /// (case-insensitively, the `DurabilityMode` grammar) and the
99 /// unknown value's authored rejection naming all three.
100 #[test]
101 fn parses_the_three_values_and_rejects_the_rest() {
102 assert_eq!(
103 "auto".parse::<PatternEngine>().unwrap(),
104 PatternEngine::Auto
105 );
106 assert_eq!("nfa".parse::<PatternEngine>().unwrap(), PatternEngine::Nfa);
107 assert_eq!(
108 "walk".parse::<PatternEngine>().unwrap(),
109 PatternEngine::Walk
110 );
111 assert_eq!(
112 " NFA ".parse::<PatternEngine>().unwrap(),
113 PatternEngine::Nfa,
114 "trimmed, case-insensitive"
115 );
116 let err = "dfs".parse::<PatternEngine>().unwrap_err();
117 let display = err.to_string();
118 assert!(display.contains("invalid pattern engine"), "{display}");
119 assert!(display.contains("expected auto, nfa, or walk"), "{display}");
120 }
121}