limu 0.1.1

A diagram layout engine. It reads Mermaid and leaves the drawing to your program.
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
//! A diagram's text read into a model, and laid out as a scene, as Pele's parse and render in
//! src/core.ts read and draw it.

use crate::detect::{Detected, DiagramType, detect_diagram};
use crate::diagrams;
use crate::errors::{Error, ErrorCode};
use crate::layout::Options;
use crate::prepare::{ParseOptions, Prepared, prepare};
use crate::preprocess::preprocess;
use crate::scene::{Kind, Scene};
use crate::value::Map;

/// The model of a diagram of a type, as its parser reads it.
// A diagram holds one model, made once each time a text is parsed, so the room the largest
// takes in every diagram matters less than a box for each would.
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug)]
enum Model {
    #[cfg(feature = "flowchart")]
    Flowchart(diagrams::flowchart::FlowDb),
    #[cfg(feature = "pie")]
    Pie(diagrams::pie::PieModel),
    #[cfg(feature = "er")]
    Er(diagrams::er::ErDb),
    #[cfg(feature = "mindmap")]
    Mindmap(diagrams::mindmap::MindmapDb),
    #[cfg(feature = "kanban")]
    Kanban(diagrams::kanban::KanbanDb),
    #[cfg(feature = "timeline")]
    Timeline(diagrams::timeline::TimelineDb),
    #[cfg(feature = "journey")]
    Journey(diagrams::journey::JourneyDb),
    #[cfg(feature = "class")]
    Class(diagrams::class::ClassDb),
    #[cfg(feature = "quadrant")]
    QuadrantChart(diagrams::quadrant::QuadrantDb),
    #[cfg(feature = "xychart")]
    XyChart(diagrams::xychart::XyChartDb),
    #[cfg(feature = "state")]
    State(diagrams::state::StateDb),
    #[cfg(feature = "gantt")]
    Gantt(diagrams::gantt::GanttDb),
    #[cfg(feature = "architecture")]
    Architecture(diagrams::architecture::ArchitectureDb),
    #[cfg(feature = "sankey")]
    Sankey(diagrams::sankey::SankeyDb),
    #[cfg(feature = "packet")]
    Packet(diagrams::packet::PacketModel),
    #[cfg(feature = "radar")]
    Radar(diagrams::radar::RadarModel),
    #[cfg(feature = "treemap")]
    Treemap(diagrams::treemap::TreemapModel),
    #[cfg(feature = "info")]
    Info(diagrams::info::InfoModel),
    #[cfg(feature = "requirement")]
    Requirement(diagrams::requirement::RequirementDb),
    #[cfg(feature = "sequence")]
    Sequence(diagrams::sequence::SeqDb),
    #[cfg(feature = "gitgraph")]
    GitGraph(diagrams::gitgraph::GitModel),
    #[cfg(feature = "c4")]
    C4(diagrams::c4::C4Db),
    #[cfg(feature = "block")]
    Block(diagrams::block::BlockDb),
    #[cfg(feature = "railroad")]
    Railroad(diagrams::railroad::RailroadDb),
    #[cfg(feature = "agentflow")]
    AgentFlow(diagrams::agentflow::AgentflowDb),
    #[cfg(feature = "treeview")]
    TreeView(diagrams::treeview::TreeViewModel),
    #[cfg(feature = "cynefin")]
    Cynefin(diagrams::cynefin::CynefinModel),
    #[cfg(feature = "wardley")]
    Wardley(diagrams::wardley::WardleyModel),
    #[cfg(feature = "eventmodeling")]
    EventModeling(diagrams::eventmodeling::EventModelingModel),
    #[cfg(feature = "usecase")]
    UseCase(diagrams::usecase::UsecaseModel),
    #[cfg(feature = "venn")]
    Venn(diagrams::venn::VennDb),
    #[cfg(feature = "ishikawa")]
    Ishikawa(diagrams::ishikawa::IshikawaDb),
    #[cfg(feature = "swimlane")]
    Swimlane(diagrams::flowchart::FlowDb),
}

/// The types of diagram limu reads, in the order Pele's registry lists them.
const TYPES: &[DiagramType] = &[
    #[cfg(feature = "flowchart")]
    DiagramType::Flowchart,
    #[cfg(feature = "pie")]
    DiagramType::Pie,
    #[cfg(feature = "er")]
    DiagramType::Er,
    #[cfg(feature = "mindmap")]
    DiagramType::Mindmap,
    #[cfg(feature = "kanban")]
    DiagramType::Kanban,
    #[cfg(feature = "timeline")]
    DiagramType::Timeline,
    #[cfg(feature = "journey")]
    DiagramType::Journey,
    #[cfg(feature = "class")]
    DiagramType::Class,
    #[cfg(feature = "quadrant")]
    DiagramType::QuadrantChart,
    #[cfg(feature = "xychart")]
    DiagramType::XyChart,
    #[cfg(feature = "state")]
    DiagramType::State,
    #[cfg(feature = "gantt")]
    DiagramType::Gantt,
    #[cfg(feature = "architecture")]
    DiagramType::Architecture,
    #[cfg(feature = "requirement")]
    DiagramType::Requirement,
    #[cfg(feature = "sankey")]
    DiagramType::Sankey,
    #[cfg(feature = "packet")]
    DiagramType::Packet,
    #[cfg(feature = "radar")]
    DiagramType::Radar,
    #[cfg(feature = "treemap")]
    DiagramType::Treemap,
    #[cfg(feature = "info")]
    DiagramType::Info,
    #[cfg(feature = "sequence")]
    DiagramType::Sequence,
    #[cfg(feature = "gitgraph")]
    DiagramType::GitGraph,
    #[cfg(feature = "c4")]
    DiagramType::C4,
    #[cfg(feature = "block")]
    DiagramType::Block,
    #[cfg(feature = "railroad")]
    DiagramType::Railroad,
    #[cfg(feature = "agentflow")]
    DiagramType::AgentFlow,
    #[cfg(feature = "treeview")]
    DiagramType::TreeView,
    #[cfg(feature = "cynefin")]
    DiagramType::Cynefin,
    #[cfg(feature = "wardley")]
    DiagramType::Wardley,
    #[cfg(feature = "eventmodeling")]
    DiagramType::EventModeling,
    #[cfg(feature = "usecase")]
    DiagramType::UseCase,
    #[cfg(feature = "venn")]
    DiagramType::Venn,
    #[cfg(feature = "ishikawa")]
    DiagramType::Ishikawa,
    #[cfg(feature = "swimlane")]
    DiagramType::Swimlane,
];

/// The types of diagram limu reads, in the order Pele's registry lists them. Each is a
/// feature of the crate, and only those it is built with are here.
pub fn types() -> &'static [DiagramType] {
    TYPES
}

/// Whether limu reads the type of diagram a text is, as Pele's supports says: its type is
/// found, and it is one limu reads. Text whose front matter does not read is not supported.
/// It does not parse the diagram, which may still be in error.
pub fn supports(src: &str) -> bool {
    preprocess(src, None)
        .ok()
        .and_then(|pre| detect_diagram(&pre.text))
        .is_some_and(|found| TYPES.contains(&found.diagram))
}

/// A diagram's text read into a model, ready to be laid out. Laying it out changes neither
/// its model nor its configuration, so one diagram may be laid out many times, with other
/// options, and from many threads at once. A host that lays a diagram out again when its
/// width changes parses it once.
#[derive(Clone, Debug)]
pub struct Diagram {
    found: Detected,
    config: Map,
    title: Option<String>,
    model: Model,
}

/// Reads a diagram's text, as Pele's parse does: its front matter and directives, its type,
/// and then its body, with the parser of its type. The options hold the limits the text is
/// held to and the configuration beneath its own. What changes where things are is given to
/// [`Diagram::layout`]. Its errors are text of no type limu reads, text it cannot parse, and
/// text past a limit. An error found once the type is known holds it, as Pele's tagErrors
/// gives it, and so do the errors of layout.
pub fn parse(src: &str, options: &ParseOptions) -> Result<Diagram, Error> {
    let p = prepare(src, options)?;
    let model = Model::read(&p)?;
    Ok(Diagram {
        found: p.found,
        config: p.config,
        title: p.title,
        model,
    })
}

impl Model {
    /// Reads the body of a prepared text with the parser of its type. A type limu is built
    /// without is not supported.
    fn read(p: &Prepared) -> Result<Model, Error> {
        match p.found.diagram {
            #[cfg(feature = "flowchart")]
            DiagramType::Flowchart => p
                .parse_with(false, |s| {
                    diagrams::flowchart::parse(s, &p.config, p.title.as_deref(), p.max_edges)
                })
                .map(Model::Flowchart),
            #[cfg(feature = "pie")]
            DiagramType::Pie => p
                .parse_with(false, |s| diagrams::pie::parse(s, p.title.as_deref()))
                .map(Model::Pie),
            #[cfg(feature = "er")]
            DiagramType::Er => p
                .parse_with(false, |s| diagrams::er::parse(s, p.title.as_deref()))
                .map(Model::Er),
            #[cfg(feature = "mindmap")]
            DiagramType::Mindmap => p
                .parse_with(false, |s| diagrams::mindmap::parse(s, p.title.as_deref()))
                .map(Model::Mindmap),
            #[cfg(feature = "kanban")]
            DiagramType::Kanban => p
                .parse_with(false, |s| diagrams::kanban::parse(s, p.title.as_deref()))
                .map(Model::Kanban),
            #[cfg(feature = "timeline")]
            DiagramType::Timeline => p
                .parse_with(false, |s| diagrams::timeline::parse(s, p.title.as_deref()))
                .map(Model::Timeline),
            #[cfg(feature = "journey")]
            DiagramType::Journey => p
                .parse_with(false, |s| diagrams::journey::parse(s, p.title.as_deref()))
                .map(Model::Journey),
            #[cfg(feature = "class")]
            DiagramType::Class => p
                .parse_with(false, |s| diagrams::class::parse(s, p.title.as_deref()))
                .map(Model::Class),
            #[cfg(feature = "quadrant")]
            DiagramType::QuadrantChart => p
                .parse_with(false, |s| diagrams::quadrant::parse(s, p.title.as_deref()))
                .map(Model::QuadrantChart),
            #[cfg(feature = "xychart")]
            DiagramType::XyChart => p
                .parse_with(false, |s| diagrams::xychart::parse(s, p.title.as_deref()))
                .map(Model::XyChart),
            #[cfg(feature = "state")]
            DiagramType::State => p
                .parse_with(false, |s| diagrams::state::parse(s, p.title.as_deref()))
                .map(Model::State),
            #[cfg(feature = "gantt")]
            DiagramType::Gantt => p
                .parse_with(false, |s| {
                    diagrams::gantt::parse(s, &p.config, p.title.as_deref())
                })
                .map(Model::Gantt),
            #[cfg(feature = "architecture")]
            DiagramType::Architecture => p
                .parse_with(false, |s| {
                    diagrams::architecture::parse(s, p.title.as_deref())
                })
                .map(Model::Architecture),
            #[cfg(feature = "sankey")]
            DiagramType::Sankey => p
                .parse_with(false, |s| diagrams::sankey::parse(s, p.title.as_deref()))
                .map(Model::Sankey),
            #[cfg(feature = "packet")]
            DiagramType::Packet => p
                .parse_with(false, |s| {
                    diagrams::packet::parse(s, &p.config, p.title.as_deref())
                })
                .map(Model::Packet),
            #[cfg(feature = "radar")]
            DiagramType::Radar => p
                .parse_with(false, |s| diagrams::radar::parse(s, p.title.as_deref()))
                .map(Model::Radar),
            #[cfg(feature = "treemap")]
            DiagramType::Treemap => p
                .parse_with(false, |s| diagrams::treemap::parse(s, p.title.as_deref()))
                .map(Model::Treemap),
            #[cfg(feature = "info")]
            DiagramType::Info => p
                .parse_with(false, |s| diagrams::info::parse(s, p.title.as_deref()))
                .map(Model::Info),
            #[cfg(feature = "requirement")]
            DiagramType::Requirement => p
                .parse_with(false, |s| {
                    diagrams::requirement::parse(s, p.title.as_deref())
                })
                .map(Model::Requirement),
            #[cfg(feature = "sequence")]
            DiagramType::Sequence => p
                .parse_with(false, |s| {
                    diagrams::sequence::parse(s, &p.config, p.title.as_deref())
                })
                .map(Model::Sequence),
            #[cfg(feature = "gitgraph")]
            DiagramType::GitGraph => p
                .parse_with(false, |s| {
                    diagrams::gitgraph::parse(s, &p.config, p.title.as_deref())
                })
                .map(Model::GitGraph),
            #[cfg(feature = "c4")]
            DiagramType::C4 => p
                .parse_with(false, |s| diagrams::c4::parse(s, p.title.as_deref()))
                .map(Model::C4),
            #[cfg(feature = "block")]
            DiagramType::Block => p
                .parse_with(false, |s| diagrams::block::parse(s, p.title.as_deref()))
                .map(Model::Block),
            #[cfg(feature = "railroad")]
            DiagramType::Railroad => p
                .parse_with(false, |s| diagrams::railroad::parse(s, p.title.as_deref()))
                .map(Model::Railroad),
            #[cfg(feature = "agentflow")]
            DiagramType::AgentFlow => p
                .parse_with(true, |s| {
                    diagrams::agentflow::parse(
                        s,
                        &p.config,
                        p.title.as_deref(),
                        p.line_offset,
                        p.max_edges,
                    )
                })
                .map(Model::AgentFlow),
            #[cfg(feature = "treeview")]
            DiagramType::TreeView => p
                .parse_with(false, |s| diagrams::treeview::parse(s, p.title.as_deref()))
                .map(Model::TreeView),
            #[cfg(feature = "cynefin")]
            DiagramType::Cynefin => p
                .parse_with(false, |s| diagrams::cynefin::parse(s, p.title.as_deref()))
                .map(Model::Cynefin),
            #[cfg(feature = "wardley")]
            DiagramType::Wardley => p
                .parse_with(false, |s| diagrams::wardley::parse(s, p.title.as_deref()))
                .map(Model::Wardley),
            #[cfg(feature = "eventmodeling")]
            DiagramType::EventModeling => p
                .parse_with(true, |s| {
                    diagrams::eventmodeling::parse(s, p.title.as_deref())
                })
                .map(Model::EventModeling),
            #[cfg(feature = "usecase")]
            DiagramType::UseCase => p
                .parse_with(false, |s| diagrams::usecase::parse(s, p.title.as_deref()))
                .map(Model::UseCase),
            #[cfg(feature = "venn")]
            DiagramType::Venn => p
                .parse_with(false, |s| diagrams::venn::parse(s, p.title.as_deref()))
                .map(Model::Venn),
            #[cfg(feature = "ishikawa")]
            DiagramType::Ishikawa => p
                .parse_with(false, |s| diagrams::ishikawa::parse(s, p.title.as_deref()))
                .map(Model::Ishikawa),
            #[cfg(feature = "swimlane")]
            DiagramType::Swimlane => p
                .parse_with(false, |s| {
                    diagrams::swimlane::parse(s, &p.config, p.title.as_deref(), p.max_edges)
                })
                .map(Model::Swimlane),
            // Built with every type of diagram, limu reads them all, and no type is left
            // for this.
            #[allow(unreachable_patterns)]
            _ => {
                let message = format!("Diagram type \"{}\" is not supported yet.", p.found.diagram);
                Err(Error::new(message, ErrorCode::UnsupportedDiagram).of(p.found))
            }
        }
    }

    /// Lays the model out with the layout of its type. Most layouts cannot fail. Those that
    /// can, as Pele's render throws for them, give their errors.
    // Built with no type of diagram, limu has no model, nothing reads the configuration or
    // the options, and nothing past the match is reached.
    #[allow(unused_variables, unreachable_code)]
    fn layout(&self, config: &Map, o: &Options) -> Result<Scene, Error> {
        let sc = match *self {
            #[cfg(feature = "flowchart")]
            Model::Flowchart(ref db) => diagrams::flowchart::layout(db, config, o),
            #[cfg(feature = "pie")]
            Model::Pie(ref m) => diagrams::pie::layout(m, config, o),
            #[cfg(feature = "er")]
            Model::Er(ref m) => diagrams::er::layout(m, config, o),
            #[cfg(feature = "mindmap")]
            Model::Mindmap(ref m) => diagrams::mindmap::layout(m, config, o),
            #[cfg(feature = "kanban")]
            Model::Kanban(ref m) => diagrams::kanban::layout(m, config, o),
            #[cfg(feature = "timeline")]
            Model::Timeline(ref m) => diagrams::timeline::layout(m, config, o),
            #[cfg(feature = "journey")]
            Model::Journey(ref m) => diagrams::journey::layout(m, config, o),
            #[cfg(feature = "class")]
            Model::Class(ref m) => diagrams::class::layout(m, config, o),
            #[cfg(feature = "quadrant")]
            Model::QuadrantChart(ref m) => diagrams::quadrant::layout(m, config, o),
            #[cfg(feature = "xychart")]
            Model::XyChart(ref m) => diagrams::xychart::layout(m, config, o)?,
            #[cfg(feature = "state")]
            Model::State(ref m) => diagrams::state::layout(m, config, o),
            #[cfg(feature = "gantt")]
            Model::Gantt(ref m) => diagrams::gantt::layout(m, config, o)?,
            #[cfg(feature = "architecture")]
            Model::Architecture(ref m) => diagrams::architecture::layout(m, config, o),
            #[cfg(feature = "sankey")]
            Model::Sankey(ref m) => diagrams::sankey::layout(m, config, o)?,
            #[cfg(feature = "packet")]
            Model::Packet(ref m) => diagrams::packet::layout(m, config, o),
            #[cfg(feature = "radar")]
            Model::Radar(ref m) => diagrams::radar::layout(m, config, o),
            #[cfg(feature = "treemap")]
            Model::Treemap(ref m) => diagrams::treemap::layout(m, config, o),
            #[cfg(feature = "info")]
            Model::Info(ref m) => diagrams::info::layout(m, config, o),
            #[cfg(feature = "requirement")]
            Model::Requirement(ref db) => diagrams::requirement::layout(db, config, o),
            #[cfg(feature = "sequence")]
            Model::Sequence(ref m) => diagrams::sequence::layout(m, config, o),
            #[cfg(feature = "gitgraph")]
            Model::GitGraph(ref m) => diagrams::gitgraph::layout(m, config, o),
            #[cfg(feature = "c4")]
            Model::C4(ref m) => diagrams::c4::layout(m, config, o)?,
            #[cfg(feature = "block")]
            Model::Block(ref m) => diagrams::block::layout(m, config, o)?,
            #[cfg(feature = "railroad")]
            Model::Railroad(ref m) => diagrams::railroad::layout(m, config, o),
            #[cfg(feature = "agentflow")]
            Model::AgentFlow(ref m) => diagrams::agentflow::layout(m, config, o),
            #[cfg(feature = "treeview")]
            Model::TreeView(ref m) => diagrams::treeview::layout(m, config, o),
            #[cfg(feature = "cynefin")]
            Model::Cynefin(ref m) => diagrams::cynefin::layout(m, config, o),
            #[cfg(feature = "wardley")]
            Model::Wardley(ref m) => diagrams::wardley::layout(m, config, o),
            #[cfg(feature = "eventmodeling")]
            Model::EventModeling(ref m) => diagrams::eventmodeling::layout(m, config, o),
            #[cfg(feature = "usecase")]
            Model::UseCase(ref m) => diagrams::usecase::layout(m, config, o),
            #[cfg(feature = "venn")]
            Model::Venn(ref m) => diagrams::venn::layout(m, config, o),
            #[cfg(feature = "ishikawa")]
            Model::Ishikawa(ref m) => diagrams::ishikawa::layout(m, config, o),
            #[cfg(feature = "swimlane")]
            Model::Swimlane(ref db) => diagrams::swimlane::layout(db, config, o),
        };
        Ok(sc)
    }
}

impl Diagram {
    /// The diagram's type.
    pub fn diagram_type(&self) -> DiagramType {
        self.found.diagram
    }

    /// The diagram's type as Mermaid names it, as Pele's parse gives it: "flowchart-v2" for
    /// `graph TD`, where the type is a flowchart.
    pub fn mermaid_type(&self) -> &'static str {
        self.found.mermaid_type
    }

    /// The title the diagram's front matter gives it.
    pub fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    /// The diagram's configuration: its own, over the configuration it was parsed with.
    pub fn config(&self) -> &Map {
        &self.config
    }

    /// Lays the diagram out as a scene, for a backend to draw, with the layout of its type,
    /// and snaps it to the options' grid. Its errors are those a layout gives, and one of a
    /// limit for a scene that would hold a number that is not finite, as numbers at the edge
    /// of what an f64 holds can give, where Pele draws NaN.
    pub fn layout(&self, o: &Options) -> Result<Scene, Error> {
        let mut sc = self
            .model
            .layout(&self.config, o)
            .map_err(|e| e.tagged(self.found))?;
        // A text item the layout gives no box of its own takes the box its label is drawn
        // in, so that a host hit-tests and clips every text by its bounds.
        for it in &mut sc.items {
            if matches!(it.kind, Kind::Text) && it.bounds.is_unset() {
                it.bounds = it.label_bounds();
            }
        }
        if let Some(grid) = o.snap {
            sc.snap(grid);
        }
        if !crate::items::finite(&sc) {
            let message = "Diagram cannot be laid out in finite numbers.";
            return Err(Error::new(message, ErrorCode::Limit).tagged(self.found));
        }
        Ok(sc)
    }
}

/// Reads a diagram's text and lays it out as a scene, for a backend to draw: [`parse`] with
/// the default limits and no configuration of the host's, and then [`Diagram::layout`]. Its
/// errors are theirs.
pub fn layout(src: &str, o: &Options) -> Result<Scene, Error> {
    parse(src, &ParseOptions::default())?.layout(o)
}

#[cfg(test)]
mod tests;