1use super::Host;
4use crate::error::{DiagCode, Diagnostic};
5use crate::span::Span;
6use crate::value::{Instant, Value};
7
8pub struct LocalHost {
10 pub staged: usize,
11 pub committed: usize,
12 pub published: Vec<String>,
13 pub query_rows: usize,
14 pub clock_time: Option<i64>,
15 pub monotonic_time: u64,
16 #[doc(hidden)]
17 pub tx_checkpoints: Vec<(usize, usize)>,
18}
19
20impl Default for LocalHost {
21 fn default() -> Self {
22 Self {
23 staged: 0,
24 committed: 0,
25 published: Vec::new(),
26 query_rows: 0,
27 clock_time: Some(1_000_000_000),
28 monotonic_time: 0,
29 tx_checkpoints: Vec::new(),
30 }
31 }
32}
33
34impl Host for LocalHost {
35 fn graph_query(
36 &mut self,
37 _args: &[Value],
38 take: u64,
39 _span: Span,
40 ) -> Result<Value, Diagnostic> {
41 let n = self.query_rows.min(take as usize);
42 Ok(Value::List(vec![Value::I64(1); n]))
43 }
44
45 fn graph_stage(&mut self, _term: &Value, _span: Span) -> Result<Value, Diagnostic> {
46 self.staged += 1;
47 Ok(Value::Null)
48 }
49
50 fn graph_commit(&mut self, _span: Span) -> Result<Value, Diagnostic> {
51 self.committed += 1;
52 Ok(Value::Receipt)
53 }
54
55 fn graph_begin(&mut self, _span: Span) -> Result<(), Diagnostic> {
56 self.tx_checkpoints.push((self.staged, self.committed));
57 Ok(())
58 }
59
60 fn graph_abort(&mut self, _span: Span) -> Result<(), Diagnostic> {
61 if let Some((saved_staged, _saved_committed)) = self.tx_checkpoints.pop() {
62 self.staged = saved_staged;
63 }
64 Ok(())
65 }
66
67 fn graph_end(&mut self, span: Span) -> Result<(), Diagnostic> {
68 if let Some((saved_staged, saved_committed)) = self.tx_checkpoints.pop() {
69 if self.committed == saved_committed && self.staged > saved_staged {
70 self.graph_commit(span)?;
71 }
72 }
73 Ok(())
74 }
75
76 fn aura_validate(
77 &mut self,
78 node: &Value,
79 shape: &Value,
80 _span: Span,
81 ) -> Result<Value, Diagnostic> {
82 if matches!(node, Value::Null) || matches!(shape, Value::Null) {
83 return Ok(Value::Bool(false));
84 }
85 if let Value::Record(shape_rec) = shape {
86 if let Some(Value::List(required)) = shape_rec.get("required") {
87 let Value::Record(node_rec) = node else {
88 return Ok(Value::Bool(required.is_empty()));
89 };
90 for key in required {
91 let Some(key_name) = key.as_str() else {
92 return Ok(Value::Bool(false));
93 };
94 match node_rec.get(key_name) {
95 None | Some(Value::Null) => return Ok(Value::Bool(false)),
96 Some(_) => {}
97 }
98 }
99 }
100 if let Some(Value::Record(expected_fields)) = shape_rec.get("fields") {
101 let Value::Record(node_rec) = node else {
102 return Ok(Value::Bool(expected_fields.is_empty()));
103 };
104 for key in expected_fields.keys() {
105 match node_rec.get(key) {
106 None | Some(Value::Null) => return Ok(Value::Bool(false)),
107 Some(_) => {}
108 }
109 }
110 }
111 }
112 Ok(Value::Bool(true))
113 }
114
115 fn pulse_publish(
116 &mut self,
117 topic: &str,
118 _payload: &Value,
119 _span: Span,
120 ) -> Result<Value, Diagnostic> {
121 self.published.push(topic.to_string());
122 Ok(Value::Null)
123 }
124
125 fn time_unix(&mut self, span: Span) -> Result<Value, Diagnostic> {
127 match self.clock_time {
128 Some(t) => Ok(Value::I64(t)),
129 None => Err(Diagnostic::new(
130 DiagCode::E702,
131 span,
132 "no clock available on this local host",
133 )),
134 }
135 }
136
137 fn time_now(&mut self, span: Span) -> Result<Value, Diagnostic> {
139 match self.clock_time {
140 Some(secs) => Ok(Value::Instant(Instant::unix(secs, 500_000_000))),
141 None => Err(Diagnostic::new(
142 DiagCode::E702,
143 span,
144 "no clock available on this local host",
145 )),
146 }
147 }
148
149 fn time_unix_nanos(&mut self, span: Span) -> Result<Value, Diagnostic> {
150 match self.clock_time {
151 Some(t) => {
152 let mut rec = std::collections::BTreeMap::new();
153 rec.insert("secs".into(), Value::I64(t));
154 rec.insert("nanos".into(), Value::U64(500_000));
155 Ok(Value::Record(rec))
156 }
157 None => Err(Diagnostic::new(
158 DiagCode::E702,
159 span,
160 "no clock available on this local host",
161 )),
162 }
163 }
164
165 fn time_monotonic_nanos(&mut self, _span: Span) -> Result<Value, Diagnostic> {
166 Ok(Value::U64(self.monotonic_time))
167 }
168
169 fn capability_invoke(
170 &mut self,
171 id: &str,
172 args: &Value,
173 span: Span,
174 ) -> Result<Value, Diagnostic> {
175 crate::catalog::invoke_local(id, args, span)
176 }
177
178 fn available_acceleration(&self) -> crate::AccelerationTier {
179 crate::detect_available_tier()
180 }
181}