diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:04 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:47 -0500 |
| commit | a06baa56b95674fc626b3c3fd680d6a65357fe60 (patch) | |
| tree | cd9d867c2ca3cff5c1d6b3bd73377c44649fb075 /src/libgraphviz | |
| parent | 8eb7c58dbb7b32701af113bc58722d0d1fefb1eb (diff) | |
| download | rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.tar.gz rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.zip | |
Format the world
Diffstat (limited to 'src/libgraphviz')
| -rw-r--r-- | src/libgraphviz/lib.rs | 48 | ||||
| -rw-r--r-- | src/libgraphviz/tests.rs | 178 |
2 files changed, 126 insertions, 100 deletions
diff --git a/src/libgraphviz/lib.rs b/src/libgraphviz/lib.rs index bb996e5f906..d04f5c1d4ee 100644 --- a/src/libgraphviz/lib.rs +++ b/src/libgraphviz/lib.rs @@ -271,16 +271,17 @@ //! //! * [DOT language](http://www.graphviz.org/doc/info/lang.html) -#![doc(html_root_url = "https://doc.rust-lang.org/nightly/", - test(attr(allow(unused_variables), deny(warnings))))] - +#![doc( + html_root_url = "https://doc.rust-lang.org/nightly/", + test(attr(allow(unused_variables), deny(warnings))) +)] #![feature(nll)] use LabelText::*; use std::borrow::Cow; -use std::io::prelude::*; use std::io; +use std::io::prelude::*; /// The text for a graphviz label on a node or edge. pub enum LabelText<'a> { @@ -402,7 +403,7 @@ impl<'a> Id<'a> { Some(c) if c.is_ascii_alphabetic() || c == '_' => {} _ => return Err(()), } - if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' ) { + if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') { return Err(()); } @@ -475,10 +476,7 @@ pub trait Labeller<'a> { /// Escape tags in such a way that it is suitable for inclusion in a /// Graphviz HTML label. pub fn escape_html(s: &str) -> String { - s.replace("&", "&") - .replace("\"", """) - .replace("<", "<") - .replace(">", ">") + s.replace("&", "&").replace("\"", """).replace("<", "<").replace(">", ">") } impl<'a> LabelText<'a> { @@ -495,7 +493,8 @@ impl<'a> LabelText<'a> { } fn escape_char<F>(c: char, mut f: F) - where F: FnMut(char) + where + F: FnMut(char), { match c { // not escaping \\, since Graphviz escString needs to @@ -559,8 +558,8 @@ impl<'a> LabelText<'a> { } } -pub type Nodes<'a,N> = Cow<'a,[N]>; -pub type Edges<'a,E> = Cow<'a,[E]>; +pub type Nodes<'a, N> = Cow<'a, [N]>; +pub type Edges<'a, E> = Cow<'a, [E]>; // (The type parameters in GraphWalk should be associated items, // when/if Rust supports such.) @@ -607,25 +606,24 @@ pub fn default_options() -> Vec<RenderOption> { /// Renders directed graph `g` into the writer `w` in DOT syntax. /// (Simple wrapper around `render_opts` that passes a default set of options.) -pub fn render<'a,N,E,G,W>(g: &'a G, w: &mut W) -> io::Result<()> - where N: Clone + 'a, - E: Clone + 'a, - G: Labeller<'a, Node=N, Edge=E> + GraphWalk<'a, Node=N, Edge=E>, - W: Write +pub fn render<'a, N, E, G, W>(g: &'a G, w: &mut W) -> io::Result<()> +where + N: Clone + 'a, + E: Clone + 'a, + G: Labeller<'a, Node = N, Edge = E> + GraphWalk<'a, Node = N, Edge = E>, + W: Write, { render_opts(g, w, &[]) } /// Renders directed graph `g` into the writer `w` in DOT syntax. /// (Main entry point for the library.) -pub fn render_opts<'a, N, E, G, W>(g: &'a G, - w: &mut W, - options: &[RenderOption]) - -> io::Result<()> - where N: Clone + 'a, - E: Clone + 'a, - G: Labeller<'a, Node=N, Edge=E> + GraphWalk<'a, Node=N, Edge=E>, - W: Write +pub fn render_opts<'a, N, E, G, W>(g: &'a G, w: &mut W, options: &[RenderOption]) -> io::Result<()> +where + N: Clone + 'a, + E: Clone + 'a, + G: Labeller<'a, Node = N, Edge = E> + GraphWalk<'a, Node = N, Edge = E>, + W: Write, { writeln!(w, "digraph {} {{", g.graph_id().as_slice())?; for n in g.nodes().iter() { diff --git a/src/libgraphviz/tests.rs b/src/libgraphviz/tests.rs index 2f713579b17..055e13156ae 100644 --- a/src/libgraphviz/tests.rs +++ b/src/libgraphviz/tests.rs @@ -1,8 +1,8 @@ -use NodeLabels::*; -use super::{Id, Labeller, Nodes, Edges, GraphWalk, render, Style}; -use super::LabelText::{self, LabelStr, EscStr, HtmlStr}; +use super::LabelText::{self, EscStr, HtmlStr, LabelStr}; +use super::{render, Edges, GraphWalk, Id, Labeller, Nodes, Style}; use std::io; use std::io::prelude::*; +use NodeLabels::*; /// each node is an index in a vector in the graph. type Node = usize; @@ -14,12 +14,7 @@ struct Edge { } fn edge(from: usize, to: usize, label: &'static str, style: Style) -> Edge { - Edge { - from, - to, - label, - style, - } + Edge { from, to, label, style } } struct LabelledGraph { @@ -75,11 +70,12 @@ impl NodeLabels<&'static str> { } impl LabelledGraph { - fn new(name: &'static str, - node_labels: Trivial, - edges: Vec<Edge>, - node_styles: Option<Vec<Style>>) - -> LabelledGraph { + fn new( + name: &'static str, + node_labels: Trivial, + edges: Vec<Edge>, + node_styles: Option<Vec<Style>>, + ) -> LabelledGraph { let count = node_labels.len(); LabelledGraph { name, @@ -94,10 +90,7 @@ impl LabelledGraph { } impl LabelledGraphWithEscStrs { - fn new(name: &'static str, - node_labels: Trivial, - edges: Vec<Edge>) - -> LabelledGraphWithEscStrs { + fn new(name: &'static str, node_labels: Trivial, edges: Vec<Edge>) -> LabelledGraphWithEscStrs { LabelledGraphWithEscStrs { graph: LabelledGraph::new(name, node_labels, edges, None) } } } @@ -203,21 +196,25 @@ fn test_input(g: LabelledGraph) -> io::Result<String> { fn empty_graph() { let labels: Trivial = UnlabelledNodes(0); let r = test_input(LabelledGraph::new("empty_graph", labels, vec![], None)); - assert_eq!(r.unwrap(), -r#"digraph empty_graph { + assert_eq!( + r.unwrap(), + r#"digraph empty_graph { } -"#); +"# + ); } #[test] fn single_node() { let labels: Trivial = UnlabelledNodes(1); let r = test_input(LabelledGraph::new("single_node", labels, vec![], None)); - assert_eq!(r.unwrap(), -r#"digraph single_node { + assert_eq!( + r.unwrap(), + r#"digraph single_node { N0[label="N0"]; } -"#); +"# + ); } #[test] @@ -225,89 +222,112 @@ fn single_node_with_style() { let labels: Trivial = UnlabelledNodes(1); let styles = Some(vec![Style::Dashed]); let r = test_input(LabelledGraph::new("single_node", labels, vec![], styles)); - assert_eq!(r.unwrap(), -r#"digraph single_node { + assert_eq!( + r.unwrap(), + r#"digraph single_node { N0[label="N0"][style="dashed"]; } -"#); +"# + ); } #[test] fn single_edge() { let labels: Trivial = UnlabelledNodes(2); - let result = test_input(LabelledGraph::new("single_edge", - labels, - vec![edge(0, 1, "E", Style::None)], - None)); - assert_eq!(result.unwrap(), -r#"digraph single_edge { + let result = test_input(LabelledGraph::new( + "single_edge", + labels, + vec![edge(0, 1, "E", Style::None)], + None, + )); + assert_eq!( + result.unwrap(), + r#"digraph single_edge { N0[label="N0"]; N1[label="N1"]; N0 -> N1[label="E"]; } -"#); +"# + ); } #[test] fn single_edge_with_style() { let labels: Trivial = UnlabelledNodes(2); - let result = test_input(LabelledGraph::new("single_edge", - labels, - vec![edge(0, 1, "E", Style::Bold)], - None)); - assert_eq!(result.unwrap(), -r#"digraph single_edge { + let result = test_input(LabelledGraph::new( + "single_edge", + labels, + vec![edge(0, 1, "E", Style::Bold)], + None, + )); + assert_eq!( + result.unwrap(), + r#"digraph single_edge { N0[label="N0"]; N1[label="N1"]; N0 -> N1[label="E"][style="bold"]; } -"#); +"# + ); } #[test] fn test_some_labelled() { let labels: Trivial = SomeNodesLabelled(vec![Some("A"), None]); let styles = Some(vec![Style::None, Style::Dotted]); - let result = test_input(LabelledGraph::new("test_some_labelled", - labels, - vec![edge(0, 1, "A-1", Style::None)], - styles)); - assert_eq!(result.unwrap(), -r#"digraph test_some_labelled { + let result = test_input(LabelledGraph::new( + "test_some_labelled", + labels, + vec![edge(0, 1, "A-1", Style::None)], + styles, + )); + assert_eq!( + result.unwrap(), + r#"digraph test_some_labelled { N0[label="A"]; N1[label="N1"][style="dotted"]; N0 -> N1[label="A-1"]; } -"#); +"# + ); } #[test] fn single_cyclic_node() { let labels: Trivial = UnlabelledNodes(1); - let r = test_input(LabelledGraph::new("single_cyclic_node", - labels, - vec![edge(0, 0, "E", Style::None)], - None)); - assert_eq!(r.unwrap(), -r#"digraph single_cyclic_node { + let r = test_input(LabelledGraph::new( + "single_cyclic_node", + labels, + vec![edge(0, 0, "E", Style::None)], + None, + )); + assert_eq!( + r.unwrap(), + r#"digraph single_cyclic_node { N0[label="N0"]; N0 -> N0[label="E"]; } -"#); +"# + ); } #[test] fn hasse_diagram() { let labels = AllNodesLabelled(vec!["{x,y}", "{x}", "{y}", "{}"]); - let r = test_input(LabelledGraph::new("hasse_diagram", - labels, - vec![edge(0, 1, "", Style::None), - edge(0, 2, "", Style::None), - edge(1, 3, "", Style::None), - edge(2, 3, "", Style::None)], - None)); - assert_eq!(r.unwrap(), -r#"digraph hasse_diagram { + let r = test_input(LabelledGraph::new( + "hasse_diagram", + labels, + vec![ + edge(0, 1, "", Style::None), + edge(0, 2, "", Style::None), + edge(1, 3, "", Style::None), + edge(2, 3, "", Style::None), + ], + None, + )); + assert_eq!( + r.unwrap(), + r#"digraph hasse_diagram { N0[label="{x,y}"]; N1[label="{x}"]; N2[label="{y}"]; @@ -317,7 +337,8 @@ r#"digraph hasse_diagram { N1 -> N3[label=""]; N2 -> N3[label=""]; } -"#); +"# + ); } #[test] @@ -332,23 +353,29 @@ fn left_aligned_text() { \\l", "branch1", "branch2", - "afterward"]); + "afterward", + ]); let mut writer = Vec::new(); - let g = LabelledGraphWithEscStrs::new("syntax_tree", - labels, - vec![edge(0, 1, "then", Style::None), - edge(0, 2, "else", Style::None), - edge(1, 3, ";", Style::None), - edge(2, 3, ";", Style::None)]); + let g = LabelledGraphWithEscStrs::new( + "syntax_tree", + labels, + vec![ + edge(0, 1, "then", Style::None), + edge(0, 2, "else", Style::None), + edge(1, 3, ";", Style::None), + edge(2, 3, ";", Style::None), + ], + ); render(&g, &mut writer).unwrap(); let mut r = String::new(); Read::read_to_string(&mut &*writer, &mut r).unwrap(); - assert_eq!(r, -r#"digraph syntax_tree { + assert_eq!( + r, + r#"digraph syntax_tree { N0[label="if test {\l branch1\l} else {\l branch2\l}\lafterward\l"]; N1[label="branch1"]; N2[label="branch2"]; @@ -358,7 +385,8 @@ r#"digraph syntax_tree { N1 -> N3[label=";"]; N2 -> N3[label=";"]; } -"#); +"# + ); } #[test] |
