xdot/layout/
graph_ext.rs
1use std::iter::{empty, once};
2
3use graphviz_rust::dot_structures::{Edge, Graph, Node, Stmt, Subgraph};
4
5pub(super) enum Elem<'a> {
6 Node(&'a Node),
7 Edge(&'a Edge),
8}
9
10pub(super) trait GraphExt {
11 fn iter_elems<'a>(&'a self) -> Box<dyn Iterator<Item = Elem<'a>> + 'a>;
12}
13
14impl GraphExt for Stmt {
15 fn iter_elems<'a>(&'a self) -> Box<dyn Iterator<Item = Elem<'a>> + 'a> {
16 match self {
17 Stmt::Edge(edge) => Box::new(once(Elem::Edge(edge))),
18 Stmt::Node(node) => Box::new(once(Elem::Node(node))),
19 Stmt::Subgraph(sg) => sg.iter_elems(),
20 _ => Box::new(empty()),
21 }
22 }
23}
24
25impl GraphExt for Subgraph {
26 fn iter_elems<'a>(&'a self) -> Box<dyn Iterator<Item = Elem<'a>> + 'a> {
27 Box::new(self.stmts.iter().flat_map(Stmt::iter_elems))
28 }
29}
30
31impl GraphExt for Graph {
32 fn iter_elems<'a>(&'a self) -> Box<dyn Iterator<Item = Elem<'a>> + 'a> {
33 let stmts = match self {
34 Graph::Graph { stmts, .. } => stmts,
35 Graph::DiGraph { stmts, .. } => stmts,
36 };
37 Box::new(stmts.iter().flat_map(Stmt::iter_elems))
38 }
39}