fkl_dot/
subgraph.rs

1use std::fmt;
2
3use crate::edge::Edge;
4use crate::helper::config::indent;
5use crate::helper::naming::cluster_name;
6use crate::node::Node;
7
8pub struct Subgraph {
9  name: String,
10  label: String,
11  // for indent
12  depth: usize,
13  nodes: Vec<Node>,
14  edges: Vec<Edge>,
15  subgraph: Vec<Subgraph>,
16}
17
18impl Subgraph {
19  pub fn new(name: &str, label: &str) -> Self {
20    Subgraph {
21      name: cluster_name(name),
22      label: label.to_string(),
23      depth: 1,
24      nodes: Vec::new(),
25      edges: Vec::new(),
26      subgraph: Vec::new(),
27    }
28  }
29
30  pub fn add_subgraph(&mut self, subgraph: Subgraph) {
31    self.subgraph.push(subgraph);
32  }
33
34  pub fn add_node(&mut self, node: Node) {
35    self.nodes.push(node);
36  }
37
38  pub fn set_depth(&mut self, depth: usize) {
39    self.depth = depth;
40  }
41}
42
43impl fmt::Display for Subgraph {
44  fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
45    out.write_str(&format!("{}subgraph {} {{\n", indent(self.depth), self.name))?;
46
47    let space = indent(self.depth + 1);
48
49    out.write_str(&format!("{}label=\"{}\";\n", space, self.label))?;
50
51    for node in &self.nodes {
52      out.write_str(&format!("{}{}\n", space, node))?
53    }
54
55    for edge in &self.edges {
56      out.write_str(&format!("{}{}\n", space, edge))?
57    }
58
59    for subgraph in &self.subgraph {
60      out.write_str(&format!("\n{}\n", subgraph))?
61    }
62
63    out.write_str(&format!("{}}}", indent(self.depth)))
64  }
65}