vole-document 0.1.0-alpha.30

Persistent procedural document runtime: byte-exact reconstruction plus a content-addressed procedural seed DAG, queryable observations with provenance, and selective late materialization.
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
//! Machine-readable capability discovery for the common observation layer
//! (Phase 12.7, ADR-0031, plan §DEC-5).
//!
//! Formats do not share coordinates, so the set of *common* observations a
//! format can serve is explicit and auditable rather than silently approximated.
//! [`capabilities_for_format`] returns the supported common selectors and their
//! admissible representations, the versioned extraction profiles, and the native
//! selectors; [`common_supported`] is the single predicate the planner and the
//! evaluator both use, so a capability error is never a surprise.

use crate::field::document_format::DocumentFormat;
use crate::field::observe::{Representation, Selector};

/// One common selector and the representations it admits for a format.
#[derive(Debug, Clone, Copy)]
pub struct SelectorCapability {
    /// Canonical common selector name.
    pub selector: &'static str,
    /// Admissible representation names.
    pub representations: &'static [&'static str],
}

/// The capability set for one detected format.
#[derive(Debug, Clone)]
pub struct Capabilities {
    /// The detected format.
    pub format: DocumentFormat,
    /// Whether the adapter for this format is compiled into this build.
    pub compiled: bool,
    /// Supported common selectors (empty when the adapter is not compiled).
    pub selectors: Vec<SelectorCapability>,
    /// Versioned extraction-profile fingerprints (DOCX/EPUB).
    pub profiles: Vec<String>,
    /// Native selectors retained as first-class peers of the common vocabulary.
    pub native_selectors: Vec<&'static str>,
}

impl Capabilities {
    /// A deterministic, flat JSON object (machine-readable capability discovery).
    pub fn to_json(&self) -> String {
        let selectors = self
            .selectors
            .iter()
            .map(|s| {
                let reps = s
                    .representations
                    .iter()
                    .map(|r| format!("\"{r}\""))
                    .collect::<Vec<_>>()
                    .join(",");
                format!(
                    "{{\"selector\":\"{}\",\"representations\":[{reps}]}}",
                    s.selector
                )
            })
            .collect::<Vec<_>>()
            .join(",");
        let profiles = self
            .profiles
            .iter()
            .map(|p| format!("\"{}\"", crate::field::provenance::json_escape(p)))
            .collect::<Vec<_>>()
            .join(",");
        let native = self
            .native_selectors
            .iter()
            .map(|s| format!("\"{s}\""))
            .collect::<Vec<_>>()
            .join(",");
        format!(
            concat!(
                "{{",
                "\"root\":\"document\",",
                "\"format\":\"{}\",",
                "\"adapter\":\"{}\",",
                "\"compiled\":{},",
                "\"selectors\":[{}],",
                "\"profiles\":[{}],",
                "\"native_selectors\":[{}]",
                "}}"
            ),
            self.format.name(),
            self.format.adapter(),
            self.compiled,
            selectors,
            profiles,
            native
        )
    }
}

const DOCX_NATIVE: &[&str] = &[
    "docx-story",
    "docx-paragraph",
    "docx-table",
    "docx-cell",
    "docx-find",
    "package-part",
    "relationship",
    "member",
];
const EPUB_NATIVE: &[&str] = &[
    "epub-package",
    "epub-manifest-item",
    "epub-spine-item",
    "epub-nav",
    "epub-nav-node",
    "epub-resource",
    "epub-block",
    "epub-cell",
    "epub-link",
    "epub-find",
    "member",
];
const ODT_NATIVE: &[&str] = &[
    "odt-part",
    "odt-paragraph",
    "odt-heading",
    "odt-table",
    "odt-cell",
    "odt-list",
    "odt-find",
    "package-part",
    "member",
];
const PDF_NATIVE: &[&str] = &[
    "document",
    "object",
    "stream",
    "revision",
    "revisions",
    "page",
    "byte-range",
    "text-match",
];
const ODS_NATIVE: &[&str] = &[
    "ods-sheet",
    "ods-cell",
    "ods-find",
    "ods-styles",
    "ods-named-expressions",
    "ods-comments",
    "package-part",
    "member",
];
const XLSX_NATIVE: &[&str] = &[
    "xlsx-sheet",
    "xlsx-cell",
    "xlsx-find",
    "xlsx-styles",
    "xlsx-defined-names",
    "xlsx-external-rels",
    "xlsx-comments",
    "xlsx-hyperlinks",
    "xlsx-tables",
    "xlsx-drawing",
    "package-part",
    "relationship",
    "member",
];
const PPTX_NATIVE: &[&str] = &[
    "pptx-slide",
    "pptx-shape",
    "pptx-notes",
    "pptx-layouts",
    "pptx-masters",
    "pptx-theme",
    "pptx-media",
    "pptx-tables",
    "pptx-find",
    "package-part",
    "relationship",
    "member",
];
const ODP_NATIVE: &[&str] = &[
    "odp-slide",
    "odp-shape",
    "odp-notes",
    "odp-masters",
    "odp-media",
    "odp-tables",
    "odp-find",
    "package-part",
    "member",
];

const COMMON_METADATA: &[&str] = &["metadata"];
const COMMON_TEXT: &[&str] = &["text"];
const COMMON_TEXT_META: &[&str] = &["text", "metadata"];
const COMMON_RESOURCE: &[&str] = &["metadata"];
const COMMON_RESOURCE_BYTES: &[&str] = &["metadata", "exact", "decoded"];
const SEARCH: &[&str] = &["text"];

fn caps(selector: &'static str, representations: &'static [&'static str]) -> SelectorCapability {
    SelectorCapability {
        selector,
        representations,
    }
}

/// The capability set for a detected format, filtered by what this build compiles.
pub fn capabilities_for_format(format: DocumentFormat) -> Capabilities {
    let compiled = format.compiled();
    if !compiled {
        return Capabilities {
            format,
            compiled,
            selectors: Vec::new(),
            profiles: Vec::new(),
            native_selectors: Vec::new(),
        };
    }
    let (selectors, profiles, native_selectors): (Vec<SelectorCapability>, Vec<String>, Vec<&str>) =
        match format {
            DocumentFormat::Pdf => (
                vec![
                    caps("metadata", COMMON_METADATA),
                    caps("text", COMMON_TEXT),
                    caps("search-match", SEARCH),
                ],
                Vec::new(),
                PDF_NATIVE.to_vec(),
            ),
            DocumentFormat::Docx => (
                vec![
                    caps("metadata", COMMON_METADATA),
                    caps("text", COMMON_TEXT),
                    caps("heading", COMMON_TEXT_META),
                    caps("block", COMMON_TEXT_META),
                    caps("table", COMMON_TEXT_META),
                    caps("cell", COMMON_TEXT_META),
                    caps("resource", COMMON_RESOURCE),
                    caps("link", COMMON_METADATA),
                    caps("search-match", SEARCH),
                ],
                docx_profiles(),
                DOCX_NATIVE.to_vec(),
            ),
            DocumentFormat::Epub => (
                vec![
                    caps("metadata", COMMON_METADATA),
                    caps("text", COMMON_TEXT),
                    caps("heading", COMMON_TEXT_META),
                    caps("block", COMMON_TEXT_META),
                    caps("table", COMMON_TEXT_META),
                    caps("cell", COMMON_TEXT_META),
                    caps("resource", COMMON_RESOURCE_BYTES),
                    caps("link", COMMON_METADATA),
                    caps("search-match", SEARCH),
                ],
                epub_profiles(),
                EPUB_NATIVE.to_vec(),
            ),
            DocumentFormat::Odt => (
                vec![
                    caps("metadata", COMMON_METADATA),
                    caps("text", COMMON_TEXT),
                    caps("heading", COMMON_TEXT_META),
                    caps("block", COMMON_TEXT_META),
                    caps("table", COMMON_TEXT_META),
                    caps("cell", COMMON_TEXT_META),
                    caps("resource", COMMON_RESOURCE),
                    caps("link", COMMON_METADATA),
                    caps("search-match", SEARCH),
                ],
                odt_profiles(),
                ODT_NATIVE.to_vec(),
            ),
            DocumentFormat::Ods => (
                vec![
                    caps("metadata", COMMON_METADATA),
                    caps("text", COMMON_TEXT),
                    caps("table", COMMON_TEXT_META),
                    caps("cell", COMMON_TEXT_META),
                    caps("search-match", SEARCH),
                ],
                ods_profiles(),
                ODS_NATIVE.to_vec(),
            ),
            DocumentFormat::Xlsx => (
                vec![
                    caps("metadata", COMMON_METADATA),
                    caps("text", COMMON_TEXT),
                    caps("table", COMMON_TEXT_META),
                    caps("cell", COMMON_TEXT_META),
                    caps("search-match", SEARCH),
                ],
                xlsx_profiles(),
                XLSX_NATIVE.to_vec(),
            ),
            DocumentFormat::Pptx => (
                vec![
                    caps("metadata", COMMON_METADATA),
                    caps("text", COMMON_TEXT),
                    caps("table", COMMON_TEXT_META),
                    caps("cell", COMMON_TEXT_META),
                    caps("search-match", SEARCH),
                ],
                pptx_profiles(),
                PPTX_NATIVE.to_vec(),
            ),
            DocumentFormat::Odp => (
                vec![
                    caps("metadata", COMMON_METADATA),
                    caps("text", COMMON_TEXT),
                    caps("table", COMMON_TEXT_META),
                    caps("cell", COMMON_TEXT_META),
                    caps("search-match", SEARCH),
                ],
                odp_profiles(),
                ODP_NATIVE.to_vec(),
            ),
            DocumentFormat::Opaque => (Vec::new(), Vec::new(), Vec::new()),
        };
    Capabilities {
        format,
        compiled,
        selectors,
        profiles,
        native_selectors,
    }
}

#[cfg(feature = "docx")]
fn docx_profiles() -> Vec<String> {
    vec![crate::adapter::docx::DocxExtractProfile::DEFAULT.fingerprint()]
}
#[cfg(not(feature = "docx"))]
fn docx_profiles() -> Vec<String> {
    Vec::new()
}

#[cfg(feature = "epub")]
fn epub_profiles() -> Vec<String> {
    vec![crate::adapter::epub::EpubExtractProfile::DEFAULT.fingerprint()]
}
#[cfg(not(feature = "epub"))]
fn epub_profiles() -> Vec<String> {
    Vec::new()
}

#[cfg(feature = "odt")]
fn odt_profiles() -> Vec<String> {
    vec![crate::adapter::odt::OdtExtractProfile::DEFAULT.fingerprint()]
}
#[cfg(not(feature = "odt"))]
fn odt_profiles() -> Vec<String> {
    Vec::new()
}

#[cfg(feature = "ods")]
fn ods_profiles() -> Vec<String> {
    vec![crate::adapter::ods::OdsExtractProfile::DEFAULT.fingerprint()]
}
#[cfg(not(feature = "ods"))]
fn ods_profiles() -> Vec<String> {
    Vec::new()
}

#[cfg(feature = "xlsx")]
fn xlsx_profiles() -> Vec<String> {
    vec![crate::adapter::xlsx::XlsxExtractProfile::DEFAULT.fingerprint()]
}
#[cfg(not(feature = "xlsx"))]
fn xlsx_profiles() -> Vec<String> {
    Vec::new()
}

#[cfg(feature = "pptx")]
fn pptx_profiles() -> Vec<String> {
    vec![crate::adapter::pptx::PptxExtractProfile::DEFAULT.fingerprint()]
}
#[cfg(not(feature = "pptx"))]
fn pptx_profiles() -> Vec<String> {
    Vec::new()
}

#[cfg(feature = "odp")]
fn odp_profiles() -> Vec<String> {
    vec![crate::adapter::odp::OdpExtractProfile::DEFAULT.fingerprint()]
}
#[cfg(not(feature = "odp"))]
fn odp_profiles() -> Vec<String> {
    Vec::new()
}

/// The canonical name of a common selector, or `None` for a native selector.
pub fn common_selector_name(selector: &Selector) -> Option<&'static str> {
    match selector {
        Selector::Metadata => Some("metadata"),
        Selector::Text => Some("text"),
        Selector::Heading(_) => Some("heading"),
        Selector::Block(_) => Some("block"),
        Selector::Table(_) => Some("table"),
        Selector::Cell { .. } => Some("cell"),
        Selector::Resource(_) => Some("resource"),
        Selector::Link(_) => Some("link"),
        Selector::SearchMatch(_) => Some("search-match"),
        _ => None,
    }
}

/// The representations admissible for `selector` under `format`, if the pair is
/// structurally applicable.
pub fn common_representations(
    format: DocumentFormat,
    selector: &Selector,
) -> Option<&'static [&'static str]> {
    if !format.compiled() {
        return None;
    }
    let name = common_selector_name(selector)?;
    let caps = capabilities_for_format(format);
    caps.selectors
        .iter()
        .find(|c| c.selector == name)
        .map(|c| c.representations)
}

/// Whether `format` can serve `selector` with `representation`.
pub fn common_supported(
    format: DocumentFormat,
    selector: &Selector,
    representation: Representation,
) -> bool {
    common_representations(format, selector)
        .is_some_and(|reps| reps.contains(&representation.name()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pdf_supports_text_and_search_but_not_tables() {
        assert!(common_supported(
            DocumentFormat::Pdf,
            &Selector::Text,
            Representation::Text
        ));
        assert!(common_supported(
            DocumentFormat::Pdf,
            &Selector::SearchMatch("x".into()),
            Representation::Text
        ));
        assert!(!common_supported(
            DocumentFormat::Pdf,
            &Selector::Table(0),
            Representation::Text
        ));
        assert!(!common_supported(
            DocumentFormat::Pdf,
            &Selector::Text,
            Representation::Metadata
        ));
    }

    #[test]
    fn opaque_and_unknown_selectors_are_unsupported() {
        assert!(!common_supported(
            DocumentFormat::Opaque,
            &Selector::Text,
            Representation::Text
        ));
        assert!(!common_supported(
            DocumentFormat::Pdf,
            &Selector::Document,
            Representation::Metadata
        ));
    }
}