1
Fork 0

rustfmt libgraphviz

This commit is contained in:
Nick Cameron 2015-09-05 15:44:26 +12:00
parent 01024c9f61
commit 1831bf8fb5

View file

@ -413,8 +413,9 @@ impl<'a> Id<'a> {
{ {
let mut chars = name.chars(); let mut chars = name.chars();
match chars.next() { match chars.next() {
Some(c) if is_letter_or_underscore(c) => { ; }, Some(c) if is_letter_or_underscore(c) => { ;
_ => return Err(()) }
_ => return Err(()),
} }
if !chars.all(is_constituent) { if !chars.all(is_constituent) {
return Err(()) return Err(())
@ -505,24 +506,28 @@ pub fn escape_html(s: &str) -> String {
} }
impl<'a> LabelText<'a> { impl<'a> LabelText<'a> {
pub fn label<S:IntoCow<'a, str>>(s: S) -> LabelText<'a> { pub fn label<S: IntoCow<'a, str>>(s: S) -> LabelText<'a> {
LabelStr(s.into_cow()) LabelStr(s.into_cow())
} }
pub fn escaped<S:IntoCow<'a, str>>(s: S) -> LabelText<'a> { pub fn escaped<S: IntoCow<'a, str>>(s: S) -> LabelText<'a> {
EscStr(s.into_cow()) EscStr(s.into_cow())
} }
pub fn html<S:IntoCow<'a, str>>(s: S) -> LabelText<'a> { pub fn html<S: IntoCow<'a, str>>(s: S) -> LabelText<'a> {
HtmlStr(s.into_cow()) HtmlStr(s.into_cow())
} }
fn escape_char<F>(c: char, mut f: F) where F: FnMut(char) { fn escape_char<F>(c: char, mut f: F)
where F: FnMut(char)
{
match c { match c {
// not escaping \\, since Graphviz escString needs to // not escaping \\, since Graphviz escString needs to
// interpret backslashes; see EscStr above. // interpret backslashes; see EscStr above.
'\\' => f(c), '\\' => f(c),
_ => for c in c.escape_default() { f(c) } _ => for c in c.escape_default() {
f(c)
},
} }
} }
fn escape_str(s: &str) -> String { fn escape_str(s: &str) -> String {
@ -613,29 +618,42 @@ pub enum RenderOption {
} }
/// Returns vec holding all the default render options. /// Returns vec holding all the default render options.
pub fn default_options() -> Vec<RenderOption> { vec![] } pub fn default_options() -> Vec<RenderOption> {
vec![]
}
/// Renders directed graph `g` into the writer `w` in DOT syntax. /// Renders directed graph `g` into the writer `w` in DOT syntax.
/// (Simple wrapper around `render_opts` that passes a default set of options.) /// (Simple wrapper around `render_opts` that passes a default set of options.)
pub fn render<'a, N:Clone+'a, E:Clone+'a, G:Labeller<'a,N,E>+GraphWalk<'a,N,E>, W:Write>( pub fn render<'a,
g: &'a G, N: Clone + 'a,
w: &mut W) -> io::Result<()> { E: Clone + 'a,
G: Labeller<'a, N, E> + GraphWalk<'a, N, E>,
W: Write>
(g: &'a G,
w: &mut W)
-> io::Result<()> {
render_opts(g, w, &[]) render_opts(g, w, &[])
} }
/// Renders directed graph `g` into the writer `w` in DOT syntax. /// Renders directed graph `g` into the writer `w` in DOT syntax.
/// (Main entry point for the library.) /// (Main entry point for the library.)
pub fn render_opts<'a, N:Clone+'a, E:Clone+'a, G:Labeller<'a,N,E>+GraphWalk<'a,N,E>, W:Write>( pub fn render_opts<'a,
g: &'a G, N: Clone + 'a,
E: Clone + 'a,
G: Labeller<'a, N, E> + GraphWalk<'a, N, E>,
W: Write>
(g: &'a G,
w: &mut W, w: &mut W,
options: &[RenderOption]) -> io::Result<()> options: &[RenderOption])
{ -> io::Result<()> {
fn writeln<W:Write>(w: &mut W, arg: &[&str]) -> io::Result<()> { fn writeln<W: Write>(w: &mut W, arg: &[&str]) -> io::Result<()> {
for &s in arg { try!(w.write_all(s.as_bytes())); } for &s in arg {
try!(w.write_all(s.as_bytes()));
}
write!(w, "\n") write!(w, "\n")
} }
fn indent<W:Write>(w: &mut W) -> io::Result<()> { fn indent<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b" ") w.write_all(b" ")
} }
@ -748,7 +766,7 @@ mod tests {
// A simple wrapper around LabelledGraph that forces the labels to // A simple wrapper around LabelledGraph that forces the labels to
// be emitted as EscStr. // be emitted as EscStr.
struct LabelledGraphWithEscStrs { struct LabelledGraphWithEscStrs {
graph: LabelledGraph graph: LabelledGraph,
} }
enum NodeLabels<L> { enum NodeLabels<L> {
@ -762,13 +780,10 @@ mod tests {
impl NodeLabels<&'static str> { impl NodeLabels<&'static str> {
fn to_opt_strs(self) -> Vec<Option<&'static str>> { fn to_opt_strs(self) -> Vec<Option<&'static str>> {
match self { match self {
UnlabelledNodes(len) UnlabelledNodes(len) => vec![None; len],
=> vec![None; len], AllNodesLabelled(lbls) => lbls.into_iter().map(
AllNodesLabelled(lbls)
=> lbls.into_iter().map(
|l|Some(l)).collect(), |l|Some(l)).collect(),
SomeNodesLabelled(lbls) SomeNodesLabelled(lbls) => lbls.into_iter().collect(),
=> lbls.into_iter().collect(),
} }
} }
@ -785,7 +800,8 @@ mod tests {
fn new(name: &'static str, fn new(name: &'static str,
node_labels: Trivial, node_labels: Trivial,
edges: Vec<Edge>, edges: Vec<Edge>,
node_styles: Option<Vec<Style>>) -> LabelledGraph { node_styles: Option<Vec<Style>>)
-> LabelledGraph {
let count = node_labels.len(); let count = node_labels.len();
LabelledGraph { LabelledGraph {
name: name, name: name,
@ -794,7 +810,7 @@ mod tests {
node_styles: match node_styles { node_styles: match node_styles {
Some(nodes) => nodes, Some(nodes) => nodes,
None => vec![Style::None; count], None => vec![Style::None; count],
} },
} }
} }
} }
@ -802,13 +818,9 @@ mod tests {
impl LabelledGraphWithEscStrs { impl LabelledGraphWithEscStrs {
fn new(name: &'static str, fn new(name: &'static str,
node_labels: Trivial, node_labels: Trivial,
edges: Vec<Edge>) -> LabelledGraphWithEscStrs { edges: Vec<Edge>)
LabelledGraphWithEscStrs { -> LabelledGraphWithEscStrs {
graph: LabelledGraph::new(name, LabelledGraphWithEscStrs { graph: LabelledGraph::new(name, node_labels, edges, None) }
node_labels,
edges,
None)
}
} }
} }
@ -829,26 +841,30 @@ mod tests {
None => LabelStr(id_name(n).name()), None => LabelStr(id_name(n).name()),
} }
} }
fn edge_label(&'a self, e: & &'a Edge) -> LabelText<'a> { fn edge_label(&'a self, e: &&'a Edge) -> LabelText<'a> {
LabelStr(e.label.into_cow()) LabelStr(e.label.into_cow())
} }
fn node_style(&'a self, n: &Node) -> Style { fn node_style(&'a self, n: &Node) -> Style {
self.node_styles[*n] self.node_styles[*n]
} }
fn edge_style(&'a self, e: & &'a Edge) -> Style { fn edge_style(&'a self, e: &&'a Edge) -> Style {
e.style e.style
} }
} }
impl<'a> Labeller<'a, Node, &'a Edge> for LabelledGraphWithEscStrs { impl<'a> Labeller<'a, Node, &'a Edge> for LabelledGraphWithEscStrs {
fn graph_id(&'a self) -> Id<'a> { self.graph.graph_id() } fn graph_id(&'a self) -> Id<'a> {
fn node_id(&'a self, n: &Node) -> Id<'a> { self.graph.node_id(n) } self.graph.graph_id()
}
fn node_id(&'a self, n: &Node) -> Id<'a> {
self.graph.node_id(n)
}
fn node_label(&'a self, n: &Node) -> LabelText<'a> { fn node_label(&'a self, n: &Node) -> LabelText<'a> {
match self.graph.node_label(n) { match self.graph.node_label(n) {
LabelStr(s) | EscStr(s) | HtmlStr(s) => EscStr(s), LabelStr(s) | EscStr(s) | HtmlStr(s) => EscStr(s),
} }
} }
fn edge_label(&'a self, e: & &'a Edge) -> LabelText<'a> { fn edge_label(&'a self, e: &&'a Edge) -> LabelText<'a> {
match self.graph.edge_label(e) { match self.graph.edge_label(e) {
LabelStr(s) | EscStr(s) | HtmlStr(s) => EscStr(s), LabelStr(s) | EscStr(s) | HtmlStr(s) => EscStr(s),
} }
@ -856,31 +872,31 @@ mod tests {
} }
impl<'a> GraphWalk<'a, Node, &'a Edge> for LabelledGraph { impl<'a> GraphWalk<'a, Node, &'a Edge> for LabelledGraph {
fn nodes(&'a self) -> Nodes<'a,Node> { fn nodes(&'a self) -> Nodes<'a, Node> {
(0..self.node_labels.len()).collect() (0..self.node_labels.len()).collect()
} }
fn edges(&'a self) -> Edges<'a,&'a Edge> { fn edges(&'a self) -> Edges<'a, &'a Edge> {
self.edges.iter().collect() self.edges.iter().collect()
} }
fn source(&'a self, edge: & &'a Edge) -> Node { fn source(&'a self, edge: &&'a Edge) -> Node {
edge.from edge.from
} }
fn target(&'a self, edge: & &'a Edge) -> Node { fn target(&'a self, edge: &&'a Edge) -> Node {
edge.to edge.to
} }
} }
impl<'a> GraphWalk<'a, Node, &'a Edge> for LabelledGraphWithEscStrs { impl<'a> GraphWalk<'a, Node, &'a Edge> for LabelledGraphWithEscStrs {
fn nodes(&'a self) -> Nodes<'a,Node> { fn nodes(&'a self) -> Nodes<'a, Node> {
self.graph.nodes() self.graph.nodes()
} }
fn edges(&'a self) -> Edges<'a,&'a Edge> { fn edges(&'a self) -> Edges<'a, &'a Edge> {
self.graph.edges() self.graph.edges()
} }
fn source(&'a self, edge: & &'a Edge) -> Node { fn source(&'a self, edge: &&'a Edge) -> Node {
edge.from edge.from
} }
fn target(&'a self, edge: & &'a Edge) -> Node { fn target(&'a self, edge: &&'a Edge) -> Node {
edge.to edge.to
} }
} }
@ -899,7 +915,7 @@ mod tests {
#[test] #[test]
fn empty_graph() { fn empty_graph() {
let labels : Trivial = UnlabelledNodes(0); let labels: Trivial = UnlabelledNodes(0);
let r = test_input(LabelledGraph::new("empty_graph", labels, vec![], None)); let r = test_input(LabelledGraph::new("empty_graph", labels, vec![], None));
assert_eq!(r.unwrap(), assert_eq!(r.unwrap(),
r#"digraph empty_graph { r#"digraph empty_graph {
@ -909,7 +925,7 @@ r#"digraph empty_graph {
#[test] #[test]
fn single_node() { fn single_node() {
let labels : Trivial = UnlabelledNodes(1); let labels: Trivial = UnlabelledNodes(1);
let r = test_input(LabelledGraph::new("single_node", labels, vec![], None)); let r = test_input(LabelledGraph::new("single_node", labels, vec![], None));
assert_eq!(r.unwrap(), assert_eq!(r.unwrap(),
r#"digraph single_node { r#"digraph single_node {
@ -920,7 +936,7 @@ r#"digraph single_node {
#[test] #[test]
fn single_node_with_style() { fn single_node_with_style() {
let labels : Trivial = UnlabelledNodes(1); let labels: Trivial = UnlabelledNodes(1);
let styles = Some(vec![Style::Dashed]); let styles = Some(vec![Style::Dashed]);
let r = test_input(LabelledGraph::new("single_node", labels, vec![], styles)); let r = test_input(LabelledGraph::new("single_node", labels, vec![], styles));
assert_eq!(r.unwrap(), assert_eq!(r.unwrap(),
@ -932,9 +948,11 @@ r#"digraph single_node {
#[test] #[test]
fn single_edge() { fn single_edge() {
let labels : Trivial = UnlabelledNodes(2); let labels: Trivial = UnlabelledNodes(2);
let result = test_input(LabelledGraph::new("single_edge", labels, let result = test_input(LabelledGraph::new("single_edge",
vec![edge(0, 1, "E", Style::None)], None)); labels,
vec![edge(0, 1, "E", Style::None)],
None));
assert_eq!(result.unwrap(), assert_eq!(result.unwrap(),
r#"digraph single_edge { r#"digraph single_edge {
N0[label="N0"]; N0[label="N0"];
@ -946,9 +964,11 @@ r#"digraph single_edge {
#[test] #[test]
fn single_edge_with_style() { fn single_edge_with_style() {
let labels : Trivial = UnlabelledNodes(2); let labels: Trivial = UnlabelledNodes(2);
let result = test_input(LabelledGraph::new("single_edge", labels, let result = test_input(LabelledGraph::new("single_edge",
vec![edge(0, 1, "E", Style::Bold)], None)); labels,
vec![edge(0, 1, "E", Style::Bold)],
None));
assert_eq!(result.unwrap(), assert_eq!(result.unwrap(),
r#"digraph single_edge { r#"digraph single_edge {
N0[label="N0"]; N0[label="N0"];
@ -960,10 +980,12 @@ r#"digraph single_edge {
#[test] #[test]
fn test_some_labelled() { fn test_some_labelled() {
let labels : Trivial = SomeNodesLabelled(vec![Some("A"), None]); let labels: Trivial = SomeNodesLabelled(vec![Some("A"), None]);
let styles = Some(vec![Style::None, Style::Dotted]); let styles = Some(vec![Style::None, Style::Dotted]);
let result = test_input(LabelledGraph::new("test_some_labelled", labels, let result = test_input(LabelledGraph::new("test_some_labelled",
vec![edge(0, 1, "A-1", Style::None)], styles)); labels,
vec![edge(0, 1, "A-1", Style::None)],
styles));
assert_eq!(result.unwrap(), assert_eq!(result.unwrap(),
r#"digraph test_some_labelled { r#"digraph test_some_labelled {
N0[label="A"]; N0[label="A"];
@ -975,9 +997,11 @@ r#"digraph test_some_labelled {
#[test] #[test]
fn single_cyclic_node() { fn single_cyclic_node() {
let labels : Trivial = UnlabelledNodes(1); let labels: Trivial = UnlabelledNodes(1);
let r = test_input(LabelledGraph::new("single_cyclic_node", labels, let r = test_input(LabelledGraph::new("single_cyclic_node",
vec![edge(0, 0, "E", Style::None)], None)); labels,
vec![edge(0, 0, "E", Style::None)],
None));
assert_eq!(r.unwrap(), assert_eq!(r.unwrap(),
r#"digraph single_cyclic_node { r#"digraph single_cyclic_node {
N0[label="N0"]; N0[label="N0"];
@ -989,8 +1013,8 @@ r#"digraph single_cyclic_node {
#[test] #[test]
fn hasse_diagram() { fn hasse_diagram() {
let labels = AllNodesLabelled(vec!("{x,y}", "{x}", "{y}", "{}")); let labels = AllNodesLabelled(vec!("{x,y}", "{x}", "{y}", "{}"));
let r = test_input(LabelledGraph::new( let r = test_input(LabelledGraph::new("hasse_diagram",
"hasse_diagram", labels, labels,
vec![edge(0, 1, "", Style::None), edge(0, 2, "", Style::None), vec![edge(0, 1, "", Style::None), edge(0, 2, "", Style::None),
edge(1, 3, "", Style::None), edge(2, 3, "", Style::None)], edge(1, 3, "", Style::None), edge(2, 3, "", Style::None)],
None)); None));
@ -1024,8 +1048,8 @@ r#"digraph hasse_diagram {
let mut writer = Vec::new(); let mut writer = Vec::new();
let g = LabelledGraphWithEscStrs::new( let g = LabelledGraphWithEscStrs::new("syntax_tree",
"syntax_tree", labels, labels,
vec![edge(0, 1, "then", Style::None), edge(0, 2, "else", Style::None), vec![edge(0, 1, "then", Style::None), edge(0, 2, "else", Style::None),
edge(1, 3, ";", Style::None), edge(2, 3, ";", Style::None)]); edge(1, 3, ";", Style::None), edge(2, 3, ";", Style::None)]);
@ -1051,8 +1075,9 @@ r#"digraph syntax_tree {
fn simple_id_construction() { fn simple_id_construction() {
let id1 = Id::new("hello"); let id1 = Id::new("hello");
match id1 { match id1 {
Ok(_) => {;}, Ok(_) => {;
Err(..) => panic!("'hello' is not a valid value for id anymore") }
Err(..) => panic!("'hello' is not a valid value for id anymore"),
} }
} }
@ -1061,7 +1086,8 @@ r#"digraph syntax_tree {
let id2 = Id::new("Weird { struct : ure } !!!"); let id2 = Id::new("Weird { struct : ure } !!!");
match id2 { match id2 {
Ok(_) => panic!("graphviz id suddenly allows spaces, brackets and stuff"), Ok(_) => panic!("graphviz id suddenly allows spaces, brackets and stuff"),
Err(..) => {;} Err(..) => {;
}
} }
} }
} }