a2ml 0.1.0

Parser and renderer for A2ML (Attested Markup Language)
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
550
551
552
553
554
555
556
557
558
559
560
// SPDX-License-Identifier: MPL-2.0
// (PMPL-1.0-or-later preferred; MPL-2.0 required for crates.io)

//! Line-by-line parser for A2ML documents.
//!
//! The parser processes input one line at a time, accumulating blocks and
//! inline content.  It recognises headings, directives (`@`-prefixed),
//! attestation blocks (`!attest`), fenced code blocks, block quotes,
//! thematic breaks, and paragraphs.

use std::path::Path;

use crate::error::{A2mlError, Result};
use crate::types::*;

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Parse an A2ML document from a string.
///
/// # Errors
///
/// Returns [`A2mlError::ParseError`] if the input contains malformed
/// directives, attestation blocks, or unclosed fenced code blocks.
///
/// # Examples
///
/// ```
/// use a2ml::parser::parse;
///
/// let doc = parse("# Hello\n\nA paragraph.").unwrap();
/// assert_eq!(doc.title.as_deref(), Some("Hello"));
/// assert_eq!(doc.blocks.len(), 2);
/// ```
pub fn parse(input: &str) -> Result<Document> {
    let mut state = ParserState::new();
    let lines: Vec<&str> = input.lines().collect();

    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];
        let line_num = i + 1; // 1-based

        // ----- fenced code block -----
        if state.in_code_block {
            if line.trim_start().starts_with("```") || line.trim_start().starts_with("~~~") {
                state.flush_code_block();
            } else {
                state.code_buf.push_str(line);
                state.code_buf.push('\n');
            }
            i += 1;
            continue;
        }

        let trimmed = line.trim();

        // blank line => flush current paragraph
        if trimmed.is_empty() {
            state.flush_paragraph();
            i += 1;
            continue;
        }

        // fenced code block start
        if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
            state.flush_paragraph();
            let lang = trimmed.trim_start_matches('`').trim_start_matches('~').trim();
            state.code_lang = if lang.is_empty() {
                None
            } else {
                Some(lang.to_string())
            };
            state.in_code_block = true;
            state.code_buf.clear();
            i += 1;
            continue;
        }

        // thematic break (---, ***, ___)
        if is_thematic_break(trimmed) {
            state.flush_paragraph();
            state.blocks.push(Block::ThematicBreak);
            i += 1;
            continue;
        }

        // heading
        if trimmed.starts_with('#') {
            state.flush_paragraph();
            let (level, text) = parse_heading(trimmed);
            let inlines = parse_inlines(text);

            // Capture the first H1 as the document title.
            if level == 1 && state.title.is_none() {
                state.title = Some(text.to_string());
            }

            state.blocks.push(Block::Heading {
                level,
                content: inlines,
            });
            i += 1;
            continue;
        }

        // directive (@name value)
        if trimmed.starts_with('@') {
            state.flush_paragraph();
            let directive = parse_directive(trimmed, line_num)?;
            state.directives.push(directive.clone());
            state.blocks.push(Block::Directive(directive));
            i += 1;
            continue;
        }

        // attestation (!attest ...)
        if trimmed.starts_with("!attest") {
            state.flush_paragraph();
            let attestation = parse_attestation(trimmed, line_num)?;
            state.attestations.push(attestation.clone());
            state.blocks.push(Block::Attestation(attestation));
            i += 1;
            continue;
        }

        // block quote (> ...)
        if trimmed.starts_with('>') {
            state.flush_paragraph();
            let quote_text = trimmed[1..].trim_start();
            let inner_doc = parse(quote_text)?;
            state
                .blocks
                .push(Block::BlockQuote(inner_doc.blocks));
            i += 1;
            continue;
        }

        // list item (- or * or 1.)
        if is_list_start(trimmed) {
            state.flush_paragraph();
            let (ordered, item_text) = parse_list_marker(trimmed);
            let item_inlines = parse_inlines(item_text);
            let item_block = vec![Block::Paragraph(item_inlines)];

            // Try to coalesce with a preceding list of the same type.
            if let Some(Block::List {
                ordered: prev_ordered,
                items,
            }) = state.blocks.last_mut()
            {
                if *prev_ordered == ordered {
                    items.push(item_block);
                    i += 1;
                    continue;
                }
            }

            state.blocks.push(Block::List {
                ordered,
                items: vec![item_block],
            });
            i += 1;
            continue;
        }

        // default: accumulate paragraph text
        state.para_buf.push_str(trimmed);
        state.para_buf.push(' ');
        i += 1;
    }

    // Flush any remaining content.
    if state.in_code_block {
        return Err(A2mlError::parse(
            lines.len(),
            1,
            "unterminated fenced code block",
        ));
    }
    state.flush_paragraph();

    Ok(Document {
        title: state.title,
        directives: state.directives,
        blocks: state.blocks,
        attestations: state.attestations,
    })
}

/// Parse an A2ML document from a file on disk.
///
/// # Errors
///
/// Returns [`A2mlError::Io`] if the file cannot be read, or a parse error
/// if the content is malformed.
pub fn parse_file(path: impl AsRef<Path>) -> Result<Document> {
    let content = std::fs::read_to_string(path)?;
    parse(&content)
}

// ---------------------------------------------------------------------------
// Internal parser state
// ---------------------------------------------------------------------------

/// Accumulator for the line-by-line parser.
struct ParserState {
    title: Option<String>,
    directives: Vec<Directive>,
    attestations: Vec<Attestation>,
    blocks: Vec<Block>,
    para_buf: String,
    in_code_block: bool,
    code_lang: Option<String>,
    code_buf: String,
}

impl ParserState {
    fn new() -> Self {
        Self {
            title: None,
            directives: Vec::new(),
            attestations: Vec::new(),
            blocks: Vec::new(),
            para_buf: String::new(),
            in_code_block: false,
            code_lang: None,
            code_buf: String::new(),
        }
    }

    /// Flush accumulated paragraph text into a `Block::Paragraph`.
    fn flush_paragraph(&mut self) {
        let text = self.para_buf.trim().to_string();
        if !text.is_empty() {
            let inlines = parse_inlines(&text);
            self.blocks.push(Block::Paragraph(inlines));
        }
        self.para_buf.clear();
    }

    /// Flush accumulated code block text into a `Block::CodeBlock`.
    fn flush_code_block(&mut self) {
        // Remove trailing newline if present.
        if self.code_buf.ends_with('\n') {
            self.code_buf.pop();
        }
        self.blocks.push(Block::CodeBlock {
            language: self.code_lang.take(),
            content: std::mem::take(&mut self.code_buf),
        });
        self.in_code_block = false;
    }
}

// ---------------------------------------------------------------------------
// Inline parser
// ---------------------------------------------------------------------------

/// Parse a string into a sequence of inline elements.
///
/// Recognises `**bold**`, `*italic*`, `` `code` ``, and `[text](url)` links.
fn parse_inlines(input: &str) -> Vec<Inline> {
    let mut result = Vec::new();
    let chars: Vec<char> = input.chars().collect();
    let mut i = 0;
    let mut text_buf = String::new();

    while i < chars.len() {
        // Inline code: `...`
        if chars[i] == '`' {
            if !text_buf.is_empty() {
                result.push(Inline::Text(std::mem::take(&mut text_buf)));
            }
            i += 1;
            let mut code = String::new();
            while i < chars.len() && chars[i] != '`' {
                code.push(chars[i]);
                i += 1;
            }
            if i < chars.len() {
                i += 1; // skip closing `
            }
            result.push(Inline::Code(code));
            continue;
        }

        // Bold: **...**
        if i + 1 < chars.len() && chars[i] == '*' && chars[i + 1] == '*' {
            if !text_buf.is_empty() {
                result.push(Inline::Text(std::mem::take(&mut text_buf)));
            }
            i += 2;
            let mut inner = String::new();
            while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '*') {
                inner.push(chars[i]);
                i += 1;
            }
            if i + 1 < chars.len() {
                i += 2; // skip closing **
            }
            result.push(Inline::Strong(parse_inlines(&inner)));
            continue;
        }

        // Emphasis: *...*
        if chars[i] == '*' {
            if !text_buf.is_empty() {
                result.push(Inline::Text(std::mem::take(&mut text_buf)));
            }
            i += 1;
            let mut inner = String::new();
            while i < chars.len() && chars[i] != '*' {
                inner.push(chars[i]);
                i += 1;
            }
            if i < chars.len() {
                i += 1; // skip closing *
            }
            result.push(Inline::Emphasis(parse_inlines(&inner)));
            continue;
        }

        // Link: [text](url)
        if chars[i] == '[' {
            if !text_buf.is_empty() {
                result.push(Inline::Text(std::mem::take(&mut text_buf)));
            }
            i += 1;
            let mut link_text = String::new();
            while i < chars.len() && chars[i] != ']' {
                link_text.push(chars[i]);
                i += 1;
            }
            if i < chars.len() {
                i += 1; // skip ]
            }
            if i < chars.len() && chars[i] == '(' {
                i += 1;
                let mut url = String::new();
                while i < chars.len() && chars[i] != ')' {
                    url.push(chars[i]);
                    i += 1;
                }
                if i < chars.len() {
                    i += 1; // skip )
                }
                result.push(Inline::Link {
                    content: parse_inlines(&link_text),
                    url,
                });
            } else {
                // Not a valid link — emit as text.
                text_buf.push('[');
                text_buf.push_str(&link_text);
                text_buf.push(']');
            }
            continue;
        }

        text_buf.push(chars[i]);
        i += 1;
    }

    if !text_buf.is_empty() {
        result.push(Inline::Text(text_buf));
    }
    result
}

// ---------------------------------------------------------------------------
// Line-level helpers
// ---------------------------------------------------------------------------

/// Parse a heading line (e.g. `## Foo`) into its level and text content.
fn parse_heading(line: &str) -> (u8, &str) {
    let level = line.chars().take_while(|&c| c == '#').count() as u8;
    let text = line[level as usize..].trim();
    (level, text)
}

/// Parse a directive line (e.g. `@version 1.0`) into a [`Directive`].
fn parse_directive(line: &str, line_num: usize) -> Result<Directive> {
    let without_at = line[1..].trim();
    let (name, value) = match without_at.split_once(char::is_whitespace) {
        Some((n, v)) => (n.trim(), v.trim()),
        None => (without_at, ""),
    };

    if name.is_empty() {
        return Err(A2mlError::parse(line_num, 1, "empty directive name"));
    }

    Ok(Directive::new(name, value))
}

/// Parse an attestation line (e.g. `!attest identity=Alice role=author trust=reviewed`).
fn parse_attestation(line: &str, line_num: usize) -> Result<Attestation> {
    let after_keyword = line.strip_prefix("!attest").unwrap_or(line).trim();
    let mut identity = None;
    let mut role = None;
    let mut trust_level = None;
    let mut timestamp = None;
    let mut note = None;

    for token in after_keyword.split_whitespace() {
        if let Some((key, val)) = token.split_once('=') {
            match key {
                "identity" => identity = Some(val.to_string()),
                "role" => role = Some(val.to_string()),
                "trust" => {
                    trust_level = TrustLevel::from_str(val);
                    if trust_level.is_none() {
                        return Err(A2mlError::parse(
                            line_num,
                            1,
                            format!("unknown trust level: {}", val),
                        ));
                    }
                }
                "timestamp" | "ts" => timestamp = Some(val.to_string()),
                "note" => note = Some(val.to_string()),
                _ => {
                    // Ignore unknown attestation keys for forward compatibility.
                }
            }
        }
    }

    let identity = identity.ok_or_else(|| {
        A2mlError::parse(line_num, 1, "attestation missing required 'identity' field")
    })?;
    let role = role.ok_or_else(|| {
        A2mlError::parse(line_num, 1, "attestation missing required 'role' field")
    })?;
    let trust_level = trust_level.ok_or_else(|| {
        A2mlError::parse(line_num, 1, "attestation missing required 'trust' field")
    })?;

    let mut att = Attestation::new(identity, role, trust_level);
    att.timestamp = timestamp;
    att.note = note;
    Ok(att)
}

/// Check whether a line is a thematic break (`---`, `***`, `___`).
fn is_thematic_break(line: &str) -> bool {
    let trimmed = line.trim();
    if trimmed.len() < 3 {
        return false;
    }
    let first = trimmed.chars().next().unwrap();
    (first == '-' || first == '*' || first == '_') && trimmed.chars().all(|c| c == first)
}

/// Check whether a line starts a list item.
fn is_list_start(line: &str) -> bool {
    line.starts_with("- ")
        || line.starts_with("* ")
        || (line.len() >= 3
            && line.as_bytes()[0].is_ascii_digit()
            && (line.contains(". ") || line.contains(") ")))
}

/// Parse the list marker from a line, returning (ordered, item_text).
fn parse_list_marker(line: &str) -> (bool, &str) {
    if line.starts_with("- ") || line.starts_with("* ") {
        (false, &line[2..])
    } else if let Some(pos) = line.find(". ") {
        let prefix = &line[..pos];
        if prefix.chars().all(|c| c.is_ascii_digit()) {
            (true, &line[pos + 2..])
        } else {
            (false, line)
        }
    } else if let Some(pos) = line.find(") ") {
        let prefix = &line[..pos];
        if prefix.chars().all(|c| c.is_ascii_digit()) {
            (true, &line[pos + 2..])
        } else {
            (false, line)
        }
    } else {
        (false, line)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn parse_empty_document() {
        let doc = parse("").unwrap();
        assert!(doc.title.is_none());
        assert!(doc.blocks.is_empty());
    }

    #[test]
    fn parse_heading_and_paragraph() {
        let doc = parse("# Title\n\nSome text here.").unwrap();
        assert_eq!(doc.title.as_deref(), Some("Title"));
        assert_eq!(doc.blocks.len(), 2);
    }

    #[test]
    fn parse_directive() {
        let doc = parse("@version 1.0").unwrap();
        assert_eq!(doc.directives.len(), 1);
        assert_eq!(doc.directives[0].name, "version");
        assert_eq!(doc.directives[0].value, "1.0");
    }

    #[test]
    fn parse_attestation_block() {
        let input = "!attest identity=Alice role=author trust=reviewed";
        let doc = parse(input).unwrap();
        assert_eq!(doc.attestations.len(), 1);
        assert_eq!(doc.attestations[0].identity, "Alice");
        assert_eq!(doc.attestations[0].trust_level, TrustLevel::Reviewed);
    }

    #[test]
    fn parse_code_block() {
        let input = "```rust\nfn main() {}\n```";
        let doc = parse(input).unwrap();
        assert_eq!(doc.blocks.len(), 1);
        if let Block::CodeBlock { language, content } = &doc.blocks[0] {
            assert_eq!(language.as_deref(), Some("rust"));
            assert_eq!(content, "fn main() {}");
        } else {
            panic!("expected CodeBlock");
        }
    }

    #[test]
    fn unterminated_code_block_errors() {
        let input = "```\nsome code";
        assert!(parse(input).is_err());
    }

    #[test]
    fn parse_thematic_break() {
        let doc = parse("---").unwrap();
        assert_eq!(doc.blocks.len(), 1);
        assert_eq!(doc.blocks[0], Block::ThematicBreak);
    }

    #[test]
    fn parse_inline_formatting() {
        let inlines = parse_inlines("hello **bold** and *italic* and `code`");
        // Should contain: Text, Strong, Text, Emphasis, Text, Code
        assert!(inlines.len() >= 5);
    }
}