summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: b39ed4b2269be011305013389c9460324a6ff5d3 (plain)
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
#![doc(html_root_url = "https://docs.rs/dotavious")]
#![cfg_attr(docsrs, deny(broken_intra_doc_links))]

//! Dotavious provides bindings to generate [DOT](https://graphviz.org/doc/info/lang.html)
//! code used by the [Graphviz](http://graphviz.org/) for visualising graphs.
//! It also provides strongly typed attribute functions and offers almost complete
//! coverage of all Graphviz attributes and syntax.
//!
//! # Examples
//!
//! First example provides a basic directed graph:
//! 2 nodes connected by a single edge
//!
//! ```rust
//! use dotavious::{Dot, Edge, Graph, GraphBuilder, Node};
//!
//! let g = GraphBuilder::new_named_directed("example")
//!         .add_node(Node::new("N0"))
//!         .add_node(Node::new("N1"))
//!         .add_edge(Edge::new("N0", "N1"))
//!         .build()
//!         .unwrap();
//!
//! let dot = Dot { graph: g };
//! println!("{}", dot);
//! ```
//! Produces
//! ```dot
//! digraph example {
//!     N0;
//!     N1;
//!     N0 -> N1;
//! }
//! ```
//!
//! We can also output to a Writer via the `render` function
//! ```rust
//! use dotavious::{Dot, Edge, Graph, GraphBuilder, Node};
//! use std::io;
//! use std::io::Read;
//!
//! let g = GraphBuilder::new_named_directed("example")
//!         .add_node(Node::new("N0".to_string()))
//!         .add_node(Node::new("N1".to_string()))
//!         .add_edge(Edge::new("N0".to_string(), "N1".to_string()))
//!         .build()
//!         .unwrap();
//!
//! let dot = Dot { graph: g };
//! let mut writer= Vec::new();
//! dot.render(&mut writer).unwrap();
//!
//! // output to graphviz DOT formatted string
//! let mut dot_string = String::new();
//! Read::read_to_string(&mut &*writer, &mut dot_string).unwrap();
//! println!("{}", dot_string);
//! ```
//!
//! Second example provides a more complex graph showcasing
//! Dotavious' various builders and strongly typed attribute
//! functions.
//!
//! ```rust
//! use dotavious::attributes::{
//!     AttributeText, Color, CompassPoint, EdgeAttributes, EdgeStyle,
//!     GraphAttributeStatementBuilder, GraphAttributes, GraphStyle, NodeAttributes,
//!     NodeStyle, PortPosition, RankDir, Shape,
//! };
//! use dotavious::{
//!     Dot, Edge, EdgeAttributeStatementBuilder, EdgeBuilder, Graph,
//!     GraphBuilder, Node, NodeAttributeStatementBuilder, NodeBuilder,
//!     SubGraphBuilder
//! };
//! use std::io;
//! use std::io::Read;
//!
//! let cluster_0 = SubGraphBuilder::new_named("cluster_0")
//!     .add_graph_attributes(
//!         GraphAttributeStatementBuilder::new()
//!             .label("process #1")
//!             .style(GraphStyle::Filled)
//!             .color(Color::Named("lightgrey"))
//!             .build()
//!             .unwrap(),
//!     )
//!     .add_node_attributes(
//!         NodeAttributeStatementBuilder::new()
//!             .style(NodeStyle::Filled)
//!             .color(Color::Named("white"))
//!             .build()
//!             .unwrap(),
//!     )
//!     .add_edge(Edge::new("a0", "a1"))
//!     .add_edge(Edge::new("a1", "a2"))
//!     .add_edge(Edge::new("a2", "a3"))
//!     .build()
//!     .unwrap();
//!
//! let cluster_1 = SubGraphBuilder::new_named("cluster_1")
//!     .add_graph_attributes(
//!         GraphAttributeStatementBuilder::new()
//!             .label("process #2")
//!             .style(GraphStyle::Filled)
//!             .color(Color::Named("blue"))
//!             .build()
//!             .unwrap(),
//!     )
//!     .add_node_attributes(
//!         NodeAttributeStatementBuilder::new()
//!             .style(NodeStyle::Filled)
//!             .build()
//!             .unwrap(),
//!     )
//!     .add_edge(Edge::new("b0", "b1"))
//!     .add_edge(Edge::new("b1", "b2"))
//!     .add_edge(Edge::new("b2", "b3"))
//!     .build()
//!     .unwrap();
//!
//! let g = GraphBuilder::new_named_directed("G")
//!     .add_node(
//!         NodeBuilder::new("start".to_string())
//!             .shape(Shape::Mdiamond)
//!             .build()
//!             .unwrap(),
//!     )
//!     .add_node(
//!         NodeBuilder::new("end")
//!             .shape(Shape::Msquare)
//!             .build()
//!             .unwrap(),
//!     )
//!     .add_sub_graph(cluster_0)
//!     .add_sub_graph(cluster_1)
//!     .add_edge(Edge::new("start", "a0"))
//!     .add_edge(Edge::new("start", "b0"))
//!     .add_edge(Edge::new("a1", "b3"))
//!     .add_edge(Edge::new("b2", "a3"))
//!     .add_edge(Edge::new("a3", "a0"))
//!     .add_edge(Edge::new("a3", "end"))
//!     .add_edge(Edge::new("b3", "end"))
//!     .build();
//! ```
//!
//! Produces
//! ```dot
//! digraph G {
//!     subgraph cluster_0 {
//!         graph [label="process #1", style=filled, color="lightgrey"];
//!         node [style=filled, color="white"];
//!         a0 -> a1;
//!         a1 -> a2;
//!         a2 -> a3;
//!     }
//!
//!     subgraph cluster_1 {
//!         graph [label="process #2", style=filled, color="blue"];
//!         node [style=filled];
//!         b0 -> b1;
//!         b1 -> b2;
//!         b2 -> b3;
//!     }
//!
//!     start [shape=Mdiamond];
//!     end [shape=Msquare];
//!     start -> a0;
//!     start -> b0;
//!     a1 -> b3;
//!     b2 -> a3;
//!     a3 -> a0;
//!     a3 -> end;
//!     b3 -> end;
//! }
//! ```

pub mod attributes;
pub mod dot;
pub mod validation;

#[doc(hidden)]
pub use crate::dot::{
    Dot, DotString, Edge, EdgeAttributeStatementBuilder, EdgeBuilder, Graph,
    GraphBuilder, Node, NodeAttributeStatementBuilder, NodeBuilder, SubGraphBuilder,
};

// TODO: support adding edge based on index of nodes?
// TODO: handle render options
// TODO: explicit attribute methods with type safety and enforce constraints
// i'm thinking we have NodeTraits/GraphTraits/EdgeTraits (what about none? is that a graph trait?)
// which will have default methods that use an associated type field called "state" or "attributes" etc
// TODO: implement Clone for Graph
// TODO: see if we can get any insights from Haskell implementation
// https://hackage.haskell.org/package/graphviz-2999.20.1.0/docs/Data-GraphViz-Attributes-Complete.html#t:Point
// - I like this: A summary of known current constraints/limitations/differences:
// Add a DPoint enum?
// /// Either a Double or a (2D) Point (i.e. created with Point::new_2d).
// pub enum DPoint {
//     Double(f32),
//     Point(Point),
// }