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
pub fn dot(graph: &crate::dot::Graph) -> String {
let mut p = crate::prettyplease::algorithm::Printer::new();
p.graph(graph);
p.eof()
}
mod dot {
use crate::dot::*;
use crate::prettyplease::{algorithm, iter::IterDelimited as _};
use crate::INDENT;
impl algorithm::Printer {
pub fn graph(
&mut self,
Graph {
strict,
directedness,
id,
brace_token: _,
stmt_list: StmtList { stmts },
}: &Graph,
) {
self.cbox(0); // Printer::file
self.cbox(INDENT); // Printer::item_mod
if strict.is_some() {
self.word("strict ");
}
match directedness {
GraphDirectedness::Graph(_) => self.word("graph "),
GraphDirectedness::Digraph(_) => self.word("digraph "),
}
if let Some(id) = id {
self.id(id)
}
self.word(" {");
self.hardbreak_if_nonempty();
for (stmt, _semi) in stmts {
self.stmt(stmt);
}
self.offset(-INDENT); // Printer::item_mod
self.end();
self.word("}");
self.hardbreak()
}
fn stmt(&mut self, stmt: &Stmt) {
match stmt {
Stmt::Assign(StmtAssign {
left,
eq_token: _,
right,
}) => {
self.id(left);
self.word(" = ");
self.id(right);
}
Stmt::Attr(StmtAttr { target, attrs }) => {
match target {
AttrTarget::Graph(_) => self.word("graph "),
AttrTarget::Node(_) => self.word("node "),
AttrTarget::Edge(_) => self.word("edge "),
};
self.attrs(attrs);
}
Stmt::Node(StmtNode { node_id, attrs }) => {
self.node_id(node_id);
self.nbsp();
if let Some(attrs) = attrs {
self.attrs(attrs);
}
}
Stmt::Edge(StmtEdge { from, edges, attrs }) => {
self.edge_target(from);
for (directedness, to) in edges {
match directedness {
EdgeDirectedness::Directed(_) => self.word(" -> "),
EdgeDirectedness::Undirected(_) => self.word(" -- "),
}
self.edge_target(to)
}
if let Some(attrs) = attrs {
self.nbsp();
self.attrs(attrs);
}
}
Stmt::Subgraph(subgraph) => self.stmt_subgraph(subgraph),
}
self.word(";");
self.hardbreak();
}
fn edge_target(&mut self, it: &EdgeTarget) {
match it {
EdgeTarget::Subgraph(subgraph) => self.stmt_subgraph(subgraph),
EdgeTarget::NodeId(node_id) => self.node_id(node_id),
}
}
fn stmt_subgraph(
&mut self,
StmtSubgraph {
prelude,
brace_token: _,
statements: StmtList { stmts },
}: &StmtSubgraph,
) {
self.word("subgraph ");
if let Some((_subgraph, Some(id))) = prelude {
self.id(id);
self.nbsp()
}
self.cbox(INDENT); // Printer::expr_block
self.word("{"); // Printer::small_block
if !stmts.is_empty() {
self.space();
for (stmt, _semi) in stmts {
self.stmt(stmt)
}
self.offset(-INDENT);
}
self.word("}"); // Printer::small_block
self.end(); // Printer::expr_block
}
fn id(&mut self, id: &ID) {
match id {
ID::AnyIdent(ident) => self.ident(ident),
ID::AnyLit(lit) => self.lit(lit),
ID::Html(
html @ HtmlString {
lt: _,
stream,
gt: _,
},
) => match html.source() {
Some(source) => {
self.word("<");
self.word(source);
self.word(">");
}
None => self.word(format!("< {} >", stream)),
},
ID::DotInt(DotInt { dot: _, int }) => {
self.word(".");
self.word(int.token().to_string())
}
}
}
fn node_id(&mut self, NodeId { id, port }: &NodeId) {
self.id(id);
if let Some(port) = port {
self.word(":");
match port {
Port::ID { colon: _, id } => self.id(id),
Port::Compass { colon: _, compass } => self.word(compass.to_string()),
Port::IDAndCompass {
colon1: _,
id,
colon2: _,
compass,
} => {
self.id(id);
self.word(":");
self.word(compass.to_string())
}
}
}
}
fn attrs(&mut self, Attrs { lists }: &Attrs) {
for attr_list in lists {
self.attr_list(attr_list)
}
}
fn attr_list(
&mut self,
AttrList {
bracket_token: _,
assigns,
}: &AttrList,
) {
self.word("["); // Printer::expr_array
self.nbsp();
self.cbox(INDENT);
self.zerobreak();
for element in assigns.iter().delimited() {
self.attr_assign(&element);
self.trailing_comma(element.is_last);
}
self.offset(-INDENT);
self.end();
self.nbsp();
self.word("]");
}
fn attr_assign(
&mut self,
AttrAssign {
left,
eq_token: _,
right,
trailing: _,
}: &AttrAssign,
) {
self.id(left);
self.word(" = ");
self.id(right);
}
}
}