nereid 0.9.0

Source-available noncommercial terminal diagram TUI and MCP server for Mermaid-backed sessions
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
// SPDX-FileCopyrightText: 2026 Bruno Meilick
// SPDX-License-Identifier: LicenseRef-Nereid-FreeUse-NoCopy-NoDerivatives
//
// All rights reserved.
//
// This file is part of Nereid and is proprietary software.
// Unauthorized copying, modification, or distribution is prohibited.

//! Coherent render pipeline: scene → layout family → paint.
//!
//! Kind-specific code stops at lowering ([`super::lower`]). This module owns family dispatch so
//! new diagram kinds do not grow parallel layout/render stacks.

use std::fmt;

use crate::layout::{
    layout_general_graph, layout_graph, layout_track, FlowchartLayoutError, SequenceLayoutError,
};
use crate::model::diagram::DiagramAst;
use crate::model::ids::DiagramId;

use super::flowchart::{
    render_flowchart_unicode_annotated_with_options, render_flowchart_unicode_with_options,
    FlowchartRenderError,
};
use super::lower::lower_diagram_ast;
use super::scene::{GraphModel, RenderScene, TrackModel};
use super::sequence::{
    render_sequence_unicode_annotated_with_options, render_sequence_unicode_with_options,
    SequenceRenderError,
};
use super::{AnnotatedRender, CanvasError, RenderOptions};

/// Layout or paint failure while rendering a lowered graph/track scene.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PipelineRenderError {
    SequenceLayout(SequenceLayoutError),
    FlowchartLayout(FlowchartLayoutError),
    SequenceRender(SequenceRenderError),
    FlowchartRender(FlowchartRenderError),
    Canvas(CanvasError),
}

impl fmt::Display for PipelineRenderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SequenceLayout(err) => write!(f, "sequence layout error: {err}"),
            Self::FlowchartLayout(err) => write!(f, "flowchart layout error: {err}"),
            Self::SequenceRender(err) => write!(f, "sequence render error: {err}"),
            Self::FlowchartRender(err) => write!(f, "flowchart render error: {err}"),
            Self::Canvas(err) => write!(f, "canvas error: {err}"),
        }
    }
}

impl std::error::Error for PipelineRenderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::SequenceLayout(err) => Some(err),
            Self::FlowchartLayout(err) => Some(err),
            Self::SequenceRender(err) => Some(err),
            Self::FlowchartRender(err) => Some(err),
            Self::Canvas(err) => Some(err),
        }
    }
}

impl From<CanvasError> for PipelineRenderError {
    fn from(value: CanvasError) -> Self {
        Self::Canvas(value)
    }
}

impl From<SequenceLayoutError> for PipelineRenderError {
    fn from(value: SequenceLayoutError) -> Self {
        Self::SequenceLayout(value)
    }
}

impl From<FlowchartLayoutError> for PipelineRenderError {
    fn from(value: FlowchartLayoutError) -> Self {
        Self::FlowchartLayout(value)
    }
}

impl From<SequenceRenderError> for PipelineRenderError {
    fn from(value: SequenceRenderError) -> Self {
        Self::SequenceRender(value)
    }
}

impl From<FlowchartRenderError> for PipelineRenderError {
    fn from(value: FlowchartRenderError) -> Self {
        Self::FlowchartRender(value)
    }
}

/// Layout + paint a graph-family scene (flow / class / er).
///
/// Uses [`layout_graph`] then bridges to the existing flowchart painter via
/// [`GraphModel::to_flowchart_ast`] (temporary until graph paint is scene-native).
///
/// Cap note: flowchart helpers still parse connector strings for glyphs; `GraphEdge` caps are
/// resolved at lower time for upcoming scene-native paint and must stay in sync with connectors.
pub fn render_graph_unicode_with_options(
    model: &GraphModel,
    options: RenderOptions,
) -> Result<String, PipelineRenderError> {
    let layout = layout_graph(model)?;
    if crate::render::graph_paint::graph_model_needs_scene_paint(model) {
        return Ok(crate::render::graph_paint::render_graph_model_with_compartments(
            model, &layout, options,
        )?);
    }
    let ast = model.to_flowchart_ast();
    Ok(render_flowchart_unicode_with_options(&ast, &layout, options)?)
}

/// Annotated graph-family render.
///
/// When `highlight_categories` is set (class/er scene paint), builds a real `HighlightIndex` for
/// TUI hints. Flow-bridge path ignores it and uses flowchart categories.
pub fn render_graph_unicode_annotated_with_options(
    diagram_id: &DiagramId,
    model: &GraphModel,
    options: RenderOptions,
) -> Result<AnnotatedRender, PipelineRenderError> {
    render_graph_unicode_annotated_with_categories(diagram_id, model, options, None)
}

/// Annotated graph paint with explicit node/edge category segments for scene-native diagrams.
///
/// When `highlight_categories` is `Some` (class/ER entry points), always uses scene-native paint
/// so dashed strokes and `class/*`/`er/*` refs are never lost to the flowchart bridge.
pub fn render_graph_unicode_annotated_with_categories(
    diagram_id: &DiagramId,
    model: &GraphModel,
    options: RenderOptions,
    highlight_categories: Option<crate::render::graph_paint::GraphHighlightCategories<'_>>,
) -> Result<AnnotatedRender, PipelineRenderError> {
    let layout = if highlight_categories.is_some() {
        layout_general_graph(model)?
    } else {
        layout_graph(model)?
    };
    if let Some(categories) = highlight_categories {
        return Ok(crate::render::graph_paint::render_graph_model_with_compartments_annotated(
            diagram_id, model, &layout, options, categories,
        )?);
    }
    if crate::render::graph_paint::graph_model_needs_scene_paint(model) {
        return Ok(crate::render::graph_paint::render_graph_model_with_compartments_annotated(
            diagram_id,
            model,
            &layout,
            options,
            crate::render::graph_paint::GraphHighlightCategories::FLOW,
        )?);
    }
    let ast = model.to_flowchart_ast();
    Ok(render_flowchart_unicode_annotated_with_options(diagram_id, &ast, &layout, options)?)
}

/// Class diagrams always use scene-native paint (dashed links, compartments, class/* hints).
pub fn render_class_unicode_with_options(
    model: &GraphModel,
    options: RenderOptions,
) -> Result<String, PipelineRenderError> {
    let layout = layout_general_graph(model)?;
    Ok(crate::render::graph_paint::render_graph_model_with_compartments(model, &layout, options)?)
}

/// ER diagrams always use scene-native paint (cardinality caps, er/* hints).
pub fn render_er_unicode_with_options(
    model: &GraphModel,
    options: RenderOptions,
) -> Result<String, PipelineRenderError> {
    let layout = layout_general_graph(model)?;
    Ok(crate::render::graph_paint::render_graph_model_with_compartments(model, &layout, options)?)
}

/// Layout + paint a track-family scene (sequence / gantt).
pub fn render_track_unicode_with_options(
    model: &TrackModel,
    options: RenderOptions,
) -> Result<String, PipelineRenderError> {
    if let Some(gantt) = model.as_gantt_ast() {
        return Ok(crate::render::track_paint::render_gantt_unicode(gantt, options)?);
    }
    let layout = layout_track(model)?;
    let ast = model.as_sequence_ast();
    Ok(render_sequence_unicode_with_options(ast, &layout, options)?)
}

/// Annotated track-family render.
pub fn render_track_unicode_annotated_with_options(
    diagram_id: &DiagramId,
    model: &TrackModel,
    options: RenderOptions,
) -> Result<AnnotatedRender, PipelineRenderError> {
    if let Some(gantt) = model.as_gantt_ast() {
        return Ok(crate::render::track_paint::render_gantt_unicode_annotated(
            diagram_id, gantt, options,
        )?);
    }
    let layout = layout_track(model)?;
    let ast = model.as_sequence_ast();
    Ok(render_sequence_unicode_annotated_with_options(diagram_id, ast, &layout, options)?)
}

/// Family-dispatched paint for a lowered scene.
pub fn render_scene_unicode_with_options(
    scene: &RenderScene,
    options: RenderOptions,
) -> Result<String, PipelineRenderError> {
    match scene {
        RenderScene::Graph(model) => render_graph_unicode_with_options(model, options),
        RenderScene::Track(model) => render_track_unicode_with_options(model, options),
    }
}

/// Family-dispatched annotated paint for a lowered scene.
pub fn render_scene_unicode_annotated_with_options(
    diagram_id: &DiagramId,
    scene: &RenderScene,
    options: RenderOptions,
) -> Result<AnnotatedRender, PipelineRenderError> {
    match scene {
        RenderScene::Graph(model) => {
            render_graph_unicode_annotated_with_options(diagram_id, model, options)
        }
        RenderScene::Track(model) => {
            render_track_unicode_annotated_with_options(diagram_id, model, options)
        }
    }
}

/// Lower a domain AST then render through the coherent pipeline.
pub fn render_ast_unicode_with_options(
    ast: &DiagramAst,
    options: RenderOptions,
) -> Result<String, PipelineRenderError> {
    match ast {
        // Always scene-native so dashed links and non-flow caps cannot fall through to flowchart.
        DiagramAst::Class(class) => {
            let model = crate::render::lower::lower_class(class);
            render_class_unicode_with_options(&model, options)
        }
        DiagramAst::Er(er) => {
            let model = crate::render::lower::lower_er(er);
            render_er_unicode_with_options(&model, options)
        }
        other => {
            let scene = lower_diagram_ast(other);
            render_scene_unicode_with_options(&scene, options)
        }
    }
}

/// Lower a domain AST then annotated-render through the coherent pipeline.
pub fn render_ast_unicode_annotated_with_options(
    diagram_id: &DiagramId,
    ast: &DiagramAst,
    options: RenderOptions,
) -> Result<AnnotatedRender, PipelineRenderError> {
    use crate::render::graph_paint::GraphHighlightCategories;

    match ast {
        DiagramAst::Class(class) => {
            let model = crate::render::lower::lower_class(class);
            render_graph_unicode_annotated_with_categories(
                diagram_id,
                &model,
                options,
                Some(GraphHighlightCategories::CLASS),
            )
        }
        DiagramAst::Er(er) => {
            let model = crate::render::lower::lower_er(er);
            render_graph_unicode_annotated_with_categories(
                diagram_id,
                &model,
                options,
                Some(GraphHighlightCategories::ER),
            )
        }
        other => {
            let scene = lower_diagram_ast(other);
            render_scene_unicode_annotated_with_options(diagram_id, &scene, options)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layout::{layout_flowchart, layout_sequence};
    use crate::model::fixtures::flowchart_small_dag;
    use crate::model::ids::ObjectId;
    use crate::model::seq_ast::{
        SequenceAst, SequenceMessage, SequenceMessageKind, SequenceParticipant,
    };
    use crate::model::{Diagram, DiagramAst, DiagramId};
    use crate::render::flowchart::render_flowchart_unicode;
    use crate::render::sequence::render_sequence_unicode;

    fn oid(value: &str) -> ObjectId {
        ObjectId::new(value).expect("object id")
    }

    #[test]
    fn pipeline_flowchart_matches_direct_render() {
        let ast = flowchart_small_dag();
        let layout = layout_flowchart(&ast).expect("layout");
        let direct = render_flowchart_unicode(&ast, &layout).expect("direct");
        let via =
            render_ast_unicode_with_options(&DiagramAst::Flowchart(ast), RenderOptions::default())
                .expect("pipeline");
        assert_eq!(via, direct);
    }

    #[test]
    fn pipeline_sequence_matches_direct_render() {
        let mut ast = SequenceAst::default();
        let p_alice = oid("p:alice");
        let p_bob = oid("p:bob");
        ast.participants_mut().insert(p_alice.clone(), SequenceParticipant::new("Alice"));
        ast.participants_mut().insert(p_bob.clone(), SequenceParticipant::new("Bob"));
        ast.messages_mut().push(SequenceMessage::new(
            oid("m:0001"),
            p_alice,
            p_bob,
            SequenceMessageKind::Sync,
            "Hello",
            1000,
        ));

        let layout = layout_sequence(&ast).expect("layout");
        let direct = render_sequence_unicode(&ast, &layout).expect("direct");
        let via =
            render_ast_unicode_with_options(&DiagramAst::Sequence(ast), RenderOptions::default())
                .expect("pipeline");
        assert_eq!(via, direct);
    }

    #[test]
    fn both_families_enter_shared_pipeline_entry() {
        let flow = DiagramAst::Flowchart(flowchart_small_dag());
        let mut seq = SequenceAst::default();
        seq.participants_mut().insert(oid("p:a"), SequenceParticipant::new("A"));
        let seq = DiagramAst::Sequence(seq);

        let flow_scene = lower_diagram_ast(&flow);
        let seq_scene = lower_diagram_ast(&seq);
        assert_eq!(flow_scene.family_name(), "graph");
        assert_eq!(seq_scene.family_name(), "track");

        let _ = render_scene_unicode_with_options(&flow_scene, RenderOptions::default())
            .expect("graph paint");
        let _ = render_scene_unicode_with_options(&seq_scene, RenderOptions::default())
            .expect("track paint");
    }

    #[test]
    fn diagram_dispatch_uses_pipeline_parity() {
        let flow_ast = flowchart_small_dag();
        let diagram_id = DiagramId::new("d-flow").expect("id");
        let diagram = Diagram::new(diagram_id, "Example", DiagramAst::Flowchart(flow_ast.clone()));

        let via_diagram = crate::render::render_diagram_unicode(&diagram).expect("diagram");
        let via_pipeline = render_ast_unicode_with_options(
            &DiagramAst::Flowchart(flow_ast),
            RenderOptions::default(),
        )
        .expect("pipeline");
        assert_eq!(via_diagram, via_pipeline);
    }

    #[test]
    fn class_and_er_render_accept_cycles_and_self_relations() {
        let class = crate::format::mermaid::parse_class_diagram(
            "classDiagram\nA --> B\nB --> C\nC --> A\nA --> A : recursive\n",
        )
        .expect("class parse");
        let class_text =
            render_ast_unicode_with_options(&DiagramAst::Class(class), RenderOptions::default())
                .expect("cyclic class render");
        assert!(class_text.contains('A'));
        assert!(class_text.contains("recursive"), "{class_text}");

        let er = crate::format::mermaid::parse_er_diagram(
            "erDiagram\nA ||--o{ B\nB ||--o{ C\nC ||--o{ A\nA ||--|| A : self\n",
        )
        .expect("er parse");
        let er_text =
            render_ast_unicode_with_options(&DiagramAst::Er(er), RenderOptions::default())
                .expect("cyclic er render");
        assert!(er_text.contains('A'));
        assert!(er_text.contains("self"), "{er_text}");
    }

    #[test]
    fn layout_graph_matches_layout_flowchart() {
        let ast = flowchart_small_dag();
        let direct = layout_flowchart(&ast).expect("direct layout");
        let via = layout_graph(&crate::render::lower_flowchart(&ast)).expect("graph layout");
        assert_eq!(via, direct);
    }

    #[test]
    fn pipeline_annotated_flowchart_matches_direct() {
        use crate::render::flowchart::render_flowchart_unicode_annotated;
        let ast = flowchart_small_dag();
        let diagram_id = DiagramId::new("d-ann").expect("id");
        let layout = layout_flowchart(&ast).expect("layout");
        let direct =
            render_flowchart_unicode_annotated(&diagram_id, &ast, &layout).expect("direct");
        let via = render_ast_unicode_annotated_with_options(
            &diagram_id,
            &DiagramAst::Flowchart(ast),
            RenderOptions::default(),
        )
        .expect("pipeline");
        assert_eq!(via.text, direct.text);
        assert_eq!(via.highlight_index, direct.highlight_index);
    }

    #[test]
    fn pipeline_dual_cap_connector_matches_direct() {
        use crate::model::flow_ast::{FlowEdge, FlowNode};
        let mut ast = crate::model::flow_ast::FlowchartAst::default();
        let a = oid("n:a");
        let b = oid("n:b");
        ast.nodes_mut().insert(a.clone(), FlowNode::new("A"));
        ast.nodes_mut().insert(b.clone(), FlowNode::new("B"));
        let mut edge = FlowEdge::new(a, b);
        edge.set_connector(Some("<-->".to_owned()));
        ast.edges_mut().insert(oid("e:1"), edge);

        let layout = layout_flowchart(&ast).expect("layout");
        let direct = render_flowchart_unicode(&ast, &layout).expect("direct");
        let via =
            render_ast_unicode_with_options(&DiagramAst::Flowchart(ast), RenderOptions::default())
                .expect("pipeline");
        assert_eq!(via, direct);
        assert!(via.contains('') || via.contains(''), "expected dual caps:\n{via}");
    }

    #[test]
    fn class_diagram_renders_compartments_via_pipeline() {
        let input = r#"
classDiagram
Class01 <|-- AveryLongClass : Cool
Class01 : size()
Class01 : int chimp
Class03 *-- Class04
"#;
        let ast = crate::format::mermaid::parse_class_diagram(input).expect("parse");
        let text =
            render_ast_unicode_with_options(&DiagramAst::Class(ast), RenderOptions::default())
                .expect("render");
        assert!(text.contains("Class01"), "{text}");
        assert!(text.contains("int chimp") || text.contains("size()"), "{text}");
        assert!(text.contains('') || text.contains(''), "expected class box:\n{text}");
    }

    #[test]
    fn pipeline_notes_on_matches_direct() {
        use crate::model::flow_ast::FlowNode;
        let mut ast = crate::model::flow_ast::FlowchartAst::default();
        let a = oid("n:a");
        let mut node = FlowNode::new("A");
        node.set_note(Some("note".to_owned()));
        ast.nodes_mut().insert(a, node);
        let options = RenderOptions { show_notes: true, ..RenderOptions::default() };
        let layout = layout_flowchart(&ast).expect("layout");
        let direct =
            crate::render::flowchart::render_flowchart_unicode_with_options(&ast, &layout, options)
                .expect("direct");
        let via = render_ast_unicode_with_options(&DiagramAst::Flowchart(ast), options)
            .expect("pipeline");
        assert_eq!(via, direct);
        // Note may be ellipsis-truncated inside a narrow box ("no…").
        assert!(
            via.lines().any(|line| line.contains('n') && line.contains('')),
            "expected note row inside box:\n{via}"
        );
    }
}