vantage_vista/source.rs
1use std::pin::Pin;
2
3use async_trait::async_trait;
4use ciborium::Value as CborValue;
5use futures_core::Stream;
6use indexmap::IndexMap;
7use vantage_core::{Result, VantageError, error};
8use vantage_types::Record;
9
10use crate::{
11 capabilities::VistaCapabilities,
12 column::Column,
13 reference::{ContainedSpec, Reference},
14 sort::SortDirection,
15 vista::Vista,
16};
17
18/// Per-driver executor for a `Vista`.
19///
20/// Implementations live in driver crates (vantage-sqlite, vantage-mongodb,
21/// vantage-aws, etc.). Each method receives `&Vista` so the driver can read
22/// the current condition state, columns, and other metadata.
23///
24/// `Id = String` and `Value = ciborium::Value` at this boundary, so every
25/// driver's native id (Mongo `ObjectId`, Surreal `Thing`, …) stringifies
26/// here. Methods are named with the `_vista_` infix to mirror
27/// `TableSource`'s `_table_` convention; `Vista`'s `ValueSet` impls
28/// delegate by stripping the infix.
29///
30/// `id: &String` (rather than `&str`) is intentional: the upstream
31/// `vantage_dataset::ValueSet` trait family fixes `Id = String` and uses
32/// `&Self::Id` in its signatures, so impls receive `&String` and forward
33/// it through unchanged.
34#[async_trait]
35#[allow(clippy::ptr_arg)]
36pub trait TableShell: Send + Sync + 'static {
37 // ---- Schema --------------------------------------------------------------
38 //
39 // The shell owns the schema. `Vista` is a thin wrapper that forwards its
40 // metadata accessors here. No defaults — every impl must answer (an empty
41 // schema is a deliberate choice the impl declares explicitly).
42
43 fn columns(&self) -> &IndexMap<String, Column>;
44
45 fn references(&self) -> &IndexMap<String, Reference>;
46
47 fn id_column(&self) -> Option<&str>;
48
49 // ---- ReadableValueSet delegates ----------------------------------------
50
51 async fn list_vista_values(&self, vista: &Vista)
52 -> Result<IndexMap<String, Record<CborValue>>>;
53
54 async fn get_vista_value(
55 &self,
56 vista: &Vista,
57 id: &String,
58 ) -> Result<Option<Record<CborValue>>>;
59
60 /// Fetch one record by id, with the caller's existing (cheap) record
61 /// available to drivers that can use it (e.g. a cmd detail script reading
62 /// list-pass columns). The default ignores `row` and delegates to
63 /// [`get_vista_value`](Self::get_vista_value); only drivers that benefit
64 /// override it.
65 async fn get_vista_value_with_row(
66 &self,
67 vista: &Vista,
68 id: &String,
69 _row: &Record<CborValue>,
70 ) -> Result<Option<Record<CborValue>>> {
71 self.get_vista_value(vista, id).await
72 }
73
74 async fn get_vista_some_value(
75 &self,
76 vista: &Vista,
77 ) -> Result<Option<(String, Record<CborValue>)>>;
78
79 /// Default implementation wraps `list_vista_values`. Drivers with native
80 /// streaming (cursor-based queries, paginated REST APIs) override.
81 #[allow(clippy::type_complexity)]
82 fn stream_vista_values<'a>(
83 &'a self,
84 vista: &'a Vista,
85 ) -> Pin<Box<dyn Stream<Item = Result<(String, Record<CborValue>)>> + Send + 'a>>
86 where
87 Self: Sync,
88 {
89 Box::pin(async_stream::stream! {
90 match self.list_vista_values(vista).await {
91 Ok(map) => {
92 for item in map {
93 yield Ok(item);
94 }
95 }
96 Err(e) => yield Err(e),
97 }
98 })
99 }
100
101 // ---- WritableValueSet delegates ----------------------------------------
102 //
103 // Default impls return a typed VantageError via `default_error` — drivers
104 // override only what they actually support. The matching `VistaCapabilities`
105 // flag must be set to `true` for any method the driver implements; if the
106 // flag is `true` but the trait method falls through to the default,
107 // `default_error` produces an `Unimplemented`-kind error (placeholder
108 // detected). If the flag is `false`, it produces `Unsupported`. Both are
109 // emitted as tracing events at construction.
110
111 async fn insert_vista_value(
112 &self,
113 _vista: &Vista,
114 _id: &String,
115 _record: &Record<CborValue>,
116 ) -> Result<Record<CborValue>> {
117 Err(self.default_error("insert_vista_value", "can_insert"))
118 }
119
120 async fn replace_vista_value(
121 &self,
122 _vista: &Vista,
123 _id: &String,
124 _record: &Record<CborValue>,
125 ) -> Result<Record<CborValue>> {
126 Err(self.default_error("replace_vista_value", "can_update"))
127 }
128
129 async fn patch_vista_value(
130 &self,
131 _vista: &Vista,
132 _id: &String,
133 _partial: &Record<CborValue>,
134 ) -> Result<Record<CborValue>> {
135 Err(self.default_error("patch_vista_value", "can_update"))
136 }
137
138 async fn delete_vista_value(&self, _vista: &Vista, _id: &String) -> Result<()> {
139 Err(self.default_error("delete_vista_value", "can_delete"))
140 }
141
142 async fn delete_vista_all_values(&self, _vista: &Vista) -> Result<()> {
143 Err(self.default_error("delete_vista_all_values", "can_delete"))
144 }
145
146 // ---- InsertableValueSet delegate ---------------------------------------
147
148 async fn insert_vista_return_id_value(
149 &self,
150 _vista: &Vista,
151 _record: &Record<CborValue>,
152 ) -> Result<String> {
153 Err(self.default_error("insert_vista_return_id_value", "can_insert"))
154 }
155
156 // ---- Aggregates --------------------------------------------------------
157
158 /// Default impl falls back to `list_vista_values` — drivers with native
159 /// count (`SELECT COUNT(*)`, etc.) override.
160 async fn get_vista_count(&self, vista: &Vista) -> Result<i64> {
161 Ok(self.list_vista_values(vista).await?.len() as i64)
162 }
163
164 // ---- Conditions --------------------------------------------------------
165
166 /// Translate `field == value` into the driver's native condition type and
167 /// apply it to the wrapped table. The default impl returns `Unimplemented`
168 /// — every driver is expected to override.
169 ///
170 /// `value` is the universal CBOR carrier; the driver picks the appropriate
171 /// translation (e.g. `cbor_to_bson` for Mongo, `cbor → AnyCsvType` for CSV).
172 fn add_eq_condition(&mut self, _field: &str, _value: &CborValue) -> Result<()> {
173 Err(error!(
174 format!(
175 "add_eq_condition not implemented for '{}'",
176 std::any::type_name::<Self>()
177 ),
178 method = "add_eq_condition",
179 source_type = std::any::type_name::<Self>()
180 )
181 .is_unimplemented())
182 }
183
184 /// Push a driver-native condition into the wrapped table. The
185 /// caller boxes the condition as `dyn Any` and the driver
186 /// downcasts to its own `T::Condition`. Used by YAML-driven
187 /// relation traversal, where the factory constructs a
188 /// `DeferredFn`-bearing condition outside the value-set surface
189 /// (which only accepts scalar eq) and pushes it through this
190 /// channel. Default is `Unimplemented`.
191 fn add_raw_condition(
192 &mut self,
193 _condition: Box<dyn std::any::Any + Send + Sync>,
194 ) -> Result<()> {
195 Err(error!(
196 format!(
197 "add_raw_condition not implemented for '{}'",
198 std::any::type_name::<Self>()
199 ),
200 method = "add_raw_condition",
201 source_type = std::any::type_name::<Self>()
202 )
203 .is_unimplemented())
204 }
205
206 // ---- Pagination --------------------------------------------------------
207
208 /// Declare how many records constitute one page. Used by both
209 /// [`fetch_page`](Self::fetch_page) and [`fetch_next`](Self::fetch_next).
210 /// Default returns `default_error("set_page_size", "can_set_page_size")`.
211 fn set_page_size(&mut self, _size: usize) -> Result<()> {
212 Err(self.default_error("set_page_size", "can_set_page_size"))
213 }
214
215 /// Fetch a specific page (1-based) using offset-style pagination. The
216 /// per-page count comes from the most recent
217 /// [`set_page_size`](Self::set_page_size).
218 ///
219 /// Drivers without random-access pagination (DynamoDB, most token-paginated
220 /// REST APIs) leave the default in place, which produces `Unsupported`.
221 /// Callers should branch on `vista.capabilities().can_fetch_page` first.
222 async fn fetch_page(
223 &self,
224 _vista: &Vista,
225 _page: usize,
226 ) -> Result<Vec<(String, Record<CborValue>)>> {
227 Err(self.default_error("fetch_page", "can_fetch_page"))
228 }
229
230 /// Cursor-style chain fetch. Pass `None` on the first call; pass the
231 /// previous call's returned token on subsequent calls. Returned token is
232 /// `None` when the result set is exhausted.
233 ///
234 /// The token is **driver-private** — its shape is whatever the backend
235 /// finds convenient (DynamoDB `LastEvaluatedKey` as a CBOR map, REST
236 /// `nextToken` as `CborValue::Text`, offset-based as `CborValue::Integer`).
237 /// Consumers treat it as opaque and round-trip it back unchanged.
238 ///
239 /// Default returns `default_error("fetch_next", "can_fetch_next")`.
240 async fn fetch_next(
241 &self,
242 _vista: &Vista,
243 _token: Option<CborValue>,
244 ) -> Result<(Vec<(String, Record<CborValue>)>, Option<CborValue>)> {
245 Err(self.default_error("fetch_next", "can_fetch_next"))
246 }
247
248 // ---- Quicksearch -------------------------------------------------------
249
250 /// Apply a quicksearch filter — a single string the driver fans out across
251 /// the columns it considers searchable (typically those flagged
252 /// [`SEARCHABLE`](crate::flags::SEARCHABLE), but each driver decides).
253 ///
254 /// **Replace semantics**: calling `add_search` again wipes the previous
255 /// search filter before applying the new one. Default produces
256 /// `Unimplemented` (when `can_search: true`) or `Unsupported` (when
257 /// `can_search: false`).
258 fn add_search(&mut self, _text: &str) -> Result<()> {
259 Err(self.default_error("add_search", "can_search"))
260 }
261
262 /// Drop the search filter previously applied via
263 /// [`add_search`](Self::add_search). Default mirrors `add_search`.
264 fn clear_search(&mut self) -> Result<()> {
265 Err(self.default_error("clear_search", "can_search"))
266 }
267
268 // ---- Ordering ----------------------------------------------------------
269
270 /// Push a single ORDER BY clause onto the wrapped table.
271 ///
272 /// Vista's `add_order` is replace-semantics: the driver shell should clear
273 /// any previously-set order before pushing the new one. Default produces
274 /// `Unimplemented` (when `can_order: true`) or `Unsupported` (when
275 /// `can_order: false`).
276 fn add_order(&mut self, _field: &str, _dir: SortDirection) -> Result<()> {
277 Err(self.default_error("add_order", "can_order"))
278 }
279
280 /// Wipe every order clause. Default mirrors [`add_order`](Self::add_order).
281 fn clear_orders(&mut self) -> Result<()> {
282 Err(self.default_error("clear_orders", "can_order"))
283 }
284
285 // ---- References --------------------------------------------------------
286
287 /// Resolve a same-persistence relation using a known source row, returning
288 /// the related table as a new `Vista`.
289 ///
290 /// Drivers override by forwarding into the wrapped typed `Table`'s
291 /// `get_ref_from_row::<EmptyEntity>(relation, &native_row)` and then
292 /// wrapping the result back as a `Vista` through the driver's factory.
293 /// The default returns `Unimplemented`. Cross-persistence refs are
294 /// handled one layer up by `vantage-vista-factory`'s `VistaCatalog`,
295 /// never here.
296 fn get_ref(&self, relation: &str, _row: &Record<CborValue>) -> Result<Vista> {
297 Err(error!(
298 format!(
299 "get_ref not implemented for '{}'",
300 std::any::type_name::<Self>()
301 ),
302 method = "get_ref",
303 relation = relation,
304 source_type = std::any::type_name::<Self>()
305 )
306 .is_unimplemented())
307 }
308
309 /// Build the **bare** target of a same-persistence relation as a `Vista` —
310 /// the table a new related row would be inserted into, with no join
311 /// condition applied. Used by Vista's nested insert to reach a has-one /
312 /// has-many child's destination.
313 ///
314 /// Drivers override by forwarding into the wrapped typed `Table`'s
315 /// `get_ref_target::<EmptyEntity>(relation)` and wrapping the result back
316 /// through the driver's factory — the same path as [`get_ref`](Self::get_ref)
317 /// minus the row-derived condition. The default returns `Unimplemented`;
318 /// cross-persistence relations are rejected at the `Vista` layer before
319 /// this is reached.
320 fn get_ref_target(&self, relation: &str) -> Result<Vista> {
321 Err(error!(
322 format!(
323 "get_ref_target not implemented for '{}'",
324 std::any::type_name::<Self>()
325 ),
326 method = "get_ref_target",
327 relation = relation,
328 source_type = std::any::type_name::<Self>()
329 )
330 .is_unimplemented())
331 }
332
333 /// Contained (embedded-in-row) relations this shell exposes, keyed by name.
334 /// Default empty — only shells that model embedded objects/arrays override.
335 fn contained(&self) -> &IndexMap<String, ContainedSpec> {
336 static EMPTY: std::sync::OnceLock<IndexMap<String, ContainedSpec>> =
337 std::sync::OnceLock::new();
338 EMPTY.get_or_init(IndexMap::new)
339 }
340
341 /// Resolve a contained relation against a known parent `row`, returning the
342 /// embedded records as a sub-`Vista`. Writes to that sub-Vista patch the
343 /// host column of `row`'s record back through the shell. Default returns
344 /// `Unimplemented`; shells override to seed [`crate::build_contained_vista`]
345 /// with a writeback that patches the parent.
346 fn get_contained_ref(&self, relation: &str, _row: &Record<CborValue>) -> Result<Vista> {
347 Err(error!(
348 format!(
349 "get_contained_ref not implemented for '{}'",
350 std::any::type_name::<Self>()
351 ),
352 method = "get_contained_ref",
353 relation = relation,
354 source_type = std::any::type_name::<Self>()
355 )
356 .is_unimplemented())
357 }
358
359 /// Names + cardinalities of the shell's same-persistence references.
360 /// Derived from [`references`](Self::references) by default; impls
361 /// should rarely need to override.
362 fn get_ref_kinds(&self) -> Vec<(String, crate::reference::ReferenceKind)> {
363 self.references()
364 .iter()
365 .map(|(name, r)| (name.clone(), r.kind))
366 .collect()
367 }
368
369 // ---- Identity ----------------------------------------------------------
370
371 /// Short human label for the underlying driver (e.g. `"csv"`, `"sqlite"`,
372 /// `"postgres"`, `"mongodb"`). Used for diagnostics and CLI output.
373 /// Drivers should override; the default is a placeholder.
374 fn driver_name(&self) -> &'static str {
375 "unknown"
376 }
377
378 // ---- Scripting ---------------------------------------------------------
379
380 /// Contribute backend-specific vocabulary to a Rhai engine that
381 /// vantage-vista has already seeded with the conventional `Vista` verbs
382 /// (see the `rhai_conventional` module). Backends with an expression engine
383 /// (SurrealDB, SQL) override this to register `ident`/`==`/`fx`/graph
384 /// constructors plus a `with_condition(<backend expr>)` builder that routes
385 /// a boxed native condition through [`add_raw_condition`](Self::add_raw_condition).
386 ///
387 /// Default is a no-op: engine-less datasources (CSV/Mongo/REST) still get
388 /// the conventional verbs and only lose the vendor expression syntax —
389 /// graceful degradation, not all-or-nothing.
390 #[cfg(feature = "rhai")]
391 fn register_rhai_extensions(&self, _engine: &mut rhai::Engine) {}
392
393 // ---- Capability advertisement -----------------------------------------
394
395 fn capabilities(&self) -> &VistaCapabilities;
396
397 /// Look up a capability flag by name. Used by `default_error` to decide
398 /// between `Unsupported` and `Unimplemented`. Drivers don't normally
399 /// need to override this.
400 fn capability_flag(&self, name: &str) -> bool {
401 let caps = self.capabilities();
402 match name {
403 "can_count" => caps.can_count,
404 "can_insert" => caps.can_insert,
405 "can_update" => caps.can_update,
406 "can_delete" => caps.can_delete,
407 "can_subscribe" => caps.can_subscribe,
408 "can_invalidate" => caps.can_invalidate,
409 "can_order" => caps.can_order,
410 "can_search" => caps.can_search,
411 "can_set_page_size" => caps.can_set_page_size,
412 "can_fetch_page" => caps.can_fetch_page,
413 "can_fetch_next" => caps.can_fetch_next,
414 "can_traverse_to_record" => caps.can_traverse_to_record,
415 "can_traverse_to_set" => caps.can_traverse_to_set,
416 "can_build_ref_via_script" => caps.can_build_ref_via_script,
417 _ => false,
418 }
419 }
420
421 /// Build the standard error returned by default trait method impls.
422 ///
423 /// Picks the kind based on the capability flag: a `true` flag means the
424 /// driver advertised support but didn't override the method (placeholder
425 /// → `Unimplemented`); a `false` flag means the driver honestly doesn't
426 /// claim the op (caller should have checked → `Unsupported`).
427 ///
428 /// Both kinds emit a `tracing::error!` at construction with `method`,
429 /// `capability`, `source_type`, and `vista_name` as structured fields.
430 fn default_error(&self, method: &str, capability: &str) -> VantageError {
431 let source_type = std::any::type_name::<Self>();
432 if self.capability_flag(capability) {
433 error!(
434 format!(
435 "'{}' is advertised as VistaCapability for '{}' but implementation for '{}' is missing",
436 capability, source_type, method
437 ),
438 method = method,
439 capability = capability,
440 source_type = source_type
441 )
442 .is_unimplemented()
443 } else {
444 error!(
445 format!(
446 "'{}' is not supported by '{}'; '{}' refused",
447 capability, source_type, method
448 ),
449 method = method,
450 capability = capability,
451 source_type = source_type
452 )
453 .is_unsupported()
454 }
455 }
456}