opys-engine 0.12.0

Core library for opys — a file-based inventory of typed markdown documents with a verify gate and SQL query layer
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
//! Validation: does a markdown document conform to the schema's body?
//!
//! The document is parsed with comrak, lowered into a simplified block tree
//! (headings reconstructed into nested sections), then matched against the
//! schema body. Frontmatter validation is a later phase; the frontmatter block
//! is stripped before parsing.

use super::error::Problem;
use super::schema::*;
use comrak::nodes::{AstNode, ListType, NodeValue};
use comrak::{parse_document, Arena, Options};
use regex::Regex;
use serde_json::{Map, Value};

impl Schema {
    /// Validate a markdown document's **body** against the schema. An empty
    /// result means it conforms. (Frontmatter typing: future phase.)
    pub fn validate(&self, markdown: &str) -> Vec<Problem> {
        let body = strip_frontmatter(markdown);
        let arena = Arena::new();
        let root = parse_document(&arena, body, &Options::default());
        let doc = build_blocks(root.children());

        let mut problems = Vec::new();
        let mut path = Vec::new();
        match_nodes(&self.body, &doc, self.opts, &mut path, &mut problems);
        problems
    }

    /// Parse a **conforming** document into the typed data object, keyed by the
    /// schema's capture aliases. Returns the validation problems if it does not
    /// conform. (Frontmatter capture: future phase — body only for now.)
    pub fn extract(&self, markdown: &str) -> Result<Value, Vec<Problem>> {
        let problems = self.validate(markdown);
        if !problems.is_empty() {
            return Err(problems);
        }
        let body = strip_frontmatter(markdown);
        let arena = Arena::new();
        let root = parse_document(&arena, body, &Options::default());
        let doc = build_blocks(root.children());

        let mut obj = Map::new();
        extract_into(&self.body, &doc, &mut obj);
        Ok(Value::Object(obj))
    }
}

// ---- a simplified document block tree ------------------------------------

#[derive(Debug, Clone)]
enum DocBlock {
    Section {
        level: u8,
        title: String,
        children: Vec<DocBlock>,
    },
    List {
        ordered: bool,
        items: Vec<DocItem>,
    },
    Para(String),
}

#[derive(Debug, Clone)]
struct DocItem {
    text: String,
    checked: Option<bool>,
    children: Vec<DocBlock>,
}

/// One flat block before heading-nesting is reconstructed.
enum Flat {
    Heading(u8, String),
    Block(DocBlock),
}

fn build_blocks<'a>(nodes: impl Iterator<Item = &'a AstNode<'a>>) -> Vec<DocBlock> {
    let flats: Vec<Flat> = nodes.filter_map(flat_of).collect();
    let mut pos = 0;
    nest(&flats, &mut pos, 0)
}

fn flat_of<'a>(node: &'a AstNode<'a>) -> Option<Flat> {
    match &node.data.borrow().value {
        NodeValue::Heading(h) => Some(Flat::Heading(h.level, text_of(node))),
        NodeValue::List(nl) => Some(Flat::Block(doc_list(node, nl.list_type))),
        NodeValue::Paragraph => Some(Flat::Block(DocBlock::Para(text_of(node)))),
        _ => None,
    }
}

/// Reconstruct heading nesting: a heading owns following blocks and deeper
/// headings until a heading of the same or higher rank.
fn nest(flats: &[Flat], pos: &mut usize, parent_level: usize) -> Vec<DocBlock> {
    let mut out = Vec::new();
    while *pos < flats.len() {
        match &flats[*pos] {
            Flat::Heading(level, title) => {
                if (*level as usize) <= parent_level {
                    break;
                }
                let (level, title) = (*level, title.clone());
                *pos += 1;
                let children = nest(flats, pos, level as usize);
                out.push(DocBlock::Section {
                    level,
                    title,
                    children,
                });
            }
            Flat::Block(b) => {
                out.push(b.clone());
                *pos += 1;
            }
        }
    }
    out
}

fn doc_list<'a>(list: &'a AstNode<'a>, list_type: ListType) -> DocBlock {
    let mut items = Vec::new();
    for item in list.children() {
        let mut text = String::new();
        let mut child_nodes = Vec::new();
        for ch in item.children() {
            match &ch.data.borrow().value {
                NodeValue::Paragraph if text.is_empty() => text = text_of(ch),
                NodeValue::List(_) => child_nodes.push(ch),
                _ => {}
            }
        }
        let (checked, text) = strip_checkbox(&text);
        items.push(DocItem {
            text,
            checked,
            children: build_blocks(child_nodes.into_iter()),
        });
    }
    DocBlock::List {
        ordered: matches!(list_type, ListType::Ordered),
        items,
    }
}

/// Concatenate the inline text of a node (headings, paragraphs, item lines).
fn text_of<'a>(node: &'a AstNode<'a>) -> String {
    let mut s = String::new();
    collect_text(node, &mut s);
    s.trim().to_string()
}

fn collect_text<'a>(node: &'a AstNode<'a>, out: &mut String) {
    for c in node.children() {
        match &c.data.borrow().value {
            NodeValue::Text(t) => out.push_str(t),
            NodeValue::Code(code) => out.push_str(&code.literal),
            // Comrak parses HTML-like text (`<uuid>`, `<ctrl><shift>`) as raw
            // inline HTML; its literal is the source text, which we must keep or
            // the round-trip silently drops anything in angle brackets.
            NodeValue::HtmlInline(html) => out.push_str(html),
            NodeValue::SoftBreak | NodeValue::LineBreak => out.push(' '),
            _ => collect_text(c, out),
        }
    }
}

/// Pull a leading `[ ]` / `[x]` checkbox off an item's text (no GFM extension).
fn strip_checkbox(text: &str) -> (Option<bool>, String) {
    let t = text.trim_start();
    if let Some(rest) = t.strip_prefix("[ ] ") {
        (Some(false), rest.to_string())
    } else if let Some(rest) = t.strip_prefix("[x] ").or_else(|| t.strip_prefix("[X] ")) {
        (Some(true), rest.to_string())
    } else {
        (None, text.to_string())
    }
}

fn strip_frontmatter(md: &str) -> &str {
    let Some(rest) = md.strip_prefix("---\n") else {
        return md;
    };
    match rest.find("\n---") {
        Some(end) => {
            let after = &rest[end + 4..];
            after.strip_prefix('\n').unwrap_or(after)
        }
        None => md,
    }
}

// ---- the matcher ----------------------------------------------------------

fn match_nodes(
    schema: &[Node],
    doc: &[DocBlock],
    opts: SchemaOpts,
    path: &mut Vec<String>,
    problems: &mut Vec<Problem>,
) {
    let mut cursor = 0usize;
    for node in schema {
        let start = if opts.ordered { cursor } else { 0 };
        match node {
            Node::Heading {
                level,
                title,
                head,
                children,
            } => {
                let idxs: Vec<usize> = (start..doc.len())
                    .filter(|&i| is_section(&doc[i], *level, title))
                    .collect();
                if let Some(msg) = card_problem(head.card, idxs.len(), "section") {
                    problems.push(problem(path, alias(node), msg));
                }
                for &i in &idxs {
                    if let DocBlock::Section { children: sc, .. } = &doc[i] {
                        path.push(alias(node));
                        match_nodes(children, sc, opts, path, problems);
                        path.pop();
                    }
                }
                if opts.ordered {
                    if let Some(&last) = idxs.last() {
                        cursor = last + 1;
                    }
                }
            }
            Node::List {
                style,
                item,
                head,
                children,
            } => {
                let found = (start..doc.len()).find(|&i| is_list(&doc[i], *style));
                let count = match found {
                    Some(i) => {
                        if let DocBlock::List { items, .. } = &doc[i] {
                            items.len()
                        } else {
                            0
                        }
                    }
                    None => 0,
                };
                if let Some(msg) = card_problem(head.card, count, "item") {
                    problems.push(problem(path, alias(node), msg));
                }
                if let Some(i) = found {
                    if let DocBlock::List { items, .. } = &doc[i] {
                        for it in items {
                            if let Some(m) = item {
                                if !matches_text(m, &it.text) {
                                    problems.push(problem(
                                        path,
                                        alias(node),
                                        format!("item does not match {}: {}", describe(m), it.text),
                                    ));
                                }
                            }
                            if !children.is_empty() {
                                path.push(alias(node));
                                match_nodes(children, &it.children, opts, path, problems);
                                path.pop();
                            }
                        }
                    }
                    if opts.ordered {
                        cursor = i + 1;
                    }
                }
            }
            Node::Prose { text, head } => {
                let found = (start..doc.len()).find(|&i| matches!(doc[i], DocBlock::Para(_)));
                if let Some(i) = found {
                    if let DocBlock::Para(t) = &doc[i] {
                        if let Some(m) = text {
                            if !matches_text(m, t) {
                                problems.push(problem(
                                    path,
                                    alias(node),
                                    format!("paragraph does not match {}", describe(m)),
                                ));
                            }
                        }
                    }
                    if opts.ordered {
                        cursor = i + 1;
                    }
                } else if presence_required(head.card) {
                    problems.push(problem(
                        path,
                        alias(node),
                        "missing required paragraph".into(),
                    ));
                }
            }
        }
    }
}

fn is_section(b: &DocBlock, level: u8, title: &Match) -> bool {
    matches!(b, DocBlock::Section { level: l, title: t, .. } if *l == level && matches_text(title, t))
}

fn is_list(b: &DocBlock, style: ListStyle) -> bool {
    let DocBlock::List { ordered, items } = b else {
        return false;
    };
    match style {
        ListStyle::Ordered => *ordered,
        ListStyle::Bullet => !*ordered,
        ListStyle::Checklist => !*ordered && items.iter().any(|i| i.checked.is_some()),
    }
}

fn matches_text(m: &Match, text: &str) -> bool {
    match m {
        Match::Literal(l) => text.trim_start().starts_with(l),
        Match::Regex(p) => Regex::new(p).map(|re| re.is_match(text)).unwrap_or(false),
    }
}

fn describe(m: &Match) -> String {
    match m {
        Match::Literal(l) => format!("\"{l}\""),
        Match::Regex(p) => format!("/{p}/"),
    }
}

/// `Some(message)` when `count` violates the cardinality. `unit` is "section"
/// or "item" for the message.
fn card_problem(card: Card, count: usize, unit: &str) -> Option<String> {
    let ok = match card {
        Card::Required | Card::Plus => count >= 1,
        Card::Optional => count <= 1,
        Card::Star => true,
        Card::Range(min, max) => {
            count >= min as usize && max.map(|m| count <= m as usize).unwrap_or(true)
        }
    };
    if ok {
        return None;
    }
    Some(match card {
        Card::Required | Card::Plus => format!("expected at least one {unit}, found {count}"),
        Card::Optional => format!("expected at most one {unit}, found {count}"),
        Card::Range(min, Some(max)) => format!("expected {min}..{max} {unit}(s), found {count}"),
        Card::Range(min, None) => format!("expected at least {min} {unit}(s), found {count}"),
        Card::Star => unreachable!(),
    })
}

fn presence_required(card: Card) -> bool {
    matches!(card, Card::Required | Card::Plus | Card::Range(1.., _))
}

fn alias(node: &Node) -> String {
    let head = match node {
        Node::Heading { head, title, .. } => {
            if let Some(n) = &head.name {
                return n.clone();
            }
            if let Match::Literal(t) = title {
                return slug(t);
            }
            head
        }
        Node::List { head, .. } | Node::Prose { head, .. } => head,
    };
    head.name.clone().unwrap_or_else(|| "block".to_string())
}

fn slug(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() {
                c.to_ascii_lowercase()
            } else {
                '_'
            }
        })
        .collect::<String>()
        .trim_matches('_')
        .to_string()
}

fn problem(path: &[String], alias: String, message: String) -> Problem {
    let mut p = path.to_vec();
    p.push(alias);
    Problem { path: p, message }
}

// ---- extraction (capture aliases → JSON) ---------------------------------

/// The alias a node captures under, or `None` if it isn't captured (an unnamed
/// list/prose; a regex-titled heading without `@name`).
fn capture_alias(node: &Node) -> Option<String> {
    match node {
        Node::Heading { head, title, .. } => head.name.clone().or_else(|| match title {
            Match::Literal(t) => Some(slug(t)),
            Match::Regex(_) => None,
        }),
        Node::List { head, .. } | Node::Prose { head, .. } => head.name.clone(),
    }
}

/// A heading/range cardinality that yields an array rather than a single value.
fn repeated(card: Card) -> bool {
    matches!(card, Card::Plus | Card::Star | Card::Range(..))
}

fn extract_into(schema: &[Node], doc: &[DocBlock], obj: &mut Map<String, Value>) {
    for node in schema {
        let Some(key) = capture_alias(node) else {
            continue;
        };
        let value = match node {
            Node::Heading {
                level,
                title,
                head,
                children,
            } => {
                let secs: Vec<&DocBlock> = doc
                    .iter()
                    .filter(|b| is_section(b, *level, title))
                    .collect();
                let make = |sec: &DocBlock| {
                    let mut m = Map::new();
                    if let DocBlock::Section {
                        children: sc,
                        title: t,
                        ..
                    } = sec
                    {
                        if matches!(title, Match::Regex(_)) {
                            m.insert("title".into(), Value::String(t.clone()));
                        }
                        extract_into(children, sc, &mut m);
                    }
                    Value::Object(m)
                };
                if repeated(head.card) {
                    Value::Array(secs.iter().map(|s| make(s)).collect())
                } else {
                    secs.first().map(|s| make(s)).unwrap_or(Value::Null)
                }
            }
            Node::List {
                style,
                item,
                head,
                children,
            } => {
                let items: &[DocItem] = match doc.iter().find(|b| is_list(b, *style)) {
                    Some(DocBlock::List { items, .. }) => items,
                    _ => &[],
                };
                let single = matches!(head.card, Card::Required | Card::Optional);
                if single && children.is_empty() {
                    // A single labeled bullet captures the text after its label.
                    items
                        .first()
                        .map(|it| Value::String(after_label(item, &it.text)))
                        .unwrap_or(Value::Null)
                } else {
                    Value::Array(
                        items
                            .iter()
                            .map(|it| {
                                if children.is_empty() {
                                    Value::String(it.text.clone())
                                } else {
                                    let mut m = Map::new();
                                    m.insert("text".into(), Value::String(it.text.clone()));
                                    extract_into(children, &it.children, &mut m);
                                    Value::Object(m)
                                }
                            })
                            .collect(),
                    )
                }
            }
            Node::Prose { .. } => match doc.iter().find(|b| matches!(b, DocBlock::Para(_))) {
                Some(DocBlock::Para(t)) => Value::String(t.clone()),
                _ => Value::Null,
            },
        };
        obj.insert(key, value);
    }
}

/// The text after a literal label (`Docs: foo` with label `Docs:` → `foo`);
/// otherwise the whole text.
fn after_label(item: &Option<Match>, text: &str) -> String {
    if let Some(Match::Literal(l)) = item {
        if let Some(rest) = text.trim_start().strip_prefix(l.as_str()) {
            return rest.trim().to_string();
        }
    }
    text.to_string()
}