Crate railroad

Source
Expand description

A library to create syntax (“railroad”) diagrams as Scalable Vector Graphics (SVG).

Railroad diagrams are a graphical way to represent context-free grammar. Every diagram has exactly one starting- and one end-point; everything that belongs to the described language is represented by one of the possible paths between those points.

Using this library, diagrams are created by primitives which implemented Node. Primitives are combined into more complex strctures by wrapping simple elements into more complex ones.

use railroad::*;

// This diagram will be a (horizontal) sequence of simple elements
let mut seq: Sequence<Box<dyn Node>> = Sequence::default();
seq.push(Box::new(Start))
   .push(Box::new(Terminal::new("BEGIN".to_owned())))
   .push(Box::new(NonTerminal::new("syntax".to_owned())))
   .push(Box::new(End));

// The library only computes the diagram's geometry; we use CSS for layout.
let mut dia = Diagram::new_with_stylesheet(seq, &Stylesheet::Light);

// A `Node`'s `fmt::Display` is its SVG.
println!("<html>{}</html>", dia);

Re-exports§

pub use crate::notactuallysvg as svg;
pub use resvg;

Modules§

notactuallysvg
render
A shorthand for rendering diagrams to images, using resvg’s default options.

Structs§

Choice
A container of elements, drawn vertically, where exactly one element has to be picked
Comment
A label / verbatim text, drawn in-line
Diagram
Empty
A dummy-element which has no size and draws nothing.
End
A symbol indicating the logical end of a syntax-diagram via two vertical bars.
HorizontalGrid
A horizontal group of unconnected elements.
LabeledBox
A box drawn around the given element and a label placed inside the box, above the element.
Link
Wraps another primitive, making it a clickable link to some URI.
NonTerminal
A NonTerminal, drawn as a rectangle.
Optional
Wraps another element to make that element logically optional.
Repeat
Wraps one element by providing a backwards-path through another element.
Sequence
A horizontal group of elements, connected from left to right.
SimpleEnd
A symbol indicating the logical end of a syntax-diagram via a circle
SimpleStart
A symbol indicating the logical start of a syntax-diagram via a circle
Stack
A vertical group of elements, drawn from top to bottom.
Start
A symbol indicating the logical start of a syntax-diagram via two vertical bars.
Terminal
A Terminal-symbol, drawn as a rectangle with rounded corners.
VerticalGrid
A vertical group of unconnected elements.

Enums§

LinkTarget
Possible targets for Link.
Stylesheet
Pre-defined stylesheets

Constants§

DEFAULT_CSS
Default Cascading Style Sheets for the resuling SVG.

Traits§

Node
A diagram is built from a set of primitives which implement Node.
NodeCollection
Helper trait for collections of nodes.