zenith-tx 0.0.7

Zenith transaction op set, apply/dry-run engine, diffs, and audit records.
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
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
//! `Group` / `Ungroup` / `Reparent` application, plus the common-parent finder
//! and ungroup splice helper they use.

use std::collections::BTreeMap;

use zenith_core::{Diagnostic, Document, GroupNode, Node};

use crate::op::Position;

use super::super::{find_node_shared, node_id_of, record_affected, subtree_contains};
use super::finders::{find_container_children_mut, remove_node_by_id, resolve_position};

/// Find which page directly contains (at the top level of `page.children`) ALL
/// of the ids in `node_ids`. Returns `(page_index, sorted_indices)` where
/// `sorted_indices` is the list of positions within `page.children` in
/// ascending order, or `None` if the ids are not all siblings on one page.
///
/// We walk each page's *direct* children only — a flat O(pages × ids) scan.
/// Nesting is handled by a second pass that descends into containers.
fn find_common_parent_children_mut<'doc>(
    doc: &'doc mut Document,
    node_ids: &[String],
) -> Option<&'doc mut Vec<Node>> {
    // Phase 1 (shared scan): find which page + container has ALL ids as direct
    // children.  We search each page's full subtree of containers.
    struct Hit {
        page_index: usize,
        /// If `None` the parent is the page itself; otherwise it's the container id.
        container_id: Option<String>,
    }

    let hit: Option<Hit> = 'outer: {
        for (pi, page) in doc.body.pages.iter().enumerate() {
            // Check if all are direct children of this page.
            if node_ids.iter().all(|id| {
                page.children
                    .iter()
                    .any(|n| node_id_of(n) == Some(id.as_str()))
            }) {
                break 'outer Some(Hit {
                    page_index: pi,
                    container_id: None,
                });
            }
            // Walk containers within this page.
            if let Some(cid) = find_container_with_all_children(&page.children, node_ids) {
                break 'outer Some(Hit {
                    page_index: pi,
                    container_id: Some(cid),
                });
            }
        }
        None
    };

    let Hit {
        page_index,
        container_id,
    } = hit?;

    // Phase 2 (exclusive borrow): return a mutable ref to the right vec.
    match container_id {
        None => doc.body.pages.get_mut(page_index).map(|p| &mut p.children),
        Some(cid) => find_container_children_mut(doc, &cid),
    }
}

/// Walk `children` recursively and return the id of the first container whose
/// *direct* children include all ids in `node_ids`. Returns `None` if no such
/// container exists in this subtree.
fn find_container_with_all_children(children: &[Node], node_ids: &[String]) -> Option<String> {
    for node in children {
        let (container_id, grandchildren) = match node {
            Node::Frame(f) => (f.id.as_str(), f.children.as_slice()),
            Node::Group(g) => (g.id.as_str(), g.children.as_slice()),
            Node::Rect(_)
            | Node::Ellipse(_)
            | Node::Line(_)
            | Node::Text(_)
            | Node::Code(_)
            | Node::Image(_)
            | Node::Polygon(_)
            | Node::Polyline(_)
            | Node::Instance(_)
            | Node::Field(_)
            | Node::Footnote(_)
            | Node::Toc(_)
            | Node::Table(_)
            | Node::Shape(_)
            | Node::Connector(_)
            | Node::Pattern(_)
            | Node::Chart(_)
            | Node::Light(_)
            | Node::Mesh(_)
            | Node::Unknown(_) => continue,
        };
        if node_ids.iter().all(|id| {
            grandchildren
                .iter()
                .any(|n| node_id_of(n) == Some(id.as_str()))
        }) {
            return Some(container_id.to_owned());
        }
        if let Some(found) = find_container_with_all_children(grandchildren, node_ids) {
            return Some(found);
        }
    }
    None
}

pub(in crate::engine) fn apply_group(
    node_ids: &[String],
    group_id: &str,
    doc: &mut Document,
    diagnostics: &mut Vec<Diagnostic>,
    affected: &mut Vec<String>,
) {
    // Require at least one id.
    if node_ids.is_empty() {
        diagnostics.push(Diagnostic::error(
            "tx.invalid_parent",
            "group requires at least one node id".to_owned(),
            None,
            None,
        ));
        return;
    }

    // Phase 1: locate the common parent children vec (shared-then-exclusive
    // two-phase, handled inside find_common_parent_children_mut).
    let children = match find_common_parent_children_mut(doc, node_ids) {
        Some(c) => c,
        None => {
            diagnostics.push(Diagnostic::error(
                "tx.invalid_parent",
                "group requires all nodes to share a parent".to_owned(),
                None,
                None,
            ));
            return;
        }
    };

    // Phase 2: collect the indices of the named nodes within this children vec,
    // in ascending order so we can determine insert position and remove cleanly.
    let mut indices: Vec<usize> = node_ids
        .iter()
        .filter_map(|id| {
            children
                .iter()
                .position(|n| node_id_of(n) == Some(id.as_str()))
        })
        .collect();

    // All ids must resolve (filter_map would silently drop missing ones).
    if indices.len() != node_ids.len() {
        diagnostics.push(Diagnostic::error(
            "tx.invalid_parent",
            "group requires all nodes to share a parent".to_owned(),
            None,
            None,
        ));
        return;
    }

    indices.sort_unstable();

    // Insert position = index of the first (lowest) member.
    // indices is non-empty: node_ids is non-empty and all ids resolved (guarded
    // by the length check above), so .first() will always be Some.
    let Some(&insert_at) = indices.first() else {
        return; // unreachable: guarded by node_ids.is_empty() check above
    };

    // Extract the nodes in their original relative order (lowest index first).
    // indices is already sorted ascending, so this produces source-order children.
    // All indices came from `.position()` on the same `children` slice and the
    // slice has not been mutated since — `.get()` returns Some for all of them.
    let group_children: Vec<Node> = indices
        .iter()
        .filter_map(|&i| children.get(i).cloned())
        .collect();

    // Remove from back to front to keep earlier indices stable.
    for &i in indices.iter().rev() {
        children.remove(i);
    }

    // Adjust insert_at: each removal of an index < insert_at shifts insert_at
    // down by one.  Since we sorted indices ascending and insert_at == indices[0],
    // all removed indices that were < insert_at have already been removed by the
    // rev-order loop above.  Actually insert_at is indices[0] (the minimum), so
    // no indices precede it — insert_at is stable after we remove >= insert_at
    // indices. We need to count how many indices were strictly less than insert_at
    // before the removals: since insert_at = indices[0] (minimum), zero indices
    // are smaller. So insert_at doesn't change.
    let insert_at = insert_at.min(children.len());

    // Build the group node with all fields at defaults (None / empty).
    // v0: x/y are None — no translation offset; authors must adjust child
    // geometry themselves if a specific group origin is needed.
    let group_node = Node::Group(GroupNode {
        id: group_id.to_owned(),
        name: None,
        role: None,
        x: None,
        y: None,
        w: None,
        h: None,
        opacity: None,
        visible: None,
        locked: None,
        rotate: None,
        blend_mode: None,
        shadow: None,
        filter: None,
        mask: None,
        blur: None,
        style: None,
        semantic_role: None,
        intensity: None,
        layer_priority: None,
        anchor: None,
        anchor_zone: None,
        anchor_sibling: None,
        anchor_edge: None,
        anchor_gap: None,
        anchor_parent: None,
        children: group_children,
        protected_regions: Vec::new(),
        editable_param_ids: Vec::new(),
        source_span: None,
        unknown_props: BTreeMap::new(),
    });

    children.insert(insert_at, group_node);
    record_affected(group_id, affected);
    // Post-validation catches group_id collision (id.duplicate).
}

pub(in crate::engine) fn apply_ungroup(
    group_id: &str,
    doc: &mut Document,
    diagnostics: &mut Vec<Diagnostic>,
    affected: &mut Vec<String>,
) {
    // Phase 1 (shared scan): verify node exists and is a group; also capture
    // whether it has a non-zero x/y (for the advisory) and its page index.
    struct GroupInfo {
        page_index: usize,
        has_nonzero_offset: bool,
    }

    let info: Option<Result<GroupInfo, &'static str>> = {
        let mut result = None;
        'outer: for (pi, page) in doc.body.pages.iter().enumerate() {
            if let Some(node) = find_node_shared(&page.children, group_id) {
                let info = match node {
                    Node::Group(g) => {
                        // A geometry offset is "non-zero" for a literal dimension
                        // whose value is non-zero, or for any token ref (its value
                        // isn't resolvable here, so treat it as a meaningful offset).
                        let nonzero = |pv: Option<&zenith_core::PropertyValue>| match pv {
                            Some(zenith_core::PropertyValue::Dimension(d)) => d.value != 0.0,
                            Some(zenith_core::PropertyValue::TokenRef(_)) => true,
                            Some(zenith_core::PropertyValue::Literal(_))
                            | Some(zenith_core::PropertyValue::DataRef(_))
                            | None => false,
                        };
                        let has_offset = nonzero(g.x.as_ref()) || nonzero(g.y.as_ref());
                        Ok(GroupInfo {
                            page_index: pi,
                            has_nonzero_offset: has_offset,
                        })
                    }
                    Node::Rect(_)
                    | Node::Ellipse(_)
                    | Node::Line(_)
                    | Node::Text(_)
                    | Node::Code(_)
                    | Node::Frame(_)
                    | Node::Image(_)
                    | Node::Polygon(_)
                    | Node::Polyline(_)
                    | Node::Instance(_)
                    | Node::Field(_)
                    | Node::Footnote(_)
                    | Node::Toc(_)
                    | Node::Table(_)
                    | Node::Shape(_)
                    | Node::Connector(_)
                    | Node::Pattern(_)
                    | Node::Chart(_)
                    | Node::Light(_)
                    | Node::Mesh(_)
                    | Node::Unknown(_) => Err("not a group"),
                };
                result = Some(info);
                break 'outer;
            }
        }
        result
    };

    let info = match info {
        None => {
            diagnostics.push(Diagnostic::error(
                "tx.unknown_node",
                format!("node {:?} not found in document", group_id),
                None,
                Some(group_id.to_owned()),
            ));
            return;
        }
        Some(Err(reason)) => {
            diagnostics.push(Diagnostic::error(
                "tx.unsupported_property",
                format!("ungroup: {:?} is {}", group_id, reason),
                None,
                Some(group_id.to_owned()),
            ));
            return;
        }
        Some(Ok(info)) => info,
    };

    // Advisory: v0 limitation — group x/y offset is not propagated to children.
    if info.has_nonzero_offset {
        diagnostics.push(Diagnostic::advisory(
            "tx.noop",
            format!(
                "ungroup: group {:?} has a non-zero x/y offset; v0 does not \
                 apply the offset to children on ungroup — child positions may \
                 shift visually",
                group_id
            ),
            None,
            Some(group_id.to_owned()),
        ));
    }

    // Phase 2 (exclusive borrow): splice the group's children in-place.
    let Some(page) = doc.body.pages.get_mut(info.page_index) else {
        return; // unreachable: page_index came from the shared scan above.
    };

    // Find and remove the group node from the page's subtree, then splice.
    splice_ungroup(&mut page.children, group_id);

    record_affected(group_id, affected);
}

/// Walk `children` to find the group with `group_id`, remove it, and insert
/// its children at the same index. Returns `true` if the group was found and
/// spliced, `false` otherwise (to continue recursion).
fn splice_ungroup(children: &mut Vec<Node>, group_id: &str) -> bool {
    // Check direct children first.
    if let Some(i) = children
        .iter()
        .position(|n| node_id_of(n) == Some(group_id))
    {
        // We confirmed it's a group in the shared-scan phase; use .get() for
        // checked access — the match arm handles the unreachable-but-safe case.
        let group_children = match children.get(i) {
            Some(Node::Group(g)) => g.children.clone(),
            // unreachable under normal flow
            Some(Node::Rect(_))
            | Some(Node::Ellipse(_))
            | Some(Node::Line(_))
            | Some(Node::Text(_))
            | Some(Node::Code(_))
            | Some(Node::Frame(_))
            | Some(Node::Image(_))
            | Some(Node::Polygon(_))
            | Some(Node::Polyline(_))
            | Some(Node::Instance(_))
            | Some(Node::Field(_))
            | Some(Node::Footnote(_))
            | Some(Node::Toc(_))
            | Some(Node::Table(_))
            | Some(Node::Shape(_))
            | Some(Node::Connector(_))
            | Some(Node::Pattern(_))
            | Some(Node::Chart(_))
            | Some(Node::Light(_))
            | Some(Node::Mesh(_))
            | Some(Node::Unknown(_))
            | None => return false,
        };
        children.remove(i);
        // Insert the group's children at the same position, in order.
        for (offset, child) in group_children.into_iter().enumerate() {
            children.insert(i + offset, child);
        }
        return true;
    }
    // Descend into nested containers.
    for child in children.iter_mut() {
        let grandchildren = match child {
            Node::Frame(f) => &mut f.children,
            Node::Group(g) => &mut g.children,
            Node::Rect(_)
            | Node::Ellipse(_)
            | Node::Line(_)
            | Node::Text(_)
            | Node::Code(_)
            | Node::Image(_)
            | Node::Polygon(_)
            | Node::Polyline(_)
            | Node::Instance(_)
            | Node::Field(_)
            | Node::Footnote(_)
            | Node::Toc(_)
            | Node::Table(_)
            | Node::Shape(_)
            | Node::Connector(_)
            | Node::Pattern(_)
            | Node::Chart(_)
            | Node::Light(_)
            | Node::Mesh(_)
            | Node::Unknown(_) => continue,
        };
        if splice_ungroup(grandchildren, group_id) {
            return true;
        }
    }
    false
}

pub(in crate::engine) fn apply_reparent(
    node_id: &str,
    new_parent: &str,
    position: &Position,
    doc: &mut Document,
    diagnostics: &mut Vec<Diagnostic>,
    affected: &mut Vec<String>,
) {
    // Phase 1 (shared scan): verify the node exists and capture the subtree so
    // we can run the cycle check without a mutable borrow.
    let node_page_index = doc.body.pages.iter().enumerate().find_map(|(pi, page)| {
        if page.children.iter().any(|n| subtree_contains(n, node_id)) {
            Some(pi)
        } else {
            None
        }
    });

    let pi = match node_page_index {
        Some(pi) => pi,
        None => {
            diagnostics.push(Diagnostic::error(
                "tx.unknown_node",
                format!("node {:?} not found in document", node_id),
                None,
                Some(node_id.to_owned()),
            ));
            return;
        }
    };

    // Cycle check (shared borrow): new_parent must not be node itself or a
    // descendant of node.  We locate the node in the shared slice and run
    // subtree_contains on it.
    {
        let page = match doc.body.pages.get(pi) {
            Some(p) => p,
            None => return, // unreachable
        };
        if let Some(node_ref) = find_node_shared(&page.children, node_id)
            && subtree_contains(node_ref, new_parent)
        {
            diagnostics.push(Diagnostic::error(
                "tx.invalid_parent",
                format!(
                    "cannot reparent {:?} into {:?}: new_parent is within \
                     the node's own subtree",
                    node_id, new_parent
                ),
                None,
                Some(new_parent.to_owned()),
            ));
            return;
        }
        // Shared borrow of `page` ends here.
    }

    // Phase 2 (exclusive borrows): remove then re-insert.
    // Step 2a — remove the node from its current parent.
    let node = {
        // We need a mutable borrow of the page to remove; we know the page index.
        let page = match doc.body.pages.get_mut(pi) {
            Some(p) => p,
            None => return, // unreachable
        };
        match remove_node_by_id(&mut page.children, node_id) {
            Some(n) => n,
            None => {
                // Unexpected: the shared scan found it but remove didn't.
                diagnostics.push(Diagnostic::error(
                    "tx.unknown_node",
                    format!("node {:?} disappeared during reparent", node_id),
                    None,
                    Some(node_id.to_owned()),
                ));
                return;
            }
        }
    };
    // The mutable borrow of `doc.body.pages[pi]` ends here.

    // Step 2b — locate the new parent's children vec.
    // `find_container_children_mut` handles page ids AND nested container ids.
    let new_children = match find_container_children_mut(doc, new_parent) {
        Some(c) => c,
        None => {
            // new_parent is not a container — roll back by re-inserting the node
            // at the end of its original page (best-effort; the transaction will
            // be rejected by the error diagnostic anyway).
            if let Some(page) = doc.body.pages.get_mut(pi) {
                page.children.push(node);
            }
            diagnostics.push(Diagnostic::error(
                "tx.invalid_parent",
                format!(
                    "no container with id {:?} (new_parent must be a page, group, or frame)",
                    new_parent
                ),
                None,
                Some(new_parent.to_owned()),
            ));
            return;
        }
    };

    // Step 2c — resolve the insertion index and insert.
    let idx = match resolve_position(position, new_children, new_parent, diagnostics) {
        Some(i) => i,
        None => {
            // resolve_position already pushed a diagnostic; roll back.
            if let Some(page) = doc.body.pages.get_mut(pi) {
                page.children.push(node);
            }
            return;
        }
    };

    new_children.insert(idx, node);
    record_affected(node_id, affected);
}