Skip to main content

intent_render/
markdown.rs

1//! Render a parsed intent specification as Markdown.
2//!
3//! Produces clean, readable documentation suitable for review
4//! by non-engineers (PMs, designers, stakeholders).
5
6use intent_parser::ast;
7
8use crate::format_type;
9
10/// Render an AST [`File`] to a Markdown string.
11pub fn render(file: &ast::File) -> String {
12    let mut out = String::new();
13    out.push_str(&format!("# {}\n\n", file.module.name));
14
15    if let Some(doc) = &file.doc {
16        for line in &doc.lines {
17            out.push_str(line);
18            out.push('\n');
19        }
20        out.push('\n');
21    }
22
23    if !file.imports.is_empty() {
24        out.push_str("**Imports:**\n\n");
25        for use_decl in &file.imports {
26            if let Some(item) = &use_decl.item {
27                out.push_str(&format!("- `{}.{}`\n", use_decl.module_name, item));
28            } else {
29                out.push_str(&format!("- `{}`\n", use_decl.module_name));
30            }
31        }
32        out.push('\n');
33    }
34
35    for item in &file.items {
36        match item {
37            ast::TopLevelItem::Entity(e) => render_entity(&mut out, e),
38            ast::TopLevelItem::Action(a) => render_action(&mut out, a),
39            ast::TopLevelItem::Invariant(i) => render_invariant(&mut out, i),
40            ast::TopLevelItem::EdgeCases(ec) => render_edge_cases(&mut out, ec),
41        }
42    }
43
44    out
45}
46
47fn render_entity(out: &mut String, entity: &ast::EntityDecl) {
48    out.push_str(&format!("## Entity: {}\n\n", entity.name));
49    if let Some(doc) = &entity.doc {
50        for line in &doc.lines {
51            out.push_str(line);
52            out.push('\n');
53        }
54        out.push('\n');
55    }
56    out.push_str("| Field | Type |\n|-------|------|\n");
57    for field in &entity.fields {
58        out.push_str(&format!(
59            "| `{}` | `{}` |\n",
60            field.name,
61            format_type(&field.ty)
62        ));
63    }
64    out.push('\n');
65}
66
67fn render_action(out: &mut String, action: &ast::ActionDecl) {
68    out.push_str(&format!("## Action: {}\n\n", action.name));
69    if let Some(doc) = &action.doc {
70        for line in &doc.lines {
71            out.push_str(line);
72            out.push('\n');
73        }
74        out.push('\n');
75    }
76    if !action.params.is_empty() {
77        out.push_str("**Parameters:**\n\n");
78        for p in &action.params {
79            out.push_str(&format!("- `{}`: `{}`\n", p.name, format_type(&p.ty)));
80        }
81        out.push('\n');
82    }
83}
84
85fn render_invariant(out: &mut String, inv: &ast::InvariantDecl) {
86    out.push_str(&format!("## Invariant: {}\n\n", inv.name));
87    if let Some(doc) = &inv.doc {
88        for line in &doc.lines {
89            out.push_str(line);
90            out.push('\n');
91        }
92        out.push('\n');
93    }
94}
95
96fn render_edge_cases(out: &mut String, _ec: &ast::EdgeCasesDecl) {
97    out.push_str("## Edge Cases\n\n");
98}