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
//! The module with the **Public API**.
use std::fmt::{self, Debug, Display};
use std::path::Path;
use syntree::Flavor;
use syntree::Tree;
use crate::{
internal::embedder::Embedder, Drawer, Embedding, LayouterError, Result, SvgDrawer, Visualize,
};
///
/// The Layouter type provides a simple builder mechanism with a fluent API.
///
pub struct Layouter<'a, T, F, D>
where
T: Copy,
F: Flavor,
D: ?Sized + Drawer,
{
tree: &'a Tree<T, F>,
drawer: &'a D,
file_name: Option<&'a Path>,
embedding: Embedding,
}
impl<'a, T, F> Layouter<'a, T, F, SvgDrawer>
where
T: Copy,
F: Flavor,
{
///
/// Creates a new Layouter with the required tree.
///
/// ```
/// use std::fmt;
/// use syntree_layout::{Layouter, Visualize};
/// use syntree::{Tree, Builder};
///
/// #[derive(Copy, Clone, Debug)]
/// struct MyNodeData(i32);
///
/// impl Visualize for MyNodeData {
/// fn visualize(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) }
/// fn emphasize(&self) -> bool { false }
/// }
///
///
/// let tree: Tree<MyNodeData, _> = Builder::new().build().unwrap();
/// let layouter = Layouter::new(&tree);
/// ```
///
pub fn new(tree: &'a Tree<T, F>) -> Self {
static DEFAULT_DRAWER: SvgDrawer = SvgDrawer::new();
Self {
tree,
drawer: &DEFAULT_DRAWER,
file_name: None,
embedding: Vec::default(),
}
}
}
impl<'a, T, F, D> Layouter<'a, T, F, D>
where
T: Copy,
F: Flavor,
D: ?Sized + Drawer,
{
///
/// Sets the path of the output file on the layouter.
///
/// ```
/// use std::fmt;
/// use syntree_layout::{Layouter, Visualize};
/// use syntree::{Tree, Builder};
///
/// #[derive(Copy, Clone, Debug)]
/// struct MyNodeData(i32);
///
/// impl Visualize for MyNodeData {
/// fn visualize(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) }
/// fn emphasize(&self) -> bool { false }
/// }
///
///
/// let tree: Tree<MyNodeData, _> = Builder::new().build().unwrap();
/// let layouter = Layouter::new(&tree)
/// .with_file_path("target/tmp/test.svg");
/// ```
///
pub fn with_file_path<P>(self, path: &'a P) -> Self
where
P: ?Sized + AsRef<Path>,
{
Self {
tree: self.tree,
file_name: Some(path.as_ref()),
drawer: self.drawer,
embedding: self.embedding,
}
}
///
/// Sets a different drawer when you don't want to use the default svg-drawer.
/// If this method is not called the crate's own svg-drawer is used.
///
/// ```
/// use std::fmt;
/// use std::path::Path;
/// use syntree_layout::{Drawer, Layouter, EmbeddedNode, Result, Visualize};
/// use syntree::{Tree, Builder};
///
/// struct NilDrawer;
/// impl Drawer for NilDrawer {
/// fn draw(&self, _file_name: &Path, _embedding: &[EmbeddedNode]) -> Result<()> {
/// Ok(())
/// }
/// }
/// #[derive(Copy, Clone, Debug)]
/// struct MyNodeData(i32);
///
/// impl Visualize for MyNodeData {
/// fn visualize(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) }
/// fn emphasize(&self) -> bool { false }
/// }
///
///
/// let tree: Tree<MyNodeData, _> = Builder::new().build().unwrap();
/// let drawer = NilDrawer;
/// let layouter = Layouter::new(&tree)
/// .with_drawer(&drawer)
/// .with_file_path("target/tmp/test.svg");
/// ```
///
pub fn with_drawer<U>(self, drawer: &'a U) -> Layouter<'a, T, F, U>
where
U: Drawer,
{
Layouter {
tree: self.tree,
file_name: self.file_name,
drawer,
embedding: self.embedding,
}
}
///
/// When the layouter instance is fully configured this method invokes the necessary embedding
/// functionality and uses the drawer which writes the result to the output file in its own
/// output format.
///
/// ```
/// use std::fmt;
/// use syntree_layout::{Layouter, Visualize, Result};
/// use syntree::{Tree, Builder};
///
/// #[derive(Copy, Clone, Debug)]
/// struct MyNodeData(i32);
///
/// impl Visualize for MyNodeData {
/// fn visualize(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) }
/// fn emphasize(&self) -> bool { false }
/// }
///
/// fn test() -> Result<()> {
/// let tree: Tree<MyNodeData, _> = Builder::new().build().unwrap();
/// Ok(Layouter::new(&tree)
/// .with_file_path("target/tmp/test.svg")
/// .embed_with_visualize()?
/// .write().expect("Failed writing layout"))
/// }
///
/// test().expect("Embedding should work");
/// ```
///
pub fn write(&self) -> Result<()> {
let Some(file_name) = &self.file_name else {
return Err(LayouterError::from_description(
"No output file name given - use Layouter::with_file_path.",
));
};
self.drawer.draw(file_name, &self.embedding)
}
/// Provides access to the embedding data for other uses than drawing, e.g. for tests
pub fn embedding(&self) -> &Embedding {
&self.embedding
}
}
impl<T, F, D> Layouter<'_, T, F, D>
where
T: Copy + Visualize,
F: Flavor,
D: ?Sized + Drawer,
{
///
/// This method creates an embedding of the nodes of the given tree in the plane.
/// The nodes representation is taken form the [Visualize][crate::Visualize] implementation of
/// type T.
///
/// # Panics
///
/// The method should not panic. If you encounter a panic this should be originated from
/// bugs in coding. Please report such panics.
///
pub fn embed_with_visualize(self) -> Result<Self> {
let embedding = Embedder::embed(
self.tree,
|value: &T, f| value.visualize(f),
|value: &T| value.emphasize(),
)?;
Ok(Self {
tree: self.tree,
file_name: self.file_name,
drawer: self.drawer,
embedding,
})
}
}
impl<T, F, D> Layouter<'_, T, F, D>
where
T: Copy,
F: Flavor,
D: ?Sized + Drawer,
{
///
/// This method creates an embedding of the nodes of the given tree in the plane.
/// The nodes representation is done with the help of the given source string.
///
/// # Panics
///
/// The method should not panic. If you encounter a panic this should be originated from
/// bugs in coding. Please report such panics.
///
pub fn embed_with_source(self, source: &str) -> Result<Self> {
let embedding = Embedder::embed_with_source(self.tree, source)?;
Ok(Self {
tree: self.tree,
file_name: self.file_name,
drawer: self.drawer,
embedding,
})
}
}
impl<T, F, D> Layouter<'_, T, F, D>
where
T: Copy + Display,
F: Flavor,
D: ?Sized + Drawer,
{
///
/// This method creates an embedding of the nodes of the given tree in the plane.
/// The nodes representation is done with the help of the given source string for tokens and the
/// implementation of the `Display` trait of the node data for inner nodes.
///
/// # Panics
///
/// The method should not panic. If you encounter a panic this should be originated from
/// bugs in coding. Please report such panics.
///
pub fn embed_with_source_and_display(self, source: &str) -> Result<Self> {
let embedding = Embedder::embed_with_source_and_display(self.tree, source)?;
Ok(Self {
tree: self.tree,
file_name: self.file_name,
drawer: self.drawer,
embedding,
})
}
}
impl<T, F, D> Layouter<'_, T, F, D>
where
T: Copy + Debug,
F: Flavor,
D: ?Sized + Drawer,
{
///
/// This method creates an embedding of the nodes of the given tree in the plane.
/// The nodes representation is taken form the [Debug] implementation of type T.
///
/// # Panics
///
/// The method should not panic. If you encounter a panic this should be originated from
/// bugs in coding. Please report such panics.
///
pub fn embed_with_debug(self) -> Result<Self> {
let embedding =
Embedder::embed(self.tree, |value: &T, f| value.fmt(f), |_value: &T| false)?;
Ok(Self {
tree: self.tree,
file_name: self.file_name,
drawer: self.drawer,
embedding,
})
}
}
impl<T, F, D> Layouter<'_, T, F, D>
where
T: Copy + Display,
F: Flavor,
D: ?Sized + Drawer,
{
///
/// This method creates an embedding of the nodes of the given tree in the plane.
/// The nodes representation is taken form the [Display] implementation of type T.
///
/// # Panics
///
/// The method should not panic. If you encounter a panic this should be originated from
/// bugs in coding. Please report such panics.
///
pub fn embed(self) -> Result<Self> {
let embedding =
Embedder::embed(self.tree, |value: &T, f| value.fmt(f), |_value: &T| false)?;
Ok(Self {
tree: self.tree,
file_name: self.file_name,
drawer: self.drawer,
embedding,
})
}
}
impl<T, F, D> Layouter<'_, T, F, D>
where
T: Copy,
F: Flavor,
D: Drawer,
{
///
/// This method creates an embedding of the nodes of the given tree in the plane.
/// The nodes representation is taken form the two given functions
/// [stringify][Layouter::embed_with] and [emphasize][Layouter::embed_with].
///
/// # Panics
///
/// The method should not panic. If you encounter a panic this should be originated from
/// bugs in coding. Please report such panics.
///
pub fn embed_with(
&self,
stringify: impl Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result,
emphasize: impl Fn(&T) -> bool,
) -> Result<Self> {
let embedding = Embedder::embed(self.tree, &stringify, &emphasize)?;
Ok(Self {
tree: self.tree,
file_name: self.file_name,
drawer: self.drawer,
embedding,
})
}
}