pub struct Layouter<'a, T, F, D>{ /* private fields */ }
Expand description
The Layouter type provides a simple builder mechanism with a fluent API.
Implementations§
Source§impl<'a, T, F> Layouter<'a, T, F, SvgDrawer>
impl<'a, T, F> Layouter<'a, T, F, SvgDrawer>
Sourcepub fn new(tree: &'a Tree<T, F>) -> Self
pub fn new(tree: &'a Tree<T, F>) -> Self
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);
Examples found in repository?
29fn main() -> Result<()> {
30 // 0
31 // / \
32 // 1 2
33 // / \
34 // 3 4
35 let mut tree = Builder::new();
36
37 tree.open(MyNodeData(0)).unwrap();
38 tree.open(MyNodeData(1)).unwrap();
39 tree.token(MyNodeData(3), 1).unwrap();
40 tree.token(MyNodeData(4), 1).unwrap();
41 tree.close().unwrap();
42 tree.token(MyNodeData(2), 1).unwrap();
43 tree.close().unwrap();
44
45 let tree = tree.build().unwrap();
46 Layouter::new(&tree)
47 .with_file_path("examples/example1_vis.svg")
48 .embed_with_visualize()?
49 .write()?;
50
51 Layouter::new(&tree)
52 .with_file_path("examples/example1_deb.svg")
53 .embed_with_debug()?
54 .write()?;
55
56 Layouter::new(&tree)
57 .with_file_path("examples/example1_dis.svg")
58 .embed()?
59 .write()
60}
More examples
62fn main() -> Result<()> {
63 let source = "256 / 2 + 64 * 2";
64 let tree = syntree::tree! {
65 Syntax::Operation => {
66 Syntax::Number => {
67 (Syntax::Number, 3),
68 },
69 (Syntax::Whitespace, 1),
70 Syntax::Operator => {
71 (Syntax::Div, 1),
72 },
73 (Syntax::Whitespace, 1),
74 Syntax::Number => {
75 (Syntax::Number, 1),
76 },
77 (Syntax::Whitespace, 1),
78 Syntax::Operator => {
79 (Syntax::Plus, 1),
80 },
81 (Syntax::Whitespace, 1),
82 Syntax::Operation => {
83 Syntax::Number => {
84 (Syntax::Number, 2),
85 },
86 (Syntax::Whitespace, 1),
87 Syntax::Operator => {
88 (Syntax::Mul, 1),
89 },
90 (Syntax::Whitespace, 1),
91 Syntax::Number => {
92 (Syntax::Number, 1),
93 },
94 },
95
96 }
97 };
98
99 // Embed the tree with the source code.
100 Layouter::new(&tree)
101 .with_file_path("examples/example3_1.svg")
102 .embed_with_source(source)
103 .map_err(|e| anyhow::anyhow!(e))?
104 .write()
105 .map_err(|e| anyhow::anyhow!(e))?;
106 // Embed the tree with the source code and `Display` trait.
107 Layouter::new(&tree)
108 .with_file_path("examples/example3_2.svg")
109 .embed_with_source_and_display(source)
110 .map_err(|e| anyhow::anyhow!(e))?
111 .write()
112 .map_err(|e| anyhow::anyhow!(e))
113}
81fn main() -> std::result::Result<(), anyhow::Error> {
82 let tree = syntree::tree! {
83 Ast::Calc => {
84 Ast::CalcLst1 => {
85 Ast::CalcLst1Itm1 => {
86 Ast::Instruction => {
87 Ast::Assignment => {
88 Ast::AssignItem => {
89 Ast::Id => { (Ast::Tok("c"), 1) },
90 Ast::AssignOp => { (Ast::Tok("="), 1) },
91 },
92 Ast::LocigalOr => {
93 Ast::LocigalAnd => {
94 Ast::BitwiseOr => {
95 Ast::BitwiseAnd => {
96 Ast::Equality => {
97 Ast::Relational => {
98 Ast::BitwiseShift => {
99 Ast::Sum => {
100 Ast::Mult => {
101 Ast::Power => {
102 Ast::Factor => {
103 Ast::Number => { (Ast::Tok("2"), 1) },
104 }
105 },
106 Ast::MultLst1 => {
107 Ast::MultLst1Itm1 => {
108 Ast::MultItem => {
109 Ast::MultOp => { (Ast::Tok("*"), 1) },
110 Ast::Power => {
111 Ast::Factor => {
112 Ast::Number => { (Ast::Tok("4"), 1) },
113 }
114 }
115 }
116 }
117 }
118 },
119 Ast::SumLst1 => {
120 Ast::SumLst1Itm1 => {
121 Ast::SumItem => {
122 Ast::AddOp => {
123 Ast::Plus => { (Ast::Tok("+"), 1) }
124 },
125 Ast::Mult => {
126 Ast::Power => {
127 Ast::Factor => {
128 Ast::Number => { (Ast::Tok("2"), 1) },
129 }
130 }
131 }
132 }
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143 },
144 },
145 (Ast::Tok(";"), 1)
146 }
147 }
148 }
149 };
150
151 Layouter::new(&tree)
152 .with_file_path("examples/example2.svg")
153 .embed_with_visualize()
154 .map_err(|e| anyhow::anyhow!(e))?
155 .write()
156 .map_err(|e| anyhow::anyhow!(e))
157}
Source§impl<'a, T, F, D> Layouter<'a, T, F, D>
impl<'a, T, F, D> Layouter<'a, T, F, D>
Sourcepub fn with_file_path<P>(self, path: &'a P) -> Self
pub fn with_file_path<P>(self, path: &'a P) -> Self
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");
Examples found in repository?
29fn main() -> Result<()> {
30 // 0
31 // / \
32 // 1 2
33 // / \
34 // 3 4
35 let mut tree = Builder::new();
36
37 tree.open(MyNodeData(0)).unwrap();
38 tree.open(MyNodeData(1)).unwrap();
39 tree.token(MyNodeData(3), 1).unwrap();
40 tree.token(MyNodeData(4), 1).unwrap();
41 tree.close().unwrap();
42 tree.token(MyNodeData(2), 1).unwrap();
43 tree.close().unwrap();
44
45 let tree = tree.build().unwrap();
46 Layouter::new(&tree)
47 .with_file_path("examples/example1_vis.svg")
48 .embed_with_visualize()?
49 .write()?;
50
51 Layouter::new(&tree)
52 .with_file_path("examples/example1_deb.svg")
53 .embed_with_debug()?
54 .write()?;
55
56 Layouter::new(&tree)
57 .with_file_path("examples/example1_dis.svg")
58 .embed()?
59 .write()
60}
More examples
62fn main() -> Result<()> {
63 let source = "256 / 2 + 64 * 2";
64 let tree = syntree::tree! {
65 Syntax::Operation => {
66 Syntax::Number => {
67 (Syntax::Number, 3),
68 },
69 (Syntax::Whitespace, 1),
70 Syntax::Operator => {
71 (Syntax::Div, 1),
72 },
73 (Syntax::Whitespace, 1),
74 Syntax::Number => {
75 (Syntax::Number, 1),
76 },
77 (Syntax::Whitespace, 1),
78 Syntax::Operator => {
79 (Syntax::Plus, 1),
80 },
81 (Syntax::Whitespace, 1),
82 Syntax::Operation => {
83 Syntax::Number => {
84 (Syntax::Number, 2),
85 },
86 (Syntax::Whitespace, 1),
87 Syntax::Operator => {
88 (Syntax::Mul, 1),
89 },
90 (Syntax::Whitespace, 1),
91 Syntax::Number => {
92 (Syntax::Number, 1),
93 },
94 },
95
96 }
97 };
98
99 // Embed the tree with the source code.
100 Layouter::new(&tree)
101 .with_file_path("examples/example3_1.svg")
102 .embed_with_source(source)
103 .map_err(|e| anyhow::anyhow!(e))?
104 .write()
105 .map_err(|e| anyhow::anyhow!(e))?;
106 // Embed the tree with the source code and `Display` trait.
107 Layouter::new(&tree)
108 .with_file_path("examples/example3_2.svg")
109 .embed_with_source_and_display(source)
110 .map_err(|e| anyhow::anyhow!(e))?
111 .write()
112 .map_err(|e| anyhow::anyhow!(e))
113}
81fn main() -> std::result::Result<(), anyhow::Error> {
82 let tree = syntree::tree! {
83 Ast::Calc => {
84 Ast::CalcLst1 => {
85 Ast::CalcLst1Itm1 => {
86 Ast::Instruction => {
87 Ast::Assignment => {
88 Ast::AssignItem => {
89 Ast::Id => { (Ast::Tok("c"), 1) },
90 Ast::AssignOp => { (Ast::Tok("="), 1) },
91 },
92 Ast::LocigalOr => {
93 Ast::LocigalAnd => {
94 Ast::BitwiseOr => {
95 Ast::BitwiseAnd => {
96 Ast::Equality => {
97 Ast::Relational => {
98 Ast::BitwiseShift => {
99 Ast::Sum => {
100 Ast::Mult => {
101 Ast::Power => {
102 Ast::Factor => {
103 Ast::Number => { (Ast::Tok("2"), 1) },
104 }
105 },
106 Ast::MultLst1 => {
107 Ast::MultLst1Itm1 => {
108 Ast::MultItem => {
109 Ast::MultOp => { (Ast::Tok("*"), 1) },
110 Ast::Power => {
111 Ast::Factor => {
112 Ast::Number => { (Ast::Tok("4"), 1) },
113 }
114 }
115 }
116 }
117 }
118 },
119 Ast::SumLst1 => {
120 Ast::SumLst1Itm1 => {
121 Ast::SumItem => {
122 Ast::AddOp => {
123 Ast::Plus => { (Ast::Tok("+"), 1) }
124 },
125 Ast::Mult => {
126 Ast::Power => {
127 Ast::Factor => {
128 Ast::Number => { (Ast::Tok("2"), 1) },
129 }
130 }
131 }
132 }
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143 },
144 },
145 (Ast::Tok(";"), 1)
146 }
147 }
148 }
149 };
150
151 Layouter::new(&tree)
152 .with_file_path("examples/example2.svg")
153 .embed_with_visualize()
154 .map_err(|e| anyhow::anyhow!(e))?
155 .write()
156 .map_err(|e| anyhow::anyhow!(e))
157}
Sourcepub fn with_drawer<U>(self, drawer: &'a U) -> Layouter<'a, T, F, U>where
U: Drawer,
pub fn with_drawer<U>(self, drawer: &'a U) -> Layouter<'a, T, F, U>where
U: Drawer,
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");
Sourcepub fn write(&self) -> Result<()>
pub fn write(&self) -> Result<()>
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");
Examples found in repository?
29fn main() -> Result<()> {
30 // 0
31 // / \
32 // 1 2
33 // / \
34 // 3 4
35 let mut tree = Builder::new();
36
37 tree.open(MyNodeData(0)).unwrap();
38 tree.open(MyNodeData(1)).unwrap();
39 tree.token(MyNodeData(3), 1).unwrap();
40 tree.token(MyNodeData(4), 1).unwrap();
41 tree.close().unwrap();
42 tree.token(MyNodeData(2), 1).unwrap();
43 tree.close().unwrap();
44
45 let tree = tree.build().unwrap();
46 Layouter::new(&tree)
47 .with_file_path("examples/example1_vis.svg")
48 .embed_with_visualize()?
49 .write()?;
50
51 Layouter::new(&tree)
52 .with_file_path("examples/example1_deb.svg")
53 .embed_with_debug()?
54 .write()?;
55
56 Layouter::new(&tree)
57 .with_file_path("examples/example1_dis.svg")
58 .embed()?
59 .write()
60}
More examples
62fn main() -> Result<()> {
63 let source = "256 / 2 + 64 * 2";
64 let tree = syntree::tree! {
65 Syntax::Operation => {
66 Syntax::Number => {
67 (Syntax::Number, 3),
68 },
69 (Syntax::Whitespace, 1),
70 Syntax::Operator => {
71 (Syntax::Div, 1),
72 },
73 (Syntax::Whitespace, 1),
74 Syntax::Number => {
75 (Syntax::Number, 1),
76 },
77 (Syntax::Whitespace, 1),
78 Syntax::Operator => {
79 (Syntax::Plus, 1),
80 },
81 (Syntax::Whitespace, 1),
82 Syntax::Operation => {
83 Syntax::Number => {
84 (Syntax::Number, 2),
85 },
86 (Syntax::Whitespace, 1),
87 Syntax::Operator => {
88 (Syntax::Mul, 1),
89 },
90 (Syntax::Whitespace, 1),
91 Syntax::Number => {
92 (Syntax::Number, 1),
93 },
94 },
95
96 }
97 };
98
99 // Embed the tree with the source code.
100 Layouter::new(&tree)
101 .with_file_path("examples/example3_1.svg")
102 .embed_with_source(source)
103 .map_err(|e| anyhow::anyhow!(e))?
104 .write()
105 .map_err(|e| anyhow::anyhow!(e))?;
106 // Embed the tree with the source code and `Display` trait.
107 Layouter::new(&tree)
108 .with_file_path("examples/example3_2.svg")
109 .embed_with_source_and_display(source)
110 .map_err(|e| anyhow::anyhow!(e))?
111 .write()
112 .map_err(|e| anyhow::anyhow!(e))
113}
81fn main() -> std::result::Result<(), anyhow::Error> {
82 let tree = syntree::tree! {
83 Ast::Calc => {
84 Ast::CalcLst1 => {
85 Ast::CalcLst1Itm1 => {
86 Ast::Instruction => {
87 Ast::Assignment => {
88 Ast::AssignItem => {
89 Ast::Id => { (Ast::Tok("c"), 1) },
90 Ast::AssignOp => { (Ast::Tok("="), 1) },
91 },
92 Ast::LocigalOr => {
93 Ast::LocigalAnd => {
94 Ast::BitwiseOr => {
95 Ast::BitwiseAnd => {
96 Ast::Equality => {
97 Ast::Relational => {
98 Ast::BitwiseShift => {
99 Ast::Sum => {
100 Ast::Mult => {
101 Ast::Power => {
102 Ast::Factor => {
103 Ast::Number => { (Ast::Tok("2"), 1) },
104 }
105 },
106 Ast::MultLst1 => {
107 Ast::MultLst1Itm1 => {
108 Ast::MultItem => {
109 Ast::MultOp => { (Ast::Tok("*"), 1) },
110 Ast::Power => {
111 Ast::Factor => {
112 Ast::Number => { (Ast::Tok("4"), 1) },
113 }
114 }
115 }
116 }
117 }
118 },
119 Ast::SumLst1 => {
120 Ast::SumLst1Itm1 => {
121 Ast::SumItem => {
122 Ast::AddOp => {
123 Ast::Plus => { (Ast::Tok("+"), 1) }
124 },
125 Ast::Mult => {
126 Ast::Power => {
127 Ast::Factor => {
128 Ast::Number => { (Ast::Tok("2"), 1) },
129 }
130 }
131 }
132 }
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143 },
144 },
145 (Ast::Tok(";"), 1)
146 }
147 }
148 }
149 };
150
151 Layouter::new(&tree)
152 .with_file_path("examples/example2.svg")
153 .embed_with_visualize()
154 .map_err(|e| anyhow::anyhow!(e))?
155 .write()
156 .map_err(|e| anyhow::anyhow!(e))
157}
Source§impl<T, F, D> Layouter<'_, T, F, D>
impl<T, F, D> Layouter<'_, T, F, D>
Sourcepub fn embed_with_visualize(self) -> Result<Self>
pub fn embed_with_visualize(self) -> Result<Self>
This method creates an embedding of the nodes of the given tree in the plane. The nodes representation is taken form the 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.
Examples found in repository?
29fn main() -> Result<()> {
30 // 0
31 // / \
32 // 1 2
33 // / \
34 // 3 4
35 let mut tree = Builder::new();
36
37 tree.open(MyNodeData(0)).unwrap();
38 tree.open(MyNodeData(1)).unwrap();
39 tree.token(MyNodeData(3), 1).unwrap();
40 tree.token(MyNodeData(4), 1).unwrap();
41 tree.close().unwrap();
42 tree.token(MyNodeData(2), 1).unwrap();
43 tree.close().unwrap();
44
45 let tree = tree.build().unwrap();
46 Layouter::new(&tree)
47 .with_file_path("examples/example1_vis.svg")
48 .embed_with_visualize()?
49 .write()?;
50
51 Layouter::new(&tree)
52 .with_file_path("examples/example1_deb.svg")
53 .embed_with_debug()?
54 .write()?;
55
56 Layouter::new(&tree)
57 .with_file_path("examples/example1_dis.svg")
58 .embed()?
59 .write()
60}
More examples
81fn main() -> std::result::Result<(), anyhow::Error> {
82 let tree = syntree::tree! {
83 Ast::Calc => {
84 Ast::CalcLst1 => {
85 Ast::CalcLst1Itm1 => {
86 Ast::Instruction => {
87 Ast::Assignment => {
88 Ast::AssignItem => {
89 Ast::Id => { (Ast::Tok("c"), 1) },
90 Ast::AssignOp => { (Ast::Tok("="), 1) },
91 },
92 Ast::LocigalOr => {
93 Ast::LocigalAnd => {
94 Ast::BitwiseOr => {
95 Ast::BitwiseAnd => {
96 Ast::Equality => {
97 Ast::Relational => {
98 Ast::BitwiseShift => {
99 Ast::Sum => {
100 Ast::Mult => {
101 Ast::Power => {
102 Ast::Factor => {
103 Ast::Number => { (Ast::Tok("2"), 1) },
104 }
105 },
106 Ast::MultLst1 => {
107 Ast::MultLst1Itm1 => {
108 Ast::MultItem => {
109 Ast::MultOp => { (Ast::Tok("*"), 1) },
110 Ast::Power => {
111 Ast::Factor => {
112 Ast::Number => { (Ast::Tok("4"), 1) },
113 }
114 }
115 }
116 }
117 }
118 },
119 Ast::SumLst1 => {
120 Ast::SumLst1Itm1 => {
121 Ast::SumItem => {
122 Ast::AddOp => {
123 Ast::Plus => { (Ast::Tok("+"), 1) }
124 },
125 Ast::Mult => {
126 Ast::Power => {
127 Ast::Factor => {
128 Ast::Number => { (Ast::Tok("2"), 1) },
129 }
130 }
131 }
132 }
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143 },
144 },
145 (Ast::Tok(";"), 1)
146 }
147 }
148 }
149 };
150
151 Layouter::new(&tree)
152 .with_file_path("examples/example2.svg")
153 .embed_with_visualize()
154 .map_err(|e| anyhow::anyhow!(e))?
155 .write()
156 .map_err(|e| anyhow::anyhow!(e))
157}
Source§impl<T, F, D> Layouter<'_, T, F, D>
impl<T, F, D> Layouter<'_, T, F, D>
Sourcepub fn embed_with_source(self, source: &str) -> Result<Self>
pub fn embed_with_source(self, source: &str) -> Result<Self>
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.
Examples found in repository?
62fn main() -> Result<()> {
63 let source = "256 / 2 + 64 * 2";
64 let tree = syntree::tree! {
65 Syntax::Operation => {
66 Syntax::Number => {
67 (Syntax::Number, 3),
68 },
69 (Syntax::Whitespace, 1),
70 Syntax::Operator => {
71 (Syntax::Div, 1),
72 },
73 (Syntax::Whitespace, 1),
74 Syntax::Number => {
75 (Syntax::Number, 1),
76 },
77 (Syntax::Whitespace, 1),
78 Syntax::Operator => {
79 (Syntax::Plus, 1),
80 },
81 (Syntax::Whitespace, 1),
82 Syntax::Operation => {
83 Syntax::Number => {
84 (Syntax::Number, 2),
85 },
86 (Syntax::Whitespace, 1),
87 Syntax::Operator => {
88 (Syntax::Mul, 1),
89 },
90 (Syntax::Whitespace, 1),
91 Syntax::Number => {
92 (Syntax::Number, 1),
93 },
94 },
95
96 }
97 };
98
99 // Embed the tree with the source code.
100 Layouter::new(&tree)
101 .with_file_path("examples/example3_1.svg")
102 .embed_with_source(source)
103 .map_err(|e| anyhow::anyhow!(e))?
104 .write()
105 .map_err(|e| anyhow::anyhow!(e))?;
106 // Embed the tree with the source code and `Display` trait.
107 Layouter::new(&tree)
108 .with_file_path("examples/example3_2.svg")
109 .embed_with_source_and_display(source)
110 .map_err(|e| anyhow::anyhow!(e))?
111 .write()
112 .map_err(|e| anyhow::anyhow!(e))
113}
Source§impl<T, F, D> Layouter<'_, T, F, D>
impl<T, F, D> Layouter<'_, T, F, D>
Sourcepub fn embed_with_source_and_display(self, source: &str) -> Result<Self>
pub fn embed_with_source_and_display(self, source: &str) -> Result<Self>
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.
Examples found in repository?
62fn main() -> Result<()> {
63 let source = "256 / 2 + 64 * 2";
64 let tree = syntree::tree! {
65 Syntax::Operation => {
66 Syntax::Number => {
67 (Syntax::Number, 3),
68 },
69 (Syntax::Whitespace, 1),
70 Syntax::Operator => {
71 (Syntax::Div, 1),
72 },
73 (Syntax::Whitespace, 1),
74 Syntax::Number => {
75 (Syntax::Number, 1),
76 },
77 (Syntax::Whitespace, 1),
78 Syntax::Operator => {
79 (Syntax::Plus, 1),
80 },
81 (Syntax::Whitespace, 1),
82 Syntax::Operation => {
83 Syntax::Number => {
84 (Syntax::Number, 2),
85 },
86 (Syntax::Whitespace, 1),
87 Syntax::Operator => {
88 (Syntax::Mul, 1),
89 },
90 (Syntax::Whitespace, 1),
91 Syntax::Number => {
92 (Syntax::Number, 1),
93 },
94 },
95
96 }
97 };
98
99 // Embed the tree with the source code.
100 Layouter::new(&tree)
101 .with_file_path("examples/example3_1.svg")
102 .embed_with_source(source)
103 .map_err(|e| anyhow::anyhow!(e))?
104 .write()
105 .map_err(|e| anyhow::anyhow!(e))?;
106 // Embed the tree with the source code and `Display` trait.
107 Layouter::new(&tree)
108 .with_file_path("examples/example3_2.svg")
109 .embed_with_source_and_display(source)
110 .map_err(|e| anyhow::anyhow!(e))?
111 .write()
112 .map_err(|e| anyhow::anyhow!(e))
113}
Source§impl<T, F, D> Layouter<'_, T, F, D>
impl<T, F, D> Layouter<'_, T, F, D>
Sourcepub fn embed_with_debug(self) -> Result<Self>
pub fn embed_with_debug(self) -> Result<Self>
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.
Examples found in repository?
29fn main() -> Result<()> {
30 // 0
31 // / \
32 // 1 2
33 // / \
34 // 3 4
35 let mut tree = Builder::new();
36
37 tree.open(MyNodeData(0)).unwrap();
38 tree.open(MyNodeData(1)).unwrap();
39 tree.token(MyNodeData(3), 1).unwrap();
40 tree.token(MyNodeData(4), 1).unwrap();
41 tree.close().unwrap();
42 tree.token(MyNodeData(2), 1).unwrap();
43 tree.close().unwrap();
44
45 let tree = tree.build().unwrap();
46 Layouter::new(&tree)
47 .with_file_path("examples/example1_vis.svg")
48 .embed_with_visualize()?
49 .write()?;
50
51 Layouter::new(&tree)
52 .with_file_path("examples/example1_deb.svg")
53 .embed_with_debug()?
54 .write()?;
55
56 Layouter::new(&tree)
57 .with_file_path("examples/example1_dis.svg")
58 .embed()?
59 .write()
60}
Source§impl<T, F, D> Layouter<'_, T, F, D>
impl<T, F, D> Layouter<'_, T, F, D>
Sourcepub fn embed(self) -> Result<Self>
pub fn embed(self) -> Result<Self>
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.
Examples found in repository?
29fn main() -> Result<()> {
30 // 0
31 // / \
32 // 1 2
33 // / \
34 // 3 4
35 let mut tree = Builder::new();
36
37 tree.open(MyNodeData(0)).unwrap();
38 tree.open(MyNodeData(1)).unwrap();
39 tree.token(MyNodeData(3), 1).unwrap();
40 tree.token(MyNodeData(4), 1).unwrap();
41 tree.close().unwrap();
42 tree.token(MyNodeData(2), 1).unwrap();
43 tree.close().unwrap();
44
45 let tree = tree.build().unwrap();
46 Layouter::new(&tree)
47 .with_file_path("examples/example1_vis.svg")
48 .embed_with_visualize()?
49 .write()?;
50
51 Layouter::new(&tree)
52 .with_file_path("examples/example1_deb.svg")
53 .embed_with_debug()?
54 .write()?;
55
56 Layouter::new(&tree)
57 .with_file_path("examples/example1_dis.svg")
58 .embed()?
59 .write()
60}
Source§impl<T, F, D> Layouter<'_, T, F, D>
impl<T, F, D> Layouter<'_, T, F, D>
Sourcepub fn embed_with(
&self,
stringify: impl Fn(&T, &mut Formatter<'_>) -> Result,
emphasize: impl Fn(&T) -> bool,
) -> Result<Self>
pub fn embed_with( &self, stringify: impl Fn(&T, &mut Formatter<'_>) -> Result, emphasize: impl Fn(&T) -> bool, ) -> Result<Self>
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 and emphasize.
§Panics
The method should not panic. If you encounter a panic this should be originated from bugs in coding. Please report such panics.