mmdflux 2.3.0

Render Mermaid diagrams as Unicode text, ASCII, SVG, and MMDS JSON.
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
//! Graph-family geometry IR contracts.
//!
//! Two-layer float-coordinate geometry produced by layout engines and
//! consumed by routing and downstream output stages. Engine-agnostic core with optional
//! engine-specific hint channels.

#![allow(dead_code)]

use std::collections::{HashMap, HashSet};

use crate::errors::RenderError;
use crate::format::normalize_enum_token;
pub use crate::graph::attachment::EdgePort;
use crate::graph::projection::GridProjection;
pub use crate::graph::space::{FPoint, FRect};
use crate::graph::{Direction, Shape};

/// Requested graph-geometry detail level for downstream emitters and exports.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GeometryLevel {
    /// Node geometry + edge topology only (no edge paths).
    #[default]
    Layout,
    /// Full geometry including routed edge paths.
    Routed,
}

impl std::fmt::Display for GeometryLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GeometryLevel::Layout => write!(f, "layout"),
            GeometryLevel::Routed => write!(f, "routed"),
        }
    }
}

impl GeometryLevel {
    /// Parse a geometry level from user-provided text.
    pub fn parse(s: &str) -> Result<Self, RenderError> {
        match normalize_enum_token(s).as_str() {
            "layout" => Ok(GeometryLevel::Layout),
            "routed" => Ok(GeometryLevel::Routed),
            _ => Err(RenderError {
                message: format!("unknown geometry level: {s:?}"),
            }),
        }
    }
}

impl std::str::FromStr for GeometryLevel {
    type Err = RenderError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

// ---------------------------------------------------------------------------
// Layer 1: GraphGeometry (layout output → routing input)
// ---------------------------------------------------------------------------

/// Positioned graph geometry in float coordinate space.
///
/// Produced by layout engines via normalization adapters,
/// consumed by routing and shared policy functions.
#[derive(Debug, Clone)]
pub struct GraphGeometry {
    /// Positioned nodes with bounding rects.
    pub nodes: HashMap<String, PositionedNode>,
    /// Edge routing hints from layout (waypoints, label positions).
    pub edges: Vec<LayoutEdge>,
    /// Subgraph bounding boxes.
    pub subgraphs: HashMap<String, SubgraphGeometry>,
    /// Self-edge loop geometry.
    pub self_edges: Vec<SelfEdgeGeometry>,
    /// Root layout direction.
    pub direction: Direction,
    /// Per-node effective direction (accounting for subgraph overrides).
    pub node_directions: HashMap<String, Direction>,
    /// Total layout bounding box.
    pub bounds: FRect,
    /// Which edge indices were reversed for cycle removal.
    pub reversed_edges: Vec<usize>,
    /// Optional engine-specific metadata for grid-snap and rank-aware routing.
    pub engine_hints: Option<EngineHints>,
    /// Optional graph-owned replay metadata for projecting float geometry onto a discrete grid.
    pub grid_projection: Option<GridProjection>,
    /// Edge indices rerouted by the layout engine (e.g., direction-override subgraph edges).
    /// Populated by engines that perform float-space subgraph post-processing.
    /// Used by downstream emitters to preserve explicit endpoint geometry.
    pub rerouted_edges: HashSet<usize>,
    /// Whether enhanced backward edge routing should be applied.
    /// Set by engines that use layout quality enhancements (e.g., flux-layered).
    pub enhanced_backward_routing: bool,
}

/// A positioned node with its bounding rect and shape.
#[derive(Debug, Clone)]
pub struct PositionedNode {
    pub id: String,
    /// Bounding rect in layout float space (x,y = center).
    pub rect: FRect,
    pub shape: Shape,
    pub label: String,
    pub parent: Option<String>,
}

/// An edge with layout-computed routing hints.
#[derive(Debug, Clone)]
pub struct LayoutEdge {
    /// Index into the diagram's edge list (for metadata lookup).
    pub index: usize,
    pub from: String,
    pub to: String,
    /// Waypoint positions from layout engine.
    pub waypoints: Vec<FPoint>,
    /// Label position computed by layout engine.
    pub label_position: Option<FPoint>,
    /// Label side (Above/Below/Center) from side selection.
    pub label_side: Option<EdgeLabelSide>,
    /// If source is a subgraph-as-node, the subgraph ID.
    pub from_subgraph: Option<String>,
    /// If target is a subgraph-as-node, the subgraph ID.
    pub to_subgraph: Option<String>,
    /// Optional complete path from engines that provide full routing (e.g. ELK).
    pub layout_path_hint: Option<Vec<FPoint>>,
    /// Preserve the explicit orthogonal topology instead of simplifying it away.
    /// Used when routing introduced a deliberate de-overlap corridor.
    pub preserve_orthogonal_topology: bool,
    /// Shared label-rectangle geometry (populated by the routing label-lane
    /// pass and copied back onto `LayoutEdge` so `Visual` SVG solve paths
    /// still see authoritative label rectangles). `None` before routing.
    pub label_geometry: Option<EdgeLabelGeometry>,
    /// Plan 0149 (#237): lane-aware re-wrap output, forwarded from
    /// `RoutedEdgeGeometry::effective_wrapped_lines` through
    /// `geometry_for_routed_svg` so the SVG renderer (which consumes
    /// `LayoutEdge`, not `RoutedEdgeGeometry`) can emit text matching
    /// the post-rewrap rect. `None` means "use the pre-engine wrap
    /// artifact from `diagram.edges[idx].wrapped_label_lines`".
    ///
    /// Only populated on the routed-SVG downgrade path; the pre-routing
    /// `LayoutEdge` produced by the kernel always carries `None` here.
    pub effective_wrapped_lines: Option<Vec<String>>,
}

/// Subgraph bounding box in layout float space.
#[derive(Debug, Clone)]
pub struct SubgraphGeometry {
    pub id: String,
    /// Bounding rect (x,y = center for layered-style, or top-left for others).
    pub rect: FRect,
    pub title: String,
    pub depth: usize,
}

/// Self-edge loop geometry.
#[derive(Debug, Clone)]
pub struct SelfEdgeGeometry {
    pub node_id: String,
    pub edge_index: usize,
    pub points: Vec<FPoint>,
}

/// Label side for positioned and routed edges.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EdgeLabelSide {
    Above,
    Below,
    #[default]
    Center,
}

/// Shared label-rectangle geometry carried on both `LayoutEdge` (for the
/// `Visual` SVG solve path) and `RoutedEdgeGeometry` (for MMDS/SVG/bounds
/// consumers after routing). Populated exactly once by the label-lane pass
/// and read-only downstream — every consumer sees the same rectangle and
/// center. See plan 0145 architecture design §3.1 for invariants.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EdgeLabelGeometry {
    /// Midpoint of `rect`. Consumers may use either; no drift.
    pub center: FPoint,
    /// Padded label rectangle (includes `padding.0` on each side in X and
    /// `padding.1` on each side in Y).
    pub rect: FRect,
    /// Snapshot of `(metrics.label_padding_x, metrics.label_padding_y)` at
    /// construction. Consumers that need unpadded dimensions can subtract.
    pub padding: (f64, f64),
    /// Side of the edge this label is placed on.
    pub side: EdgeLabelSide,
    /// Signed lane-assignment track. `0` means no lane displacement was
    /// applied; `±1, ±2, ...` encode lane rank (sign encodes direction).
    pub track: i32,
    /// Number of members in the lane-assignment compartment this edge was
    /// placed in. `1` means singleton (no compartment coordination); `≥2`
    /// means the label was placed as part of a multi-edge group and
    /// consumers must trust `center` / `rect` exactly — they were chosen
    /// relative to the compartment's shared anchor and may sit off the
    /// edge's own arc-length midpoint by more than a revalidation
    /// tolerance. Pre-lane placeholders use the `Default` value of `1` so
    /// singleton behavior is preserved when no lane pass has run.
    pub compartment_size: usize,
}

impl Default for EdgeLabelGeometry {
    fn default() -> Self {
        Self {
            center: FPoint::new(0.0, 0.0),
            rect: FRect::new(0.0, 0.0, 0.0, 0.0),
            padding: (0.0, 0.0),
            side: EdgeLabelSide::Center,
            track: 0,
            compartment_size: 1,
        }
    }
}

// ---------------------------------------------------------------------------
// Engine hints (optional engine-specific metadata for grid-snap and routing)
// ---------------------------------------------------------------------------

/// Engine-specific metadata channel.
///
/// Layout engines attach algorithm-specific hints that downstream stages
/// (grid projection, routing, rendering) can consume.  The outer enum
/// discriminates by engine family; the inner struct carries the payload.
#[derive(Debug, Clone)]
pub enum EngineHints {
    /// Hints produced by layered (Sugiyama-family) engines.
    Layered(LayeredHints),
}

/// Layered-layout-specific metadata needed during migration.
///
/// Preserves rank-annotated data from the layered layout that grid replay
/// needs for coordinate transformation. Other engines won't populate this.
#[derive(Debug, Clone)]
pub struct LayeredHints {
    /// Per-node rank assignments (node_id → rank).
    pub node_ranks: HashMap<String, i32>,
    /// Rank → (primary_start, primary_end) coordinates in layout float space.
    /// Primary axis is Y for TD/BT, X for LR/RL.
    pub rank_to_position: HashMap<i32, (f64, f64)>,
    /// Waypoints with rank info for grid-snap transformation.
    /// Key: edge index, Value: list of (position, rank) pairs.
    pub edge_waypoints: HashMap<usize, Vec<(FPoint, i32)>>,
    /// Label positions with rank info for grid-snap transformation.
    /// Key: edge index, Value: (position, rank).
    pub label_positions: HashMap<usize, (FPoint, i32)>,
}

// ---------------------------------------------------------------------------
// Layer 2: RoutedGraphGeometry (routing output → downstream emitter input)
// ---------------------------------------------------------------------------

/// Graph geometry with fully-routed edge paths.
///
/// Produced by the routing stage, consumed by downstream emitters.
#[derive(Debug, Clone)]
pub struct RoutedGraphGeometry {
    /// Same positioned nodes as input.
    pub nodes: HashMap<String, PositionedNode>,
    /// Fully-routed edges with polyline paths.
    pub edges: Vec<RoutedEdgeGeometry>,
    /// Subgraph bounds (may differ from layout bounds after routing adjustments).
    pub subgraphs: HashMap<String, SubgraphGeometry>,
    /// Routed self-edge paths.
    pub self_edges: Vec<RoutedSelfEdge>,
    /// Root direction.
    pub direction: Direction,
    /// Total bounds.
    pub bounds: FRect,
    /// Edges whose label rect could not fit in the gap between source and
    /// target node faces (after marker avoidance). Populated by the routing
    /// label-clamp pass in `route_graph_geometry`. **Empty in all healthy
    /// layouts.**
    ///
    /// Plan 0146 Task 2.1: this field is populated unconditionally in **all**
    /// builds (no `cfg(test)`, no env-var gating). Consumers may inspect it
    /// to surface a warning. Not serialized in MMDS layout output (it is a
    /// routing diagnostic, not part of the layout contract); MMDS adds it
    /// under a separate optional `diagnostics` object — see
    /// `mmds::output::serialize_routed_graph_with_diagnostics` (Task 2.3).
    pub unfit_label_overlaps: Vec<UnfitOverlap>,
}

/// One label that the clamp pass could not fit in the available gap
/// between source and target node faces (after marker avoidance).
///
/// Populated on `RoutedGraphGeometry::unfit_label_overlaps`. Read-only
/// after routing.
#[derive(Debug, Clone, PartialEq)]
pub struct UnfitOverlap {
    /// Index of the offending edge in the original `Graph::edges` list.
    pub edge_index: usize,
    /// Authored label text. Empty string for edges that have a label
    /// position but no label string (should not normally happen).
    pub label: String,
    /// Available gap between visual source's far face and visual target's
    /// near face along the edge-parallel axis, after marker avoidance.
    /// Negative values indicate marker avoidance + spacing already exceeds
    /// the inter-node distance.
    pub gap_pixels: f64,
    /// Label rect dimension along the edge-parallel axis.
    pub label_span_pixels: f64,
    /// The side the engine attempted to place the label on.
    pub attempted_side: EdgeLabelSide,
}

/// A fully-routed edge with polyline path.
#[derive(Debug, Clone)]
pub struct RoutedEdgeGeometry {
    pub index: usize,
    pub from: String,
    pub to: String,
    /// Polyline path in float coordinates.
    pub path: Vec<FPoint>,
    /// Label center position.
    pub label_position: Option<FPoint>,
    /// Label side (Above/Below/Center) from side selection.
    pub label_side: Option<EdgeLabelSide>,
    /// Label position near the target endpoint (head).
    pub head_label_position: Option<FPoint>,
    /// Label position near the source endpoint (tail).
    pub tail_label_position: Option<FPoint>,
    /// Whether this edge flows backward in the layout direction.
    pub is_backward: bool,
    /// If source is a subgraph-as-node, the subgraph ID.
    pub from_subgraph: Option<String>,
    /// If target is a subgraph-as-node, the subgraph ID.
    pub to_subgraph: Option<String>,
    /// Port attachment at the source node.
    pub source_port: Option<EdgePort>,
    /// Port attachment at the target node.
    pub target_port: Option<EdgePort>,
    /// Preserve the explicit orthogonal topology instead of simplifying it away.
    /// Set when routing introduced a deliberate de-overlap corridor.
    pub preserve_orthogonal_topology: bool,
    /// Shared label-rectangle geometry populated by the routing label-lane
    /// pass. Read by SVG/MMDS/bounds consumers; one rectangle, no divergence.
    pub label_geometry: Option<EdgeLabelGeometry>,
    /// Plan 0149 (#237): lane-aware re-wrap output. `Some(lines)` when the
    /// post-lane `label_rewrap` pass decided to narrow this edge's label
    /// below the pre-engine wrap width so it fits the compartment's
    /// `label_step` budget. Renderers (SVG `labels.rs`, text `edge.rs`,
    /// MMDS replay) must prefer this over `diagram.edges[idx].
    /// wrapped_label_lines` when present so the rect geometry and the
    /// emitted text stay in sync.
    ///
    /// Intentionally NOT propagated back to `Graph::edges.wrapped_label_lines`
    /// so the kernel's Grid measurement (which ran pre-routing) stays on the
    /// original pre-engine wrap. That's what keeps Text snapshots
    /// byte-stable — Grid-measured layer heights were committed before this
    /// field existed, and Text consumers that honor a routed override need
    /// to opt in explicitly. See `findings/01-spike-result.md` §1.2.
    pub effective_wrapped_lines: Option<Vec<String>>,
}

/// A routed self-edge loop.
#[derive(Debug, Clone)]
pub struct RoutedSelfEdge {
    pub node_id: String,
    pub edge_index: usize,
    pub path: Vec<FPoint>,
}

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

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

    #[test]
    fn graph_geometry_default_construction() {
        let geo = GraphGeometry {
            nodes: HashMap::new(),
            edges: Vec::new(),
            subgraphs: HashMap::new(),
            self_edges: Vec::new(),
            direction: Direction::TopDown,
            node_directions: HashMap::new(),
            bounds: FRect::new(0.0, 0.0, 0.0, 0.0),
            reversed_edges: Vec::new(),
            engine_hints: None,
            grid_projection: None,
            rerouted_edges: HashSet::new(),
            enhanced_backward_routing: false,
        };
        assert!(geo.nodes.is_empty());
        assert!(geo.edges.is_empty());
        assert!(geo.engine_hints.is_none());
        assert!(geo.grid_projection.is_none());
    }

    #[test]
    fn engine_hints_layered_construction() {
        let hints = EngineHints::Layered(LayeredHints {
            node_ranks: HashMap::new(),
            rank_to_position: HashMap::new(),
            edge_waypoints: HashMap::new(),
            label_positions: HashMap::new(),
        });
        let EngineHints::Layered(inner) = &hints;
        assert!(inner.node_ranks.is_empty());
    }

    #[test]
    fn layout_edge_path_hint_optional() {
        let edge = LayoutEdge {
            index: 0,
            from: "A".into(),
            to: "B".into(),
            waypoints: vec![FPoint::new(1.0, 2.0)],
            label_position: None,
            label_side: None,
            from_subgraph: None,
            to_subgraph: None,
            layout_path_hint: None,
            preserve_orthogonal_topology: false,
            label_geometry: None,
            effective_wrapped_lines: None,
        };
        assert!(edge.layout_path_hint.is_none());
        assert_eq!(edge.waypoints.len(), 1);
    }

    #[test]
    fn edge_label_geometry_constructs_with_center_rect_padding_side_track() {
        let g = EdgeLabelGeometry {
            center: FPoint::new(10.0, 20.0),
            rect: FRect::new(0.0, 10.0, 20.0, 20.0),
            padding: (4.0, 2.0),
            side: EdgeLabelSide::Above,
            track: 1,
            compartment_size: 1,
        };
        assert_eq!(g.center.x, 10.0);
        assert_eq!(g.center.y, 20.0);
        assert_eq!(g.rect.width, 20.0);
        assert_eq!(g.track, 1);
    }

    #[test]
    fn layout_edge_carries_label_geometry_none_by_default() {
        let edge = LayoutEdge {
            index: 0,
            from: "A".into(),
            to: "B".into(),
            waypoints: vec![],
            label_position: None,
            label_side: None,
            from_subgraph: None,
            to_subgraph: None,
            layout_path_hint: None,
            preserve_orthogonal_topology: false,
            label_geometry: None,
            effective_wrapped_lines: None,
        };
        assert!(edge.label_geometry.is_none());
    }

    #[test]
    fn routed_edge_geometry_carries_label_geometry_none_by_default() {
        let edge = RoutedEdgeGeometry {
            index: 0,
            from: "A".into(),
            to: "B".into(),
            path: vec![],
            label_position: None,
            label_side: None,
            head_label_position: None,
            tail_label_position: None,
            is_backward: false,
            from_subgraph: None,
            to_subgraph: None,
            source_port: None,
            target_port: None,
            preserve_orthogonal_topology: false,
            label_geometry: None,
            effective_wrapped_lines: None,
        };
        assert!(edge.label_geometry.is_none());
    }

    #[test]
    fn routed_edge_geometry_with_ports() {
        let edge = RoutedEdgeGeometry {
            index: 0,
            from: "A".to_string(),
            to: "B".to_string(),
            path: vec![],
            label_position: None,
            label_side: None,
            head_label_position: None,
            tail_label_position: None,
            is_backward: false,
            from_subgraph: None,
            to_subgraph: None,
            source_port: Some(EdgePort {
                face: crate::graph::attachment::PortFace::Bottom,
                fraction: 0.5,
                position: FPoint::new(50.0, 35.0),
                group_size: 1,
            }),
            target_port: None,
            preserve_orthogonal_topology: false,
            label_geometry: None,
            effective_wrapped_lines: None,
        };
        assert!(edge.source_port.is_some());
        assert!(edge.target_port.is_none());
    }
}