summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorseancarroll <seanc28@gmail.com>2020-12-13 21:28:40 -0600
committerseancarroll <seanc28@gmail.com>2020-12-13 21:28:40 -0600
commitd31be2dcc613dec53d7e6df6ae982834f5a46b15 (patch)
tree48e42bda40b6c36f1f78f897f51b27e7ae3e12bb
parent7eb6a81ede9e3670cc2a8ebff476689b5d346cd6 (diff)
downloaddotavious-d31be2dcc613dec53d7e6df6ae982834f5a46b15.zip
clean up to_dot_string string concatenation
Rather than appending separator at the end within a loop and then having to pop off the trailing separator characters we consume the first entry from the iter and then loop over the rest of the elements in the iter adding the separator then key/values
-rw-r--r--src/lib.rs27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/lib.rs b/src/lib.rs
index f35641c..13968d9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,6 +12,9 @@ static INDENT: &str = " ";
// TODO: should we use a hashmap that retains insertion order?
+// TODO: support adding edge based on index of nodes?
+
+
/// Most of this comes from core rust. Where to provide attribution?
/// The text for a graphviz label on a node or edge.
#[derive(Clone, PartialEq, Eq, Debug)]
@@ -246,6 +249,9 @@ where Ty: GraphType
writeln!(w, "{}{} {} {{", strict, self.graph.as_slice(), id)?;
// TODO: add global graph attributes
+ for a in self.graph.attributes {
+
+ }
for n in self.graph.nodes {
// TODO: handle render options
@@ -327,9 +333,7 @@ pub struct Graph<'a, Ty = Directed> {
// TODO: support multiple comments
pub comment: Option<String>,
- pub graph_attributes: Option<Vec<String>>,
-
- // pub nodes: Vec<&'a Node<'a>>,
+ pub attributes: HashMap<String, AttributeText<'a>>,
pub nodes: Vec<Node<'a>>,
@@ -351,7 +355,7 @@ impl<'a> Graph<'a, Directed> {
id: id,
strict: false,
comment: None,
- graph_attributes: None,
+ attributes: HashMap::new(),
nodes: Vec::new(),
edges: Vec::new(),
ty: PhantomData,
@@ -369,7 +373,7 @@ impl<'a> Graph<'a, Undirected> {
id: id,
strict: false,
comment: None,
- graph_attributes: None,
+ attributes: HashMap::new(),
nodes: Vec::new(),
edges: Vec::new(),
ty: PhantomData,
@@ -470,13 +474,14 @@ impl<'a> Node<'a> {
// I think we introduce a AttributeText enum which encodes how to write out the attribute value
if !self.attributes.is_empty() {
dot_string.push_str(" [");
- for (key, value) in &self.attributes {
- dot_string.push_str(format!("{}={}, ", key, value.to_dot_string()).as_str());
+ let mut iter = self.attributes.iter();
+ let first = iter.next().unwrap();
+ dot_string.push_str(format!("{}={}", first.0, first.1.to_dot_string()).as_str());
+ for (key, value) in iter {
+ dot_string.push_str(", ");
+ dot_string.push_str(format!("{}={}", key, value.to_dot_string()).as_str());
}
- // remove trailing comma and spae
- // TODO: make this less shitty
- dot_string.pop();
- dot_string.pop();
+
dot_string.push_str("]");
}
dot_string.push_str(";");