Skip to main content

agm_core/renderer/
mod.rs

1//! Renderer: converts AST (or filtered views) to output formats.
2//!
3//! Supported formats: JSON, JSON canonical (S37), Markdown, canonical AGM
4//! text, and graph visualization (DOT/Mermaid).
5
6pub mod canonical;
7pub mod graph;
8pub mod json;
9pub mod json_canonical;
10pub mod markdown;
11pub mod mem;
12pub mod state;
13
14use crate::model::file::AgmFile;
15
16// ---------------------------------------------------------------------------
17// RenderError
18// ---------------------------------------------------------------------------
19
20/// Errors produced by the fallible [`json_canonical::json_to_agm`] function.
21#[derive(Debug, Clone, PartialEq, thiserror::Error)]
22pub enum RenderError {
23    #[error("missing required field: {field}")]
24    MissingField { field: String },
25
26    #[error("invalid field type for '{field}': expected {expected}, got {actual}")]
27    InvalidType {
28        field: String,
29        expected: String,
30        actual: String,
31    },
32
33    #[error("invalid enum value for '{field}': {value}")]
34    InvalidEnumValue { field: String, value: String },
35
36    #[error("JSON deserialization error: {0}")]
37    JsonError(String),
38}
39
40// ---------------------------------------------------------------------------
41// RenderFormat
42// ---------------------------------------------------------------------------
43
44/// Output format selector for the top-level [`render`] function.
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum RenderFormat {
47    Json,
48    JsonCanonical,
49    Markdown,
50    Canonical,
51    Dot,
52    Mermaid,
53}
54
55// ---------------------------------------------------------------------------
56// render
57// ---------------------------------------------------------------------------
58
59/// Renders an `AgmFile` in the requested format.
60///
61/// All variants are infallible — they operate on a valid AST and produce
62/// best-effort output.
63#[must_use]
64pub fn render(file: &AgmFile, format: RenderFormat) -> String {
65    match format {
66        RenderFormat::Json => json::render_json(file),
67        RenderFormat::JsonCanonical => json_canonical::render_json_canonical(file),
68        RenderFormat::Markdown => markdown::render_markdown(file),
69        RenderFormat::Canonical => canonical::render_canonical(file),
70        RenderFormat::Dot => graph::render_graph_dot(file),
71        RenderFormat::Mermaid => graph::render_graph_mermaid(file),
72    }
73}