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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use std::collections::HashSet;
use logisheets_base::{BlockId, SheetId, errors::BasicError};
use crate::{
Error,
block_manager::schema_manager::{
ctx::BlockSchemaCtx,
field_type::{AggFunc, FieldAggregate, FieldType, FieldWritePolicy},
manager::SchemaManager,
schema::{ColSchema, FieldEntry, RandomSchema, RowSchema, Schema, SchemaTrait},
},
edit_action::EditPayload,
};
/// Trim a template and collapse an empty / whitespace-only one to `None`, so
/// a caller sending `Some("")` means the same as sending nothing.
fn normalize_formula(input: Option<String>) -> Option<String> {
input.and_then(|s| {
let t = s.trim().to_string();
if t.is_empty() { None } else { Some(t) }
})
}
/// Normalize a `Vec<Option<String>>` of formula templates: trim whitespace,
/// collapse empty / whitespace-only strings to `None`. Used uniformly for
/// value / validation / editability formula columns.
fn normalize_formula_vec(input: Vec<Option<String>>) -> Vec<Option<String>> {
input
.into_iter()
.map(|f| {
f.and_then(|s| {
let t = s.trim().to_string();
if t.is_empty() { None } else { Some(t) }
})
})
.collect()
}
/// Apply a slice of (possibly-updated) rule values to a schema's existing
/// rule slots. When `incoming.is_empty()`, the existing rule is preserved
/// untouched — this is how callers say "don't change validation, I'm only
/// updating value_formulas". Otherwise, the incoming vec replaces all
/// existing values for that rule kind (per-field).
fn apply_rule_update(
incoming_empty: bool,
incoming: Vec<Option<String>>,
take_existing: impl Fn(usize) -> Option<String>,
field_count: usize,
) -> Vec<Option<String>> {
if incoming_empty {
(0..field_count).map(take_existing).collect()
} else {
incoming
}
}
/// Validate that every `#FIELD("X")` reference in a list of formula
/// templates points at a declared field. `kind` is used in error
/// messages ("value_formulas" / "validation_formulas" / ...).
fn validate_field_refs(
formulas: &[Option<String>],
declared_names: &std::collections::HashSet<String>,
kind: &str,
) -> Result<(), Error> {
for (i, formula_opt) in formulas.iter().enumerate() {
let Some(formula) = formula_opt else { continue };
let trimmed = formula.trim();
if trimmed.is_empty() {
continue;
}
let Some(placeholders) = logisheets_parser::Parser {}.scan_placeholders(trimmed) else {
continue;
};
for ph in placeholders {
// Only the field name is checked. The row key of a
// `#FIELD("f", "key")` is data, not schema: rows come and go,
// so a key that resolves today may not tomorrow, and vice
// versa. An unresolvable key surfaces as `#NAME?` at eval
// time rather than blocking the bind.
let logisheets_parser::Placeholder::FieldRef(name, _) = ph else {
continue;
};
if !declared_names.contains(&name) {
return Err(BasicError::InvalidFormula(format!(
"{}[{}] references unknown field {:?}",
kind, i, name
))
.into());
}
}
}
Ok(())
}
pub struct BlockSchemaExecutor {
pub manager: SchemaManager,
/// Blocks whose schema was (re)bound during this payload. The formula
/// executor turns these into `Vertex::BlockAll(sheet, block)` dirty
/// entries — id-keyed so that ref-name renames don't break dependency
/// tracking the way the old string-keyed `dirty_schemas` did.
pub dirty_blocks: HashSet<(SheetId, BlockId)>,
}
/// Field-formula templates are a row/column-schema feature; a random schema has
/// no field list to attach them to.
fn no_field_formulas(block_id: BlockId) -> Error {
Error::PayloadError(format!(
"UpsertFieldFormulas: block {block_id} uses RandomSchema, which does not support \
field-formula templates"
))
}
impl BlockSchemaExecutor {
pub fn new(manager: SchemaManager) -> Self {
Self {
manager,
dirty_blocks: HashSet::new(),
}
}
pub fn execute<C: BlockSchemaCtx>(
self,
ctx: &mut C,
payload: EditPayload,
) -> Result<(Self, bool), Error> {
match payload {
EditPayload::BindFormSchema(p) => {
let mut dirty_blocks = self.dirty_blocks;
let mut manager = self.manager;
let sheet_id = ctx
.fetch_sheet_id_by_index(p.sheet_idx)
.map_err(|l| BasicError::SheetIdxExceed(l))?;
let block_id = p.block_id;
// Validate template references *before* committing the schema:
// every #FIELD("X") in any rule template must name a field
// actually declared in this bind. (#KEY is always valid;
// #PLACEHOLDER is allowed in validation / editability but not
// in value_formula — leaving it untouched there surfaces as
// #NAME?.)
let declared_names: HashSet<String> =
p.fields.iter().map(|f| f.name.clone()).collect();
validate_field_refs(
&p.fields
.iter()
.map(|f| f.value_formula.clone())
.collect::<Vec<_>>(),
&declared_names,
"value_formula",
)?;
validate_field_refs(
&p.fields
.iter()
.map(|f| f.validation_formula.clone())
.collect::<Vec<_>>(),
&declared_names,
"validation_formula",
)?;
validate_field_refs(
&p.fields
.iter()
.map(|f| f.editability_formula.clone())
.collect::<Vec<_>>(),
&declared_names,
"editability_formula",
)?;
let mut fields = Vec::new();
for (i, spec) in p.fields.into_iter().enumerate() {
let idx = i + p.field_from;
// RowSchema (p.row=true) stores ColId per field — fields
// run along columns, records along rows. ColSchema flips
// both. We fetch from the *records* dimension at index 0
// and take the *fields* dimension's id at idx, so the
// bind only needs the block to extend `idx` cells along
// the fields axis. Existing buggy behavior swapped these
// and accidentally worked for square blocks because
// RowId and ColId share the u32 representation.
let id = if p.row {
ctx.fetch_block_cell_id(&sheet_id, &block_id, 0, idx)?.col
} else {
ctx.fetch_block_cell_id(&sheet_id, &block_id, idx, 0)?.row
};
let field_type = spec
.field_type
.map(FieldType::from)
.unwrap_or(FieldType::Unspecified);
let entry = FieldEntry::new(id, spec.render_id)
.with_value_formula(normalize_formula(spec.value_formula))
.with_validation_formula(normalize_formula(spec.validation_formula))
.with_editability_formula(normalize_formula(spec.editability_formula))
.with_field_type(field_type)
.with_description(normalize_formula(spec.description))
.with_required(spec.required.unwrap_or(false))
.with_unique(spec.unique.unwrap_or(false))
.with_default_value(spec.default_value)
.with_write_policy(FieldWritePolicy::from_str(spec.write_policy.as_deref()))
// Both halves or neither. A function with no field to
// aggregate, or a field with no function, is not a
// declaration — and a function this build does not know
// yields no aggregate rather than a guess.
.with_pivot_column(
crate::block_manager::schema_manager::field_type::PivotColumn::from_parts(
spec.pivot_col_value.as_deref(),
spec.pivot_measure.as_deref(),
spec.pivot_func.as_deref(),
),
)
.with_aggregate(match (spec.agg_func.as_deref(), spec.agg_field) {
(Some(func), Some(source_field)) => AggFunc::from_str(func)
.map(|func| FieldAggregate { func, source_field }),
_ => None,
});
fields.push((spec.name, entry));
}
// The header arrives as an INDEX along the record axis and is
// stored as that line's stable ID: an index would be wrong the
// moment a line is inserted above it, and the id is also what
// lets `cell_role` classify a header cell without consulting
// the navigator.
// A group naming fewer than two fields says nothing that
// `unique` does not already say, so it is dropped rather than
// generating a rule that duplicates one.
let unique_together: Vec<Vec<String>> = p
.unique_together
.clone()
.unwrap_or_default()
.into_iter()
.map(|g| g.fields)
.filter(|g: &Vec<String>| g.len() > 1)
.collect();
let schema = if p.row {
let key = ctx
.fetch_block_cell_id(&sheet_id, &block_id, 0, p.key_idx)?
.col;
let header = match p.header_idx {
Some(idx) => {
Some(ctx.fetch_block_cell_id(&sheet_id, &block_id, idx, 0)?.row)
}
None => None,
};
Schema::RowSchema(RowSchema {
fields,
key,
name: p.ref_name.clone(),
header,
unique_together: unique_together.clone(),
})
} else {
let key = ctx
.fetch_block_cell_id(&sheet_id, &block_id, p.key_idx, 0)?
.row;
let header = match p.header_idx {
Some(idx) => {
Some(ctx.fetch_block_cell_id(&sheet_id, &block_id, 0, idx)?.col)
}
None => None,
};
Schema::ColSchema(ColSchema {
fields,
key,
name: p.ref_name.clone(),
header,
unique_together: unique_together.clone(),
})
};
// A ref name addresses one block for the whole workbook. Taking
// one that is already spoken for used to overwrite the entry, so
// the first block stayed on the sheet but every BLOCKREF naming
// it silently began resolving to the second — formulas that kept
// evaluating, against the wrong data.
if let Some((owner_sheet, owner_block)) = manager.ref_name_owner(&p.ref_name) {
if (owner_sheet, owner_block) != (sheet_id, block_id) {
return Err(BasicError::BlockRefNameTaken(
p.ref_name.clone(),
owner_sheet,
owner_block,
)
.into());
}
}
if let Some(old_schema) = manager.schemas.get(&(sheet_id, block_id)) {
let old_ref = old_schema.get_ref_name();
manager.refs.remove(&old_ref);
}
manager.schemas.insert((sheet_id, block_id), schema);
manager
.refs
.insert(p.ref_name.clone(), (sheet_id, block_id));
dirty_blocks.insert((sheet_id, block_id));
Ok((
Self {
manager,
dirty_blocks,
},
true,
))
}
EditPayload::UpsertFieldFormulas(p) => {
let mut dirty_blocks = self.dirty_blocks;
let mut manager = self.manager;
let sheet_id = ctx
.fetch_sheet_id_by_index(p.sheet_idx)
.map_err(|l| BasicError::SheetIdxExceed(l))?;
let block_id = p.block_id;
// Must follow a prior BindFormSchema — refuse if no
// RowSchema / ColSchema is registered for this block.
let Some(existing) = manager.schemas.get(&(sheet_id, block_id)) else {
return Err(BasicError::BlockIdDoesNotExist(block_id).into());
};
let field_count = match existing {
Schema::RowSchema(s) => s.fields.len(),
Schema::ColSchema(s) => s.fields.len(),
Schema::RandomSchema(_) => return Err(no_field_formulas(block_id)),
};
// `[]` for any rule vec means "leave this rule kind
// untouched" — preserve the existing per-field values.
// Non-empty vecs replace all per-field values for that
// rule kind. This lets callers update one rule kind
// (e.g. only validation_formulas) without re-sending
// the others.
let field_empty = p.field_formulas.is_empty();
let validation_empty = p.validation_formulas.is_empty();
let editability_empty = p.editability_formulas.is_empty();
// Snapshot existing per-field rules so apply_rule_update
// can preserve them for any kind with an empty incoming
// vec.
let existing_rules: Vec<(Option<String>, Option<String>, Option<String>)> =
match existing {
Schema::RowSchema(s) => s
.fields
.iter()
.map(|(_, e)| {
(
e.value_formula.clone(),
e.validation_formula.clone(),
e.editability_formula.clone(),
)
})
.collect(),
Schema::ColSchema(s) => s
.fields
.iter()
.map(|(_, e)| {
(
e.value_formula.clone(),
e.validation_formula.clone(),
e.editability_formula.clone(),
)
})
.collect(),
Schema::RandomSchema(_) => return Err(no_field_formulas(block_id)),
};
let field_formulas = apply_rule_update(
field_empty,
p.field_formulas,
|i| existing_rules[i].0.clone(),
field_count,
);
let validation_formulas = apply_rule_update(
validation_empty,
p.validation_formulas,
|i| existing_rules[i].1.clone(),
field_count,
);
let editability_formulas = apply_rule_update(
editability_empty,
p.editability_formulas,
|i| existing_rules[i].2.clone(),
field_count,
);
if field_formulas.len() != field_count
|| validation_formulas.len() != field_count
|| editability_formulas.len() != field_count
{
return Err(BasicError::InvalidFormula(format!(
"UpsertFieldFormulas: formula vec length mismatch \
(fields={}, value={}, validation={}, editability={}) \
for block {}",
field_count,
field_formulas.len(),
validation_formulas.len(),
editability_formulas.len(),
block_id
))
.into());
}
// Validate every `#FIELD("X")` against the existing
// field names (same rule as BindFormSchema).
let declared_names: std::collections::HashSet<String> = match existing {
Schema::RowSchema(s) => s.fields.iter().map(|(n, _)| n.clone()).collect(),
Schema::ColSchema(s) => s.fields.iter().map(|(n, _)| n.clone()).collect(),
Schema::RandomSchema(_) => return Err(no_field_formulas(block_id)),
};
validate_field_refs(&field_formulas, &declared_names, "field_formulas")?;
validate_field_refs(&validation_formulas, &declared_names, "validation_formulas")?;
validate_field_refs(
&editability_formulas,
&declared_names,
"editability_formulas",
)?;
// Normalize (trim + empty → None), then mutate the
// schema's per-field rule slots in place.
let normalized_value = normalize_formula_vec(field_formulas);
let normalized_validation = normalize_formula_vec(validation_formulas);
let normalized_editability = normalize_formula_vec(editability_formulas);
let Some(schema_mut) = manager.schemas.get(&(sheet_id, block_id)).cloned() else {
return Err(BasicError::BlockIdDoesNotExist(block_id).into());
};
let updated = match schema_mut {
Schema::RowSchema(mut s) => {
for (i, entry) in s.fields.iter_mut().enumerate() {
entry.1.value_formula = normalized_value[i].clone();
entry.1.validation_formula = normalized_validation[i].clone();
entry.1.editability_formula = normalized_editability[i].clone();
}
Schema::RowSchema(s)
}
Schema::ColSchema(mut s) => {
for (i, entry) in s.fields.iter_mut().enumerate() {
entry.1.value_formula = normalized_value[i].clone();
entry.1.validation_formula = normalized_validation[i].clone();
entry.1.editability_formula = normalized_editability[i].clone();
}
Schema::ColSchema(s)
}
Schema::RandomSchema(_) => return Err(no_field_formulas(block_id)),
};
manager.schemas.insert((sheet_id, block_id), updated);
// Mark dirty so the formula_manager re-walks every
// cell in the block and re-materializes templates
// through input_block_cell_template (which now reads
// the updated formula slots).
dirty_blocks.insert((sheet_id, block_id));
Ok((
Self {
manager,
dirty_blocks,
},
true,
))
}
EditPayload::BindRandomSchema(p) => {
let mut dirty_blocks = self.dirty_blocks;
let mut manager = self.manager;
let sheet_id = ctx
.fetch_sheet_id_by_index(p.sheet_idx)
.map_err(|l| BasicError::SheetIdxExceed(l))?;
let block_id = p.block_id;
let mut key_field = Vec::new();
for unit in p.units {
let r = unit.row;
let c = unit.col;
let cell_id = ctx.fetch_block_cell_id(&sheet_id, &block_id, r, c)?;
key_field.push((unit.key, cell_id.row, cell_id.col, unit.render_id));
}
let schema = Schema::RandomSchema(RandomSchema {
key_field,
name: p.ref_name.clone(),
});
// A ref name addresses one block for the whole workbook. Taking
// one that is already spoken for used to overwrite the entry, so
// the first block stayed on the sheet but every BLOCKREF naming
// it silently began resolving to the second — formulas that kept
// evaluating, against the wrong data.
if let Some((owner_sheet, owner_block)) = manager.ref_name_owner(&p.ref_name) {
if (owner_sheet, owner_block) != (sheet_id, block_id) {
return Err(BasicError::BlockRefNameTaken(
p.ref_name.clone(),
owner_sheet,
owner_block,
)
.into());
}
}
if let Some(old_schema) = manager.schemas.get(&(sheet_id, block_id)) {
let old_ref = old_schema.get_ref_name();
manager.refs.remove(&old_ref);
}
manager.schemas.insert((sheet_id, block_id), schema);
manager
.refs
.insert(p.ref_name.clone(), (sheet_id, block_id));
dirty_blocks.insert((sheet_id, block_id));
Ok((
Self {
manager,
dirty_blocks,
},
true,
))
}
// Structural edits to a block's rows or columns.
//
// `BlockAll` is documented as being dirtied when a row or field is
// added or removed, and BLOCKREFS depends on nothing else — its
// filters scan whatever the block currently holds, so no per-cell
// edge can stand in for it. Only the three schema payloads above
// were dirtying it, which left deletion silently stale: removing a
// row from a block did not change `SUM(BLOCKREFS(...))` until some
// unrelated write happened to trigger a recalculation. A stale
// total that still looks like a total is the worst failure a
// calculation engine has.
//
// Insertion appeared to work only by accident: new rows materialize
// their fields' value formulas, and writing those cells reaches
// `BlockAll` through the per-cell edge. Rows with no computed field
// had the same bug. Dirty it explicitly for both directions rather
// than relying on a side effect.
EditPayload::InsertRowsInBlock(p) => {
Self::dirty_structural(self, ctx, p.sheet_idx, p.block_id)
}
EditPayload::DeleteRowsInBlock(p) => {
Self::dirty_structural(self, ctx, p.sheet_idx, p.block_id)
}
EditPayload::InsertColsInBlock(p) => {
Self::dirty_structural(self, ctx, p.sheet_idx, p.block_id)
}
EditPayload::DeleteColsInBlock(p) => {
Self::dirty_structural(self, ctx, p.sheet_idx, p.block_id)
}
_ => Ok((self, false)),
}
}
/// Mark a block's structure as changed, so `BlockAll` gets dirtied and the
/// block's external readers recompute. Returns `false` for "handled but
/// nothing else to do" — the payload's real work happens elsewhere; this
/// only records the dependency consequence.
fn dirty_structural<C: BlockSchemaCtx>(
mut this: Self,
ctx: &mut C,
sheet_idx: usize,
block_id: BlockId,
) -> Result<(Self, bool), Error> {
let sheet_id = ctx
.fetch_sheet_id_by_index(sheet_idx)
.map_err(|l| BasicError::SheetIdxExceed(l))?;
this.dirty_blocks.insert((sheet_id, block_id));
Ok((this, false))
}
}