ailint-core 1.0.1

Core library for ailint: discovery, parsing, rule engine, and reporters for AI agent guidance files.
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
//! Markdown parsing via `pulldown-cmark`.

use std::ops::Range;

use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};

/// Structural index of a Markdown document, built in one parse pass.
#[derive(Debug, Clone, Default)]
pub struct MarkdownDoc {
    /// YAML frontmatter block, if the file starts with `---`.
    pub frontmatter: Option<Frontmatter>,
    /// All headings, in document order.
    pub headings: Vec<Heading>,
    /// Heading-delimited spans covering the whole body.
    pub sections: Vec<Section>,
    /// Fenced and indented code blocks.
    pub code_blocks: Vec<CodeBlock>,
    /// Bullet and ordered list items, flattened.
    pub list_items: Vec<ListItem>,
    /// All hyperlinks.
    pub links: Vec<Link>,
}

/// Raw YAML frontmatter (`---` fenced) at the top of a document.
#[derive(Debug, Clone)]
pub struct Frontmatter {
    /// Frontmatter body without the `---` fences.
    pub raw: String,
    /// Byte span in the original input, fences included.
    pub byte_range: Range<usize>,
}

/// A Markdown heading.
#[derive(Debug, Clone)]
pub struct Heading {
    /// Heading depth, 1–6.
    pub level: u8,
    /// Heading text with inline markup stripped.
    pub text: String,
    /// Byte span in the original input.
    pub byte_range: Range<usize>,
    /// 1-based line number.
    pub line: usize,
}

/// A span of the body owned by one heading (or the preamble).
#[derive(Debug, Clone)]
pub struct Section {
    /// Index into [`MarkdownDoc::headings`]; `None` for pre-heading content.
    pub heading_index: Option<usize>,
    /// Byte span in the original input.
    pub byte_range: Range<usize>,
}

/// A fenced or indented code block.
#[derive(Debug, Clone)]
pub struct CodeBlock {
    /// Info-string language tag, if any.
    pub lang: Option<String>,
    /// Code block contents.
    pub text: String,
    /// Byte span in the original input.
    pub byte_range: Range<usize>,
    /// 1-based line number.
    pub line: usize,
}

/// A single list item (bullet or ordered), flattened.
#[derive(Debug, Clone)]
pub struct ListItem {
    /// Item text with inline markup stripped.
    pub text: String,
    /// Byte span in the original input.
    pub byte_range: Range<usize>,
    /// 1-based line number.
    pub line: usize,
}

/// A hyperlink discovered in the document (`[text](url)` or reference form).
#[derive(Debug, Clone)]
pub struct Link {
    /// Link destination as written.
    pub url: String,
    /// Link text.
    pub text: String,
    /// Byte span in the original input.
    pub byte_range: Range<usize>,
    /// 1-based line number.
    pub line: usize,
}

/// Parse `input` into a [`MarkdownDoc`].
pub fn parse(input: &str) -> MarkdownDoc {
    let (frontmatter, body_start) = split_frontmatter(input);
    let body = &input[body_start..];
    let line_starts = compute_line_starts(input);

    let mut doc = MarkdownDoc {
        frontmatter,
        headings: Vec::new(),
        sections: Vec::new(),
        code_blocks: Vec::new(),
        list_items: Vec::new(),
        links: Vec::new(),
    };

    let mut heading_stack: Vec<PendingHeading> = Vec::new();
    let mut code_stack: Vec<PendingCodeBlock> = Vec::new();
    let mut item_stack: Vec<PendingListItem> = Vec::new();
    let mut link_stack: Vec<PendingLink> = Vec::new();

    for (event, raw_range) in Parser::new_ext(body, Options::empty()).into_offset_iter() {
        let range = (raw_range.start + body_start)..(raw_range.end + body_start);
        match event {
            Event::Start(Tag::Heading { level, .. }) => {
                heading_stack.push(PendingHeading {
                    level: heading_level_u8(level),
                    text: String::new(),
                    start: range.start,
                });
            }
            Event::End(TagEnd::Heading(_)) => {
                if let Some(pending) = heading_stack.pop() {
                    let line = offset_to_line(&line_starts, pending.start);
                    doc.headings.push(Heading {
                        level: pending.level,
                        text: pending.text.trim().to_string(),
                        byte_range: pending.start..range.end,
                        line,
                    });
                }
            }
            Event::Start(Tag::CodeBlock(kind)) => {
                let lang = match kind {
                    CodeBlockKind::Fenced(l) => {
                        let s = l.to_string();
                        if s.is_empty() {
                            None
                        } else {
                            Some(s)
                        }
                    }
                    CodeBlockKind::Indented => None,
                };
                code_stack.push(PendingCodeBlock {
                    lang,
                    text: String::new(),
                    start: range.start,
                });
            }
            Event::End(TagEnd::CodeBlock) => {
                if let Some(pending) = code_stack.pop() {
                    let line = offset_to_line(&line_starts, pending.start);
                    doc.code_blocks.push(CodeBlock {
                        lang: pending.lang,
                        text: pending.text,
                        byte_range: pending.start..range.end,
                        line,
                    });
                }
            }
            Event::Start(Tag::Item) => {
                item_stack.push(PendingListItem {
                    text: String::new(),
                    start: range.start,
                });
            }
            Event::End(TagEnd::Item) => {
                if let Some(pending) = item_stack.pop() {
                    let line = offset_to_line(&line_starts, pending.start);
                    doc.list_items.push(ListItem {
                        text: pending.text.trim().to_string(),
                        byte_range: pending.start..range.end,
                        line,
                    });
                }
            }
            Event::Start(Tag::Link { dest_url, .. }) => {
                link_stack.push(PendingLink {
                    url: dest_url.to_string(),
                    text: String::new(),
                    start: range.start,
                });
            }
            Event::End(TagEnd::Link) => {
                if let Some(pending) = link_stack.pop() {
                    let line = offset_to_line(&line_starts, pending.start);
                    doc.links.push(Link {
                        url: pending.url,
                        text: pending.text.trim().to_string(),
                        byte_range: pending.start..range.end,
                        line,
                    });
                }
            }
            Event::Text(t) => {
                if let Some(c) = code_stack.last_mut() {
                    c.text.push_str(&t);
                } else {
                    if let Some(h) = heading_stack.last_mut() {
                        h.text.push_str(&t);
                    }
                    if let Some(li) = item_stack.last_mut() {
                        li.text.push_str(&t);
                    }
                    if let Some(lk) = link_stack.last_mut() {
                        lk.text.push_str(&t);
                    }
                }
            }
            Event::Code(t) => {
                if let Some(h) = heading_stack.last_mut() {
                    h.text.push_str(&t);
                }
                if let Some(li) = item_stack.last_mut() {
                    li.text.push_str(&t);
                }
                if let Some(lk) = link_stack.last_mut() {
                    lk.text.push_str(&t);
                }
            }
            _ => {}
        }
    }

    doc.sections = build_sections(&doc.headings, body_start, input.len());
    doc
}

struct PendingHeading {
    level: u8,
    text: String,
    start: usize,
}

struct PendingCodeBlock {
    lang: Option<String>,
    text: String,
    start: usize,
}

struct PendingListItem {
    text: String,
    start: usize,
}

struct PendingLink {
    url: String,
    text: String,
    start: usize,
}

fn heading_level_u8(level: HeadingLevel) -> u8 {
    match level {
        HeadingLevel::H1 => 1,
        HeadingLevel::H2 => 2,
        HeadingLevel::H3 => 3,
        HeadingLevel::H4 => 4,
        HeadingLevel::H5 => 5,
        HeadingLevel::H6 => 6,
    }
}

fn build_sections(headings: &[Heading], body_start: usize, file_end: usize) -> Vec<Section> {
    let mut sections = Vec::new();
    let first_start = headings
        .first()
        .map(|h| h.byte_range.start)
        .unwrap_or(file_end);
    if body_start < first_start {
        sections.push(Section {
            heading_index: None,
            byte_range: body_start..first_start,
        });
    }
    for (i, h) in headings.iter().enumerate() {
        let end = headings
            .get(i + 1)
            .map(|nh| nh.byte_range.start)
            .unwrap_or(file_end);
        sections.push(Section {
            heading_index: Some(i),
            byte_range: h.byte_range.end..end,
        });
    }
    sections
}

fn compute_line_starts(input: &str) -> Vec<usize> {
    let mut v = Vec::with_capacity(64);
    v.push(0);
    for (i, b) in input.bytes().enumerate() {
        if b == b'\n' {
            v.push(i + 1);
        }
    }
    v
}

fn offset_to_line(starts: &[usize], offset: usize) -> usize {
    match starts.binary_search(&offset) {
        Ok(i) => i + 1,
        Err(i) => i.max(1),
    }
}

fn split_frontmatter(input: &str) -> (Option<Frontmatter>, usize) {
    let bom_len = if input.starts_with('\u{FEFF}') {
        '\u{FEFF}'.len_utf8()
    } else {
        0
    };
    let after_bom = &input[bom_len..];

    let open_len = if after_bom.starts_with("---\r\n") {
        5
    } else if after_bom.starts_with("---\n") {
        4
    } else {
        return (None, 0);
    };

    let body_after_open = &after_bom[open_len..];
    let mut cursor = 0usize;
    while cursor <= body_after_open.len() {
        let rest = &body_after_open[cursor..];
        let (line, consumed) = match rest.find('\n') {
            Some(nl) => (&rest[..nl], nl + 1),
            None => (rest, rest.len()),
        };
        let content = line.strip_suffix('\r').unwrap_or(line);
        if content == "---" {
            let close_end = cursor + consumed;
            let raw_slice = &body_after_open[..cursor];
            let raw_trimmed = raw_slice.strip_suffix('\n').unwrap_or(raw_slice);
            let raw_trimmed = raw_trimmed.strip_suffix('\r').unwrap_or(raw_trimmed);
            let full_end = bom_len + open_len + close_end;
            return (
                Some(Frontmatter {
                    raw: raw_trimmed.to_string(),
                    byte_range: bom_len..full_end,
                }),
                full_end,
            );
        }
        if consumed == 0 {
            break;
        }
        cursor += consumed;
    }
    (None, 0)
}

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

    #[test]
    fn parses_frontmatter_when_present() {
        let src = "---\ntitle: Hi\nkey: val\n---\n# Heading\n";
        let doc = parse(src);
        let fm = doc.frontmatter.expect("frontmatter");
        assert_eq!(fm.raw, "title: Hi\nkey: val");
        assert_eq!(fm.byte_range.start, 0);
        assert_eq!(
            &src[fm.byte_range.clone()],
            "---\ntitle: Hi\nkey: val\n---\n"
        );
        assert_eq!(doc.headings.len(), 1);
        assert_eq!(doc.headings[0].text, "Heading");
    }

    #[test]
    fn no_frontmatter_when_missing_closing_fence() {
        let src = "---\ntitle: unterminated\n# Heading\n";
        let doc = parse(src);
        assert!(doc.frontmatter.is_none());
    }

    #[test]
    fn no_frontmatter_when_not_delimited() {
        let src = "# Heading\n\nBody text.\n";
        let doc = parse(src);
        assert!(doc.frontmatter.is_none());
        assert_eq!(doc.headings.len(), 1);
    }

    #[test]
    fn extracts_headings_with_line_numbers() {
        let src = "# One\n\n## Two\n\nBody\n\n### Three\n";
        let doc = parse(src);
        assert_eq!(doc.headings.len(), 3);
        assert_eq!(doc.headings[0].level, 1);
        assert_eq!(doc.headings[0].text, "One");
        assert_eq!(doc.headings[0].line, 1);
        assert_eq!(doc.headings[1].level, 2);
        assert_eq!(doc.headings[1].line, 3);
        assert_eq!(doc.headings[2].level, 3);
        assert_eq!(doc.headings[2].line, 7);
    }

    #[test]
    fn sections_span_between_headings() {
        let src = "prelude\n\n# One\nbody-one\n\n# Two\nbody-two\n";
        let doc = parse(src);
        assert_eq!(doc.sections.len(), 3);
        assert!(doc.sections[0].heading_index.is_none());
        assert_eq!(doc.sections[1].heading_index, Some(0));
        assert_eq!(doc.sections[2].heading_index, Some(1));
        let s1 = &src[doc.sections[1].byte_range.clone()];
        assert!(s1.contains("body-one"));
        assert!(!s1.contains("body-two"));
    }

    #[test]
    fn code_blocks_capture_language() {
        let src = "text\n\n```rust\nfn main() {}\n```\n\n```\nplain\n```\n";
        let doc = parse(src);
        assert_eq!(doc.code_blocks.len(), 2);
        assert_eq!(doc.code_blocks[0].lang.as_deref(), Some("rust"));
        assert!(doc.code_blocks[0].text.contains("fn main"));
        assert_eq!(doc.code_blocks[1].lang, None);
    }

    #[test]
    fn handles_crlf_line_endings() {
        let src = "---\r\ntitle: crlf\r\n---\r\n# Heading\r\n\r\n- item one\r\n- item two\r\n";
        let doc = parse(src);
        let fm = doc.frontmatter.expect("frontmatter");
        assert_eq!(fm.raw, "title: crlf");
        assert_eq!(doc.headings.len(), 1);
        assert_eq!(doc.headings[0].text, "Heading");
        assert_eq!(doc.list_items.len(), 2);
        assert_eq!(doc.list_items[0].text, "item one");
        assert_eq!(doc.list_items[1].text, "item two");
    }
}