datacraft-core 0.1.2

Headless domain model, command bus and application state for DataCraft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Command dispatch core (step 4.1, item H13; ARCHITECTURE §5).
//!
//! `Core::dispatch` is a total reducer: every command produces a
//! deterministic event list, failures become `Event::Failed` with precise
//! reasons. Storage I/O happens only behind the [`StoragePort`](crate::port)
//! — the reducer itself is pure over [`AppState`].

use crate::command::Command;
use crate::event::Event;
use crate::failure::FailureCode;
use crate::port::{PortError, StoragePort};
use crate::snapshot::UiStateSnapshot;
use crate::state::AppState;

mod relationships;
mod rename;
mod table_view_state;
mod tabs;
mod views;

/// The headless application core: reducer + state + storage port.
pub struct Core {
    pub(crate) state: AppState,
    pub(crate) port: Box<dyn StoragePort>,
}

impl Core {
    /// Creates a core over the given storage backend.
    pub fn new(port: Box<dyn StoragePort>) -> Self {
        let mut state = AppState::new();
        // The composition's driver inventory (M23) is fixed at
        // construction — the registry never mutates at runtime.
        state.drivers = port.driver_inventory();
        Self { state, port }
    }

    /// Read access to the application state (the UI re-renders from it).
    pub fn state(&self) -> &AppState {
        &self.state
    }

    /// Serializable snapshot of everything the UI shows — the headless
    /// test seam (ARCHITECTURE §5).
    pub fn inspect(&self) -> UiStateSnapshot {
        UiStateSnapshot::of(&self.state)
    }

    /// Executes a query (in-memory predicates over port rows, decision
    /// default 7); errors are precise strings for headless rendering.
    pub fn run_query(&self, name: &str) -> Result<crate::query_engine::QueryResult, String> {
        let Some(db) = &self.state.database else {
            return Err("no database is open".to_owned());
        };
        let Some(def) = db
            .queries
            .iter()
            .find(|q| q.name.eq_ignore_ascii_case(name))
            .cloned()
        else {
            let native = db
                .native_queries
                .iter()
                .find(|query| query.name.eq_ignore_ascii_case(name))
                .ok_or_else(|| format!("query {name:?} not found"))?;
            // Native queries execute through the qualified dialect
            // (step 27.1, M12): the decoded expression dialect runs on the
            // phase-15 expression service; anything outside it answers a
            // precise `Unsupported`.
            let mut sources = Vec::with_capacity(native.tables.len());
            for table in &native.tables {
                let Some(source) = db
                    .tables
                    .iter()
                    .find(|candidate| candidate.name.eq_ignore_ascii_case(table))
                else {
                    return Err(format!(
                        "native query {:?}: table {table:?} not found",
                        native.name
                    ));
                };
                let rows = self
                    .port
                    .read_rows(
                        &source.name,
                        crate::port::RowRange {
                            start: 0,
                            len: usize::MAX,
                        },
                    )
                    .map_err(|e| e.to_string())?;
                sources.push((source, rows));
            }
            return crate::native_exec::run(native, &sources).map_err(|e| e.to_string());
        };
        let Some(source) = db
            .tables
            .iter()
            .find(|t| t.name.eq_ignore_ascii_case(&def.table))
        else {
            return Err(format!("source table {:?} not found", def.table));
        };
        let rows = self
            .port
            .read_rows(
                &source.name,
                crate::port::RowRange {
                    start: 0,
                    len: usize::MAX,
                },
            )
            .map_err(|e| e.to_string())?;
        crate::query_engine::run_query(&def, source, &rows).map_err(|e| e.to_string())
    }

    /// A native saved query by name (step 15.3, M3) — the read-only
    /// consumer seam (bus/MCP/UI) for queries the sidecar does not own.
    pub fn native_query(&self, name: &str) -> Option<&crate::port::NativeQuery> {
        self.state
            .database
            .as_ref()?
            .native_queries
            .iter()
            .find(|query| query.name.eq_ignore_ascii_case(name))
    }

    /// The form's own filter criteria, if one is applied (phase 64, M71;
    /// ADR-8). `None` means every source row is visible.
    pub fn form_filter(&self, form: &str) -> Option<&str> {
        self.state
            .form_filters
            .iter()
            .find(|(name, _)| name.eq_ignore_ascii_case(form))
            .map(|(_, criteria)| criteria.as_str())
    }

    /// Row indices of a form's filtered record set, evaluated through the
    /// expression service (Access wildcards included). No filter means
    /// every row; an evaluation failure is precise and local.
    pub fn form_filter_rows(
        &self,
        form: &str,
        rows: &[Vec<crate::schema::FieldValue>],
    ) -> Result<Vec<usize>, String> {
        let Some(criteria) = self.form_filter(form) else {
            return Ok((0..rows.len()).collect());
        };
        let criteria = criteria.to_owned();
        let Some(db) = self.state.database.as_ref() else {
            return Err("no database is open".to_owned());
        };
        let Some(form_def) = db.forms.iter().find(|f| f.name.eq_ignore_ascii_case(form)) else {
            return Err(format!("form {form:?} not found"));
        };
        let Some(table) = db
            .tables
            .iter()
            .find(|table| table.name.eq_ignore_ascii_case(&form_def.source))
        else {
            return Err(format!("source table {:?} not found", form_def.source));
        };
        let expression = crate::expr::parse(&criteria)
            .map_err(|error| format!("filter {criteria:?}: {error}"))?;
        let mut indices = Vec::new();
        for (index, row) in rows.iter().enumerate() {
            let mut scope = crate::expr::Scope::new();
            for (field, value) in table.fields.iter().zip(row) {
                scope.set(field.name.clone(), value.clone());
            }
            match expression.evaluate(&scope) {
                Ok(crate::schema::FieldValue::Boolean(true)) => indices.push(index),
                Ok(crate::schema::FieldValue::Boolean(false) | crate::schema::FieldValue::Null) => {
                }
                Ok(other) => {
                    return Err(format!(
                        "filter {criteria:?} evaluated to {other:?}, not a logical value"
                    ));
                }
                Err(error) => return Err(format!("filter {criteria:?}: {error}")),
            }
        }
        Ok(indices)
    }

    /// Runs one gated SQL statement over the loaded tables in a throwaway
    /// in-memory database (step 13.3, H37) — the data seam of the SQL
    /// window. Sources are read through the port and never written.
    #[cfg(feature = "sql-engine")]
    pub fn run_sql(&self, sql: &str) -> Result<crate::sql_types::SqlResult, String> {
        let Some(db) = &self.state.database else {
            return Err("no database is open".to_owned());
        };
        let mut sources = Vec::with_capacity(db.tables.len());
        for table in &db.tables {
            let rows = self
                .port
                .read_rows(
                    &table.name,
                    crate::port::RowRange {
                        start: 0,
                        len: usize::MAX,
                    },
                )
                .map_err(|e| format!("materializing {:?} failed: {e}", table.name))?;
            sources.push((table.clone(), rows));
        }
        crate::sql_engine::run_sql(sql, &sources)
    }

    /// Without the `sql-engine` feature the engine is not compiled in; the
    /// command still dispatches and answers a precise failure (step 31.2).
    #[cfg(not(feature = "sql-engine"))]
    pub fn run_sql(&self, _sql: &str) -> Result<crate::sql_types::SqlResult, String> {
        Err(
            "the SQL engine is not part of this build (enable the datacraft-core `sql-engine` feature)"
                .to_owned(),
        )
    }

    /// Result of the last successful `RunSql` (the UI's data source).
    pub fn sql_result(&self) -> Option<&crate::sql_types::SqlResult> {
        self.state.sql_result.as_ref()
    }

    fn run_sql_command(&mut self, sql: &str) -> Vec<Event> {
        match self.run_sql(sql) {
            Ok(result) => {
                let rows = result.rows.len();
                self.state.sql_result = Some(result);
                vec![Event::SqlRan { rows }]
            }
            Err(reason) => {
                self.state.sql_result = None;
                failed(FailureCode::Parse, reason)
            }
        }
    }

    /// Reads a window of rows through the port (the datasheet's data
    /// source; the port owns the backend mapping).
    pub fn read_rows(
        &self,
        table: &str,
        range: crate::port::RowRange,
    ) -> Result<Vec<Vec<crate::schema::FieldValue>>, PortError> {
        self.port.read_rows(table, range)
    }

    /// The open database's table definition (the datasheet's new-record
    /// row and the designers need domain kinds, not the projection).
    pub fn table_definition(&self, table: &str) -> Option<&crate::schema::TableDef> {
        self.state
            .database
            .as_ref()?
            .tables
            .iter()
            .find(|candidate| candidate.name.eq_ignore_ascii_case(table))
    }

    /// Applies one command; returns the observable outcomes in order.
    pub fn dispatch(&mut self, command: Command) -> Vec<Event> {
        let clears_history = clears_history(&command);
        let events = match command {
            Command::ToggleNavPane => {
                self.state.nav_pane_visible = !self.state.nav_pane_visible;
                vec![Event::NavPaneToggled {
                    visible: self.state.nav_pane_visible,
                }]
            }
            Command::CreateRelationship { relationship } => self.create_relationship(relationship),
            Command::DeleteRelationship { relationship } => self.delete_relationship(relationship),
            Command::ToggleRelationshipMapper => {
                self.state.relationship_mapper_open = !self.state.relationship_mapper_open;
                vec![Event::RelationshipMapperToggled {
                    open: self.state.relationship_mapper_open,
                }]
            }
            Command::ToggleSqlWindow => {
                self.state.sql_window_open = !self.state.sql_window_open;
                vec![Event::SqlWindowToggled {
                    open: self.state.sql_window_open,
                }]
            }
            Command::ToggleImportWindow => {
                self.state.import_window_open = !self.state.import_window_open;
                Vec::new()
            }
            Command::PreviewImportFile { source } => self.preview_import_file(&source),
            Command::ToggleExpressionWindow => {
                self.state.expression_window_open = !self.state.expression_window_open;
                vec![Event::ExpressionWindowToggled {
                    open: self.state.expression_window_open,
                }]
            }
            Command::RunSql { sql } => self.run_sql_command(&sql),
            Command::RunExpression {
                context,
                expression,
            } => self.run_expression_command(context, &expression),
            Command::CreateQuery { name } => self.create_query(&name),
            Command::View(action) => self.view_command(action),
            Command::SetQbeCell {
                query,
                row,
                col,
                value,
            } => self.set_qbe_cell(&query, row, col, value),
            Command::ClearQbeCriteria { query } => self.clear_qbe_criteria(&query),
            Command::SetTableSort {
                table,
                field,
                order,
            } => self.set_table_sort(&table, &field, order),
            Command::SetTableFilter {
                table,
                field,
                criteria,
            } => self.set_table_filter(&table, &field, criteria.as_deref()),
            Command::ClearTableFilters { table } => self.clear_table_filters(&table),
            Command::DeleteQuery { name } => self.delete_query(&name),
            Command::RunQuery { query } => self.run_query_command(&query),
            Command::CreateForm { name } => self.create_form(&name),
            Command::DeleteForm { name } => self.delete_form(&name),
            Command::AddFormControl { form, control } => self.add_control(&form, control),
            Command::SetFormControlContent {
                form,
                control,
                value,
            } => self.set_control_content(&form, &control, &value),
            Command::SetFormControlData(data) => self.set_control_data(&data),
            Command::SetFormFilter(filter) => self.set_form_filter(&filter),
            Command::SetFormControlRect {
                form,
                control,
                rect,
            } => self.set_control_rect(&form, &control, rect),
            Command::DeleteFormControl { form, control } => self.delete_control(&form, &control),
            Command::AddFormField { form, field } => self.add_form_field(&form, &field),
            Command::SetFormSource { form, table } => self.set_form_source(&form, &table),
            Command::CreateReport { name } => self.create_report(&name),
            Command::DeleteReport { name } => self.delete_report(&name),
            Command::AddReportControl { report, control } => {
                self.add_report_control(&report, control)
            }
            Command::SetReportControlContent {
                report,
                control,
                value,
            } => self.set_report_control_content(&report, &control, &value),
            Command::SetReportControlRect {
                report,
                control,
                rect,
            } => self.set_report_control_rect(&report, &control, rect),
            Command::DeleteReportControl { report, control } => {
                self.delete_report_control(&report, &control)
            }
            Command::AddReportField { report, field } => self.add_report_field(&report, &field),
            Command::SetReportSource { report, table } => self.set_report_source(&report, &table),
            Command::SetReportGrouping { report, group_by } => {
                self.set_report_grouping(&report, group_by)
            }
            Command::LoadVbaModule { module } => self.load_vba_module(&module),
            Command::AnalyzeVba => self.analyze_vba_command(),
            Command::SaveDesignDocument { path } => self.save_design_document(&path),
            Command::LoadDesignDocument { path } => self.load_design_document(&path),
            Command::OpenDatabase {
                path,
                password,
                source,
            } => self.open_database(path, password, source),
            Command::CloseDatabase => self.close_database(),
            Command::CreateDatabase { path, format } => self.create_database(path, format),
            Command::CreateDatabaseFromTemplate { path, template } => {
                self.create_database_from_template(path, template)
            }
            Command::SaveDatabaseAs { path, password } => self.save_database_as(path, password),
            Command::ChangePassword { new_password } => self.change_password(new_password),
            Command::CreateTable { definition } => self.create_table(definition),
            Command::ImportFile { source, table } => self.import_file(&source, &table),
            Command::DropTable { table } => self.drop_table(&table),
            Command::AddField { table, field } => {
                self.alter_table(&table, crate::port::SchemaChange::AddField { field })
            }
            Command::DropField { table, field } => {
                self.alter_table(&table, crate::port::SchemaChange::DropField { field })
            }
            Command::RenameField {
                table,
                field,
                new_name,
            } => self.alter_table(
                &table,
                crate::port::SchemaChange::RenameField { field, new_name },
            ),
            Command::SetFieldProperties {
                table,
                field,
                properties,
            } => self.alter_table(
                &table,
                crate::port::SchemaChange::SetFieldProperties { field, properties },
            ),
            Command::InsertRow { table, values } => self.insert_row(&table, &values),
            Command::UpdateRow { table, row, values } => self.update_row(&table, row, &values),
            Command::DeleteRow { table, row } => self.delete_row(&table, row),
            Command::RenameObject { object, new_name } => self.rename_object(&object, &new_name),
            Command::PreviewWrite { table, ops } => self.preview_write(&table, ops),
            Command::ApplyWrite { token } => self.apply_write(token),
            Command::RollbackWrite { token } => self.rollback_write(token),
            Command::DiscardWrite { token } => self.discard_write(token),
            Command::OpenObject(object) => self.open_object(object, None),
            Command::OpenObjectInMode { object, mode } => self.open_object(object, Some(mode)),
            Command::CloseTab { tab_id } => self.close_tab(tab_id),
            Command::CloseOtherTabs { tab_id } => self.close_other_tabs(tab_id),
            Command::CloseAllTabs => self.close_all_tabs(),
            Command::SetActiveTab { tab_id } => self.set_active_tab(tab_id),
            Command::SetTabMode { tab_id, mode } => self.set_tab_mode(tab_id, mode),
            Command::Undo => self.undo(),
            Command::Redo => self.redo(),
        };
        if clears_history {
            // A failed barrier command changed nothing: keep the history it
            // would otherwise discard (L15).
            let failed = events.iter().any(|event| {
                matches!(
                    event,
                    Event::Failed { .. }
                        | Event::PasswordRequired { .. }
                        | Event::PasswordRejected { .. }
                )
            });
            if !failed {
                self.clear_history();
            }
        }
        events
    }
}

/// Commands whose row effects are outside the row-image history: schema
/// changes, journal batches and file-level lifecycle (step 28.1, U14).
fn clears_history(command: &Command) -> bool {
    matches!(
        command,
        Command::OpenDatabase { .. }
            | Command::CloseDatabase
            | Command::CreateTable { .. }
            | Command::ImportFile { .. }
            | Command::DropTable { .. }
            | Command::AddField { .. }
            | Command::DropField { .. }
            | Command::RenameField { .. }
            | Command::SetFieldProperties { .. }
            | Command::ApplyWrite { .. }
            | Command::RollbackWrite { .. }
    ) || matches!(
        command,
        Command::RenameObject { object, .. } if object.kind == crate::ObjectKind::Table
    )
}

pub(crate) fn failed(code: crate::failure::FailureCode, reason: impl Into<String>) -> Vec<Event> {
    vec![Event::Failed {
        code,
        reason: reason.into(),
    }]
}