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
// =============================================================================
//        #######
//     ###       ###     F: validation.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 validation contracts and behavior for this crate.

use serde::{Deserialize, Serialize};

use crate::{
    DataValue, ElementKind, ErrorCode, ExportContext, ExportFormat, ExportRequest, FileMakerError,
    HtmlMode, OperationControl, PdfMode, ResolvedScene, ResourceLimits, Result, TemplateIr,
};

/// Severity retained in machine-readable validation output.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ValidationSeverity {
    /// Rendering can proceed unless strict warnings are requested.
    Warning,
    /// Rendering must not proceed.
    Error,
}

/// Stable validation/preflight condition.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ValidationCode {
    /// Binding/condition/repeat expression is invalid.
    Binding,
    /// Typed input does not satisfy its structural or declared data contract.
    Data,
    /// Referenced asset is absent or invalid.
    Asset,
    /// Font/glyph layout cannot be preserved.
    Glyph,
    /// Resolved elements overlap.
    Collision,
    /// Visual bounds leave the page.
    Overflow,
    /// Effective raster resolution is below policy.
    Dpi,
    /// Requested exporter cannot preserve an element.
    Capability,
    /// Editable PDF font embedding is not configured.
    FontEmbedding,
    /// Required accessibility metadata is unavailable.
    Accessibility,
    /// A generic schema/data/layout invariant failed.
    Contract,
    /// Diagnostic/comparison budget was exhausted.
    Budget,
}

/// One bounded first-class validation issue.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ValidationIssue {
    /// Severity.
    pub severity: ValidationSeverity,
    /// Stable condition.
    pub code: ValidationCode,
    /// Optional page.
    pub page: Option<usize>,
    /// Optional element.
    pub element: Option<String>,
    /// Bounded explanation.
    pub message: String,
}

/// Deterministic validation output.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct ValidationReport {
    /// Issues in discovery order.
    pub issues: Vec<ValidationIssue>,
    /// Whether additional issues were omitted by the caller's bound.
    #[serde(default)]
    pub truncated: bool,
}

impl ValidationReport {
    /// Returns true when at least one hard error exists.
    #[must_use]
    pub fn has_errors(&self) -> bool {
        self.issues
            .iter()
            .any(|issue| issue.severity == ValidationSeverity::Error)
    }

    /// Returns true when at least one warning exists.
    #[must_use]
    pub fn has_warnings(&self) -> bool {
        self.issues
            .iter()
            .any(|issue| issue.severity == ValidationSeverity::Warning)
    }

    /// Enforces errors, plus warnings when strict mode is active.
    pub fn enforce(&self, strict: bool) -> Result<()> {
        if self.truncated {
            return Err(FileMakerError::new(
                ErrorCode::Validation,
                "validation report was truncated before completion",
            ));
        }
        let rejected = self.issues.iter().find(|issue| {
            issue.severity == ValidationSeverity::Error
                || (strict && issue.severity == ValidationSeverity::Warning)
        });
        if let Some(issue) = rejected {
            return Err(FileMakerError::new(
                ErrorCode::Validation,
                format!("validation rejected {:?}: {}", issue.code, issue.message),
            ));
        }
        Ok(())
    }

    pub(crate) fn push(
        &mut self,
        severity: ValidationSeverity,
        code: ValidationCode,
        page: Option<usize>,
        element: Option<&str>,
        message: impl Into<String>,
        max_issues: usize,
    ) {
        if self.issues.len() >= max_issues {
            self.truncated = true;
            return;
        }
        let mut message = message.into();
        message.truncate(512);
        self.issues.push(ValidationIssue {
            severity,
            code,
            page,
            element: element.map(ToOwned::to_owned),
            message,
        });
    }
}

/// Caller-selected preflight policy.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PreflightOptions {
    /// Treat warnings as rejection.
    pub strict: bool,
    /// Require accessibility metadata from the current schema.
    pub require_accessibility: bool,
    /// Minimum effective image resolution.
    pub minimum_image_dpi: u32,
    /// Maximum retained diagnostics.
    pub max_issues: usize,
}

impl Default for PreflightOptions {
    fn default() -> Self {
        Self {
            strict: false,
            require_accessibility: false,
            minimum_image_dpi: 150,
            max_issues: 1_000,
        }
    }
}

/// Validates reusable template IR and binding syntax.
pub fn validate_template(template: &TemplateIr, limits: &ResourceLimits) -> ValidationReport {
    let mut report = ValidationReport::default();
    crate::validation_data::inspect_template(template, limits, &mut report);
    report
}

/// Validates bounded typed data, its optional schema, and every binding.
pub fn validate_data(
    template: &TemplateIr,
    data: &DataValue,
    limits: &ResourceLimits,
) -> ValidationReport {
    let mut report = ValidationReport::default();
    crate::validation_data::inspect_data(template, data, limits, &mut report);
    report
}

/// Validates resolved page, glyph, table, overflow, and collision invariants.
pub fn validate_layout(
    scene: &ResolvedScene,
    limits: &ResourceLimits,
    max_issues: usize,
    control: &OperationControl,
) -> Result<ValidationReport> {
    if max_issues == 0 {
        return Err(FileMakerError::new(
            ErrorCode::Validation,
            "layout validation requires a non-zero issue bound",
        ));
    }
    crate::validation_layout::inspect(
        scene,
        limits,
        &PreflightOptions {
            max_issues,
            ..PreflightOptions::default()
        },
        control,
    )
}

/// Runs exporter-aware validation over a fully resolved scene.
pub fn preflight(
    scene: &ResolvedScene,
    request: &ExportRequest,
    context: &ExportContext<'_>,
    options: &PreflightOptions,
    control: &OperationControl,
) -> Result<ValidationReport> {
    if options.max_issues == 0 || options.minimum_image_dpi == 0 {
        return Err(FileMakerError::new(
            ErrorCode::Validation,
            "preflight options require non-zero issue and DPI bounds",
        ));
    }
    crate::export::validate_request(scene, request, context.limits)?;
    let mut report = crate::validation_layout::inspect(scene, context.limits, options, control)?;
    inspect_accessibility(request, options, &mut report);
    for page in &scene.pages {
        for element in &page.elements {
            inspect_element(page.index, element, request, context, options, &mut report)?;
        }
    }
    report.enforce(options.strict)?;
    Ok(report)
}

fn inspect_element(
    page: usize,
    element: &crate::ResolvedElement,
    request: &ExportRequest,
    context: &ExportContext<'_>,
    options: &PreflightOptions,
    report: &mut ValidationReport,
) -> Result<()> {
    crate::validation_capability::inspect_paint(page, element, request, options, report);
    if matches!(
        element.kind,
        ElementKind::Chart | ElementKind::Qr | ElementKind::Barcode
    ) {
        report.push(
            ValidationSeverity::Error,
            ValidationCode::Capability,
            Some(page),
            Some(element.id.as_str()),
            "requested exporter has no renderer for this prepared element kind",
            options.max_issues,
        );
    }
    if element.kind == ElementKind::Text {
        inspect_text(page, element, request, context, options, report);
    }
    if element.kind == ElementKind::Image {
        inspect_image(page, element, request, context, options, report)?;
    }
    if element.kind == ElementKind::Table {
        crate::validation_table::inspect_export(page, element, request, context, options, report);
    }
    Ok(())
}

fn inspect_text(
    page: usize,
    element: &crate::ResolvedElement,
    request: &ExportRequest,
    context: &ExportContext<'_>,
    options: &PreflightOptions,
    report: &mut ValidationReport,
) {
    let Some(layout) = &element.text_layout else {
        return;
    };
    if request.format == ExportFormat::Pdf
        && matches!(request.pdf_mode, PdfMode::Editable | PdfMode::Hybrid)
    {
        for run in layout.lines.iter().flat_map(|line| &line.runs) {
            if context.fonts.get(&run.font).is_err() {
                report.push(
                    ValidationSeverity::Error,
                    ValidationCode::FontEmbedding,
                    Some(page),
                    Some(element.id.as_str()),
                    format!("font `{}` is unavailable for embedding", run.font),
                    options.max_issues,
                );
            }
        }
    }
}

fn inspect_image(
    page: usize,
    element: &crate::ResolvedElement,
    request: &ExportRequest,
    context: &ExportContext<'_>,
    options: &PreflightOptions,
    report: &mut ValidationReport,
) -> Result<()> {
    if options.require_accessibility {
        report.push(
            ValidationSeverity::Warning,
            ValidationCode::Accessibility,
            Some(page),
            Some(element.id.as_str()),
            "schema 1.0 does not carry image alternative text",
            options.max_issues,
        );
    }
    let (Some(name), Some(resolver)) = (&element.asset, context.assets) else {
        report.push(
            ValidationSeverity::Error,
            ValidationCode::Asset,
            Some(page),
            Some(element.id.as_str()),
            "image asset or explicit resolver is missing",
            options.max_issues,
        );
        return Ok(());
    };
    match resolver.resolve_asset(name, context.limits.max_asset_bytes) {
        Ok(asset) => match element.image_placement {
            Some(placement) if !placement.vector => {
                let transformed_destination = element.transform.bounds(placement.destination)?;
                let dpi_x_tenths = effective_dpi_tenths(
                    placement.source.width,
                    transformed_destination.size.width,
                )?;
                let dpi_y_tenths = effective_dpi_tenths(
                    placement.source.height,
                    transformed_destination.size.height,
                )?;
                let dpi_tenths = dpi_x_tenths.min(dpi_y_tenths);
                if dpi_tenths < u64::from(options.minimum_image_dpi) * 10 {
                    report.push(
                        ValidationSeverity::Warning,
                        ValidationCode::Dpi,
                        Some(page),
                        Some(element.id.as_str()),
                        format!(
                            "effective image DPI {}.{} is below {}",
                            dpi_tenths / 10,
                            dpi_tenths % 10,
                            options.minimum_image_dpi
                        ),
                        options.max_issues,
                    );
                }
                if request.format == ExportFormat::Jpeg
                    && crate::validation_capability::raster_asset_has_alpha(&asset, placement)?
                {
                    report.push(
                        ValidationSeverity::Warning,
                        ValidationCode::Capability,
                        Some(page),
                        Some(element.id.as_str()),
                        "JPEG flattens raster image alpha on white",
                        options.max_issues,
                    );
                }
            }
            Some(_) => {
                if matches!(
                    request.format,
                    ExportFormat::Pdf | ExportFormat::Png | ExportFormat::Jpeg
                ) {
                    report.push(
                        ValidationSeverity::Warning,
                        ValidationCode::Capability,
                        Some(page),
                        Some(element.id.as_str()),
                        "requested exporter does not rasterize SVG assets",
                        options.max_issues,
                    );
                }
            }
            None => report.push(
                ValidationSeverity::Error,
                ValidationCode::Asset,
                Some(page),
                Some(element.id.as_str()),
                "image geometry was not resolved during layout",
                options.max_issues,
            ),
        },
        Err(error) => report.push(
            ValidationSeverity::Error,
            ValidationCode::Asset,
            Some(page),
            Some(element.id.as_str()),
            error.to_string(),
            options.max_issues,
        ),
    }
    Ok(())
}

fn inspect_accessibility(
    request: &ExportRequest,
    options: &PreflightOptions,
    report: &mut ValidationReport,
) {
    if options.require_accessibility
        && !(request.format == ExportFormat::Html && request.html_mode == HtmlMode::Semantic)
    {
        report.push(
            ValidationSeverity::Warning,
            ValidationCode::Accessibility,
            None,
            None,
            "requested exporter does not provide tagged semantic reading structure",
            options.max_issues,
        );
    }
}

fn effective_dpi_tenths(pixels: u32, size: crate::Unit) -> Result<u64> {
    if size <= crate::Unit::ZERO {
        return Err(FileMakerError::new(
            ErrorCode::GeometryInvalid,
            "image destination size must be positive",
        ));
    }
    let numerator = u128::from(pixels) * 720 * u128::from(crate::Unit::PER_POINT as u64);
    let denominator = u128::try_from(size.raw()).map_err(|_| {
        FileMakerError::new(
            ErrorCode::GeometryInvalid,
            "image destination size cannot be converted",
        )
    })?;
    let value = numerator / denominator;
    u64::try_from(value).map_err(|_| {
        FileMakerError::new(ErrorCode::GeometryInvalid, "effective image DPI overflow")
    })
}