Layouter

Struct Layouter 

Source
pub struct Layouter<'a, T, F, D>
where T: Copy, F: Flavor, D: ?Sized + Drawer,
{ /* 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>
where T: Copy, F: Flavor,

Source

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?
examples/example1.rs (line 46)
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
Hide additional examples
examples/example3.rs (line 100)
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}
examples/example2.rs (line 151)
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>
where T: Copy, F: Flavor, D: ?Sized + Drawer,

Source

pub fn with_file_path<P>(self, path: &'a P) -> Self
where P: ?Sized + AsRef<Path>,

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?
examples/example1.rs (line 47)
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
Hide additional examples
examples/example3.rs (line 101)
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}
examples/example2.rs (line 152)
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

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");
Source

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?
examples/example1.rs (line 49)
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
Hide additional examples
examples/example3.rs (line 104)
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}
examples/example2.rs (line 155)
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

pub fn embedding(&self) -> &Embedding

Provides access to the embedding data for other uses than drawing, e.g. for tests

Source§

impl<T, F, D> Layouter<'_, T, F, D>
where T: Copy + Visualize, F: Flavor, D: ?Sized + Drawer,

Source

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?
examples/example1.rs (line 48)
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
Hide additional examples
examples/example2.rs (line 153)
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>
where T: Copy, F: Flavor, D: ?Sized + Drawer,

Source

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?
examples/example3.rs (line 102)
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>
where T: Copy + Display, F: Flavor, D: ?Sized + Drawer,

Source

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?
examples/example3.rs (line 109)
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>
where T: Copy + Debug, F: Flavor, D: ?Sized + Drawer,

Source

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?
examples/example1.rs (line 53)
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>
where T: Copy + Display, F: Flavor, D: ?Sized + Drawer,

Source

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?
examples/example1.rs (line 58)
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>
where T: Copy, F: Flavor, D: Drawer,

Source

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.

Auto Trait Implementations§

§

impl<'a, T, F, D> Freeze for Layouter<'a, T, F, D>
where D: ?Sized,

§

impl<'a, T, F, D> RefUnwindSafe for Layouter<'a, T, F, D>
where D: RefUnwindSafe + ?Sized, <F as Flavor>::Storage<Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>>: RefUnwindSafe, <F as Flavor>::Indexes: RefUnwindSafe, <F as Flavor>::Index: RefUnwindSafe, <F as Flavor>::Pointer: RefUnwindSafe,

§

impl<'a, T, F, D> Send for Layouter<'a, T, F, D>
where D: Sync + ?Sized, <F as Flavor>::Storage<Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>>: Sync, <F as Flavor>::Indexes: Sync, <F as Flavor>::Index: Sync, <F as Flavor>::Pointer: Sync,

§

impl<'a, T, F, D> Sync for Layouter<'a, T, F, D>
where D: Sync + ?Sized, <F as Flavor>::Storage<Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>>: Sync, <F as Flavor>::Indexes: Sync, <F as Flavor>::Index: Sync, <F as Flavor>::Pointer: Sync,

§

impl<'a, T, F, D> Unpin for Layouter<'a, T, F, D>
where D: ?Sized,

§

impl<'a, T, F, D> UnwindSafe for Layouter<'a, T, F, D>
where D: RefUnwindSafe + ?Sized, <F as Flavor>::Storage<Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>>: RefUnwindSafe, <F as Flavor>::Indexes: RefUnwindSafe, <F as Flavor>::Index: RefUnwindSafe, <F as Flavor>::Pointer: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.