appcore-filemaker 0.1.0-beta.1

Deterministic declarative document, canvas, and dataset compiler for AppCore
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
// =============================================================================
//        #######
//     ###       ###     F: debug.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/08/30 05:00:00 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/30 05:00:00 by dnettoRaw
//      ###########      S: 1.0.2-rc
// =============================================================================

//! Defines bounded debug contracts and behavior for this crate.

use serde::{Deserialize, Serialize};

use crate::debug_geometry::{mask_free_regions, selected_bounds};
use crate::{
    Color, ElementId, ErrorCode, FileMakerError, Point, Rect, ResolvedElement, ResolvedPage,
    ResolvedScene, ResourceLimits, Result, SceneInspector, Size, Unit,
};

/// Geometry view used by a derived mask.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MaskView {
    /// Collision bounds.
    #[default]
    CollisionMask,
    /// Layout bounds.
    LayoutBounds,
    /// Visual bounds.
    VisualBounds,
    /// All distinct bounds.
    Combined,
}

/// Debug overlay switches. These never mutate a scene.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct DebugOverlayOptions {
    /// Grid spacing; common values are 1/5/10/20 logical units.
    pub grid: Option<Unit>,
    /// Draw coordinate rulers.
    pub ruler: bool,
    /// Draw IDs.
    pub ids: bool,
    /// Draw resolved origin coordinates.
    pub coordinates: bool,
    /// Draw bounds.
    pub bounds: bool,
    /// Label retained anchor expressions.
    pub anchors: bool,
    /// Draw named region rectangles.
    pub regions: bool,
    /// Draw the page safe-area rectangle.
    pub safe_area: bool,
    /// Draw collidable geometry and named exclusions.
    pub collision: bool,
    /// Draw a crosshair at each element origin.
    pub crosshair: bool,
    /// Bounds class.
    pub view: MaskView,
}

/// Format-neutral debug primitive.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum DebugPrimitive {
    /// Line segment.
    Line {
        /// Start.
        from: Point,
        /// End.
        to: Point,
        /// Stroke.
        color: Color,
    },
    /// Rectangle outline.
    Rect {
        /// Bounds.
        bounds: Rect,
        /// Stroke.
        color: Color,
    },
    /// Label.
    Label {
        /// Origin.
        origin: Point,
        /// UTF-8 label.
        text: String,
        /// Text color.
        color: Color,
    },
}

/// Derived overlay, separate from resolved scene elements.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct DebugOverlay {
    /// Page index.
    pub page: usize,
    /// Debug-only primitives.
    pub primitives: Vec<DebugPrimitive>,
}

impl DebugOverlay {
    /// Builds an overlay without modifying scene geometry or paint order.
    pub fn build(
        scene: &ResolvedScene,
        page: usize,
        options: &DebugOverlayOptions,
    ) -> Result<Self> {
        Self::build_bounded(scene, page, options, &ResourceLimits::default())
    }

    /// Builds an overlay under the caller's scene and diagnostic budgets.
    pub fn build_bounded(
        scene: &ResolvedScene,
        page: usize,
        options: &DebugOverlayOptions,
        limits: &ResourceLimits,
    ) -> Result<Self> {
        crate::resolved::validate_scene_contract(scene, limits)?;
        let page_ref = scene
            .pages
            .get(page)
            .ok_or_else(|| debug_error("debug page was not found"))?;
        if let Some(grid) = options.grid {
            validate_grid(grid)?;
        }
        crate::debug_plan::validate_overlay(page_ref, options, limits)?;
        let mut primitives = Vec::new();
        if let Some(grid) = options.grid {
            add_grid(&mut primitives, page_ref.size, grid)?;
        }
        add_page_geometry(&mut primitives, page_ref, options)?;
        for element in &page_ref.elements {
            add_element_geometry(&mut primitives, element, options)?;
        }
        if options.ruler {
            add_ruler(
                &mut primitives,
                page_ref.size,
                options.grid.unwrap_or(Unit::points(10)?),
            )?;
        }
        Ok(Self { page, primitives })
    }
}

fn add_page_geometry(
    primitives: &mut Vec<DebugPrimitive>,
    page: &ResolvedPage,
    options: &DebugOverlayOptions,
) -> Result<()> {
    if options.safe_area {
        if let Some(bounds) = page
            .page_template
            .as_ref()
            .map(crate::PageTemplate::safe_bounds)
            .transpose()?
        {
            add_named_rect(
                primitives,
                "safe",
                bounds,
                Color::Rgb { r: 0, g: 128, b: 0 },
            );
        }
    }
    if options.regions {
        for region in &page.regions {
            add_named_rect(
                primitives,
                &format!("region:{}", region.name),
                region.bounds,
                Color::Rgb {
                    r: 0,
                    g: 96,
                    b: 192,
                },
            );
        }
    }
    if options.collision {
        for element in page.elements.iter().filter(|element| element.collidable) {
            add_named_rect(
                primitives,
                &format!("collision:{}", element.id.as_str()),
                element.bounds.collision,
                Color::Rgb {
                    r: 220,
                    g: 0,
                    b: 160,
                },
            );
        }
        for exclusion in &page.exclusions {
            add_named_rect(
                primitives,
                &format!("exclusion:{}", exclusion.name),
                exclusion.bounds,
                Color::Rgb {
                    r: 160,
                    g: 0,
                    b: 220,
                },
            );
        }
    }
    Ok(())
}

fn add_element_geometry(
    primitives: &mut Vec<DebugPrimitive>,
    element: &ResolvedElement,
    options: &DebugOverlayOptions,
) -> Result<()> {
    if options.bounds {
        for bounds in selected_bounds(element.bounds, options.view) {
            primitives.push(DebugPrimitive::Rect {
                bounds,
                color: Color::Rgba {
                    r: 255,
                    g: 0,
                    b: 0,
                    a: 160,
                },
            });
        }
    }
    if options.ids {
        primitives.push(DebugPrimitive::Label {
            origin: element.bounds.layout.origin,
            text: element.id.as_str().to_owned(),
            color: Color::Rgb { r: 180, g: 0, b: 0 },
        });
    }
    if options.coordinates {
        primitives.push(DebugPrimitive::Label {
            origin: element.bounds.layout.origin,
            text: format!(
                "({:.3}, {:.3}) pt",
                element.bounds.layout.origin.x.as_points_f64(),
                element.bounds.layout.origin.y.as_points_f64()
            ),
            color: Color::Rgb { r: 0, g: 0, b: 0 },
        });
    }
    if options.anchors {
        for (edge, expression) in &element.layout_trace.geometry.anchors {
            primitives.push(DebugPrimitive::Label {
                origin: element.bounds.layout.origin,
                text: format!("anchor:{edge}={expression}"),
                color: Color::Rgb {
                    r: 128,
                    g: 64,
                    b: 0,
                },
            });
        }
    }
    if options.crosshair {
        add_crosshair(primitives, element.bounds.layout.origin)?;
    }
    Ok(())
}

fn add_grid(primitives: &mut Vec<DebugPrimitive>, size: Size, spacing: Unit) -> Result<()> {
    let color = Color::Rgba {
        r: 0,
        g: 128,
        b: 255,
        a: 64,
    };
    let mut x = Unit::ZERO;
    while x <= size.width {
        primitives.push(DebugPrimitive::Line {
            from: Point { x, y: Unit::ZERO },
            to: Point { x, y: size.height },
            color,
        });
        x = x.checked_add(spacing)?;
    }
    let mut y = Unit::ZERO;
    while y <= size.height {
        primitives.push(DebugPrimitive::Line {
            from: Point { x: Unit::ZERO, y },
            to: Point { x: size.width, y },
            color,
        });
        y = y.checked_add(spacing)?;
    }
    Ok(())
}

fn validate_grid(grid: Unit) -> Result<()> {
    for spacing in [1, 5, 10, 20] {
        if grid == Unit::points(spacing)? {
            return Ok(());
        }
    }
    Err(debug_error("debug grid must be 1, 5, 10, or 20 points"))
}

fn add_ruler(primitives: &mut Vec<DebugPrimitive>, size: Size, spacing: Unit) -> Result<()> {
    let mut x = Unit::ZERO;
    while x <= size.width {
        primitives.push(DebugPrimitive::Label {
            origin: Point { x, y: Unit::ZERO },
            text: format!("{:.3}", x.as_points_f64()),
            color: Color::Rgb { r: 0, g: 0, b: 0 },
        });
        x = x.checked_add(spacing)?;
    }
    let mut y = Unit::ZERO;
    while y <= size.height {
        primitives.push(DebugPrimitive::Label {
            origin: Point { x: Unit::ZERO, y },
            text: format!("{:.3}", y.as_points_f64()),
            color: Color::Rgb { r: 0, g: 0, b: 0 },
        });
        y = y.checked_add(spacing)?;
    }
    Ok(())
}

fn add_named_rect(primitives: &mut Vec<DebugPrimitive>, name: &str, bounds: Rect, color: Color) {
    primitives.push(DebugPrimitive::Rect { bounds, color });
    primitives.push(DebugPrimitive::Label {
        origin: bounds.origin,
        text: name.to_owned(),
        color,
    });
}

/// JSON-serializable geometry-derived mask.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct CollisionMask {
    /// Page index.
    pub page: usize,
    /// Page size.
    pub size: Size,
    /// Selected occupied rectangles.
    pub occupied: Vec<(ElementId, Rect)>,
    /// Disjoint free rectangles.
    pub free: Vec<Rect>,
    /// Pairwise collisions.
    pub collisions: Vec<(ElementId, ElementId, Rect)>,
    /// Visual overflow IDs.
    pub overflow: Vec<ElementId>,
}

impl CollisionMask {
    /// Derives a mask from resolved geometry; bitmap pixels are never queried.
    pub fn derive(scene: &ResolvedScene, page: usize, view: MaskView) -> Result<Self> {
        Self::derive_bounded(scene, page, view, &ResourceLimits::default())
    }

    /// Derives a mask under the caller's scene and diagnostic geometry budgets.
    pub fn derive_bounded(
        scene: &ResolvedScene,
        page: usize,
        view: MaskView,
        limits: &ResourceLimits,
    ) -> Result<Self> {
        crate::resolved::validate_scene_contract(scene, limits)?;
        let page_ref = scene
            .pages
            .get(page)
            .ok_or_else(|| debug_error("mask page was not found"))?;
        let mut budget = crate::diagnostic_budget::DiagnosticBudget::new(limits)?;
        let mut occupied = Vec::new();
        for element in page_ref
            .elements
            .iter()
            .filter(|element| view != MaskView::CollisionMask || element.collidable)
        {
            let mut element_bounds = Vec::new();
            for bounds in selected_bounds(element.bounds, view) {
                if !element_bounds.contains(&bounds) {
                    budget.retained(occupied.len().saturating_add(1))?;
                    occupied.push((element.id.clone(), bounds));
                    element_bounds.push(bounds);
                }
            }
        }
        if matches!(view, MaskView::CollisionMask | MaskView::Combined) {
            for exclusion in &page_ref.exclusions {
                budget.retained(occupied.len().saturating_add(1))?;
                occupied.push((
                    ElementId::new(format!("exclusion.{}", exclusion.name))?,
                    exclusion.bounds,
                ));
            }
        }
        let mut collisions = Vec::new();
        for (index, (left_id, left)) in occupied.iter().enumerate() {
            for (right_id, right) in &occupied[index + 1..] {
                if left_id == right_id {
                    continue;
                }
                budget.operation()?;
                if let Some(overlap) = left.intersection(*right)? {
                    budget.retained(collisions.len().saturating_add(1))?;
                    collisions.push((left_id.clone(), right_id.clone(), overlap));
                }
            }
        }
        let free = mask_free_regions(page_ref.size, &occupied, &mut budget)?;
        let overflow = SceneInspector::new(scene).inspect_page(page)?.overflow;
        Ok(Self {
            page,
            size: page_ref.size,
            occupied,
            free,
            collisions,
            overflow,
        })
    }

    /// Serializes stable geometry JSON.
    pub fn to_json(&self) -> Result<Vec<u8>> {
        self.to_json_bounded(&ResourceLimits::default())
    }

    /// Serializes stable geometry JSON under caller-supplied budgets.
    pub fn to_json_bounded(&self, limits: &ResourceLimits) -> Result<Vec<u8>> {
        self.validate_limits(limits)?;
        let size = crate::memory::serialized_size_pretty(self)?;
        if size > limits.max_output_bytes {
            return Err(FileMakerError::new(
                ErrorCode::LimitExceeded,
                "debug mask JSON exceeds the output budget",
            ));
        }
        let mut bytes = Vec::with_capacity(size);
        serde_json::to_writer_pretty(&mut bytes, self)
            .map_err(|error| debug_error(format!("cannot encode mask JSON: {error}")))?;
        debug_assert_eq!(bytes.len(), size);
        Ok(bytes)
    }

    pub(crate) fn validate_limits(&self, limits: &ResourceLimits) -> Result<()> {
        limits.validate()?;
        let retained = self
            .occupied
            .len()
            .checked_add(self.free.len())
            .and_then(|count| count.checked_add(self.collisions.len()))
            .and_then(|count| count.checked_add(self.overflow.len()))
            .ok_or_else(|| {
                FileMakerError::new(ErrorCode::LimitExceeded, "debug mask count overflow")
            })?;
        crate::diagnostic_budget::DiagnosticBudget::new(limits)?.retained(retained)
    }
}

fn add_crosshair(primitives: &mut Vec<DebugPrimitive>, origin: Point) -> Result<()> {
    let arm = Unit::points(3)?;
    primitives.push(DebugPrimitive::Line {
        from: Point {
            x: origin.x.checked_sub(arm)?,
            y: origin.y,
        },
        to: Point {
            x: origin.x.checked_add(arm)?,
            y: origin.y,
        },
        color: Color::Rgb {
            r: 255,
            g: 0,
            b: 255,
        },
    });
    primitives.push(DebugPrimitive::Line {
        from: Point {
            x: origin.x,
            y: origin.y.checked_sub(arm)?,
        },
        to: Point {
            x: origin.x,
            y: origin.y.checked_add(arm)?,
        },
        color: Color::Rgb {
            r: 255,
            g: 0,
            b: 255,
        },
    });
    Ok(())
}

fn debug_error(message: impl Into<String>) -> FileMakerError {
    FileMakerError::new(ErrorCode::LayoutInvalid, message)
}