From e33b0b90e3a683a6c0431a50a8eb293aa3c3e326 Mon Sep 17 00:00:00 2001 From: seancarroll Date: Sat, 16 Jan 2021 14:59:07 -0600 Subject: Remove Attribute Statement Structs for IndexMap I didnt think we were getting a ton of benefit from the AttributeStatement abstraction so I replaced it with IndexMap which the attribute statement impl were using internally. While the DOT language docs (https://graphviz.org/doc/info/lang.html) do call out attr_stmt as part of the language definition the overhead of the related traits, impl, structs, etc felt a bit heavy. In particular, supporting the ability to create them as part of a build as well as allowing users to add to them via other bulid fns was a bit awkward. For now I think removing the abstraction makes sense and provides for a simpler implementation. Can revisit this down the road if other requirements come up that perhaps warrant the addittional code. --- tests/dot.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) (limited to 'tests/dot.rs') diff --git a/tests/dot.rs b/tests/dot.rs index 526ff83..c4d31ba 100644 --- a/tests/dot.rs +++ b/tests/dot.rs @@ -1,5 +1,5 @@ use dotavious::attributes::{ - AttributeText, Color, CompassPoint, EdgeAttributes, EdgeStyle, + AttributeText, AttributeType, Color, CompassPoint, EdgeAttributes, EdgeStyle, GraphAttributeStatementBuilder, GraphAttributes, GraphStyle, NodeAttributes, NodeStyle, PortPosition, RankDir, Shape, }; @@ -298,6 +298,84 @@ fn port_position_attribute() { #[test] fn graph_attributes() { + let g = GraphBuilder::new_directed(Some("graph_attributes".to_string())) + .add_attribute( + AttributeType::Graph, + "rankdir".to_string(), + AttributeText::from(RankDir::LeftRight), + ) + .add_attribute( + AttributeType::Node, + "style".to_string(), + AttributeText::from(NodeStyle::Filled), + ) + .add_attribute( + AttributeType::Edge, + "color".to_string(), + AttributeText::from(Color::Named("red")), + ) + .build(); + + let r = test_input(g); + + assert_eq!( + r.unwrap(), + r#"digraph graph_attributes { + graph [rankdir=LR]; + node [style=filled]; + edge [color="red"]; +} +"# + ); +} + +#[test] +fn graph_attributes_extend() { + let g = GraphBuilder::new_directed(Some("graph_attributes".to_string())) + .extend_with_attributes( + AttributeType::Graph, + [( + "rankdir".to_string(), + AttributeText::from(RankDir::LeftRight), + )] + .iter() + .cloned() + .collect(), + ) + .extend_with_attributes( + AttributeType::Node, + [("style".to_string(), AttributeText::from(NodeStyle::Filled))] + .iter() + .cloned() + .collect(), + ) + .extend_with_attributes( + AttributeType::Edge, + [( + "color".to_string(), + AttributeText::from(Color::Named("red")), + )] + .iter() + .cloned() + .collect(), + ) + .build(); + + let r = test_input(g); + + assert_eq!( + r.unwrap(), + r#"digraph graph_attributes { + graph [rankdir=LR]; + node [style=filled]; + edge [color="red"]; +} +"# + ); +} + +#[test] +fn graph_attributes_statement_builders() { let graph_attributes = GraphAttributeStatementBuilder::new() .rank_dir(RankDir::LeftRight) .build(); -- cgit v1.2.3