rustfmt libgraphviz
This commit is contained in:
parent
01024c9f61
commit
1831bf8fb5
1 changed files with 100 additions and 74 deletions
|
@ -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(())
|
||||||
|
@ -517,12 +518,16 @@ impl<'a> 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,25 +618,38 @@ 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")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -841,8 +853,12 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
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),
|
||||||
|
@ -933,8 +949,10 @@ 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"];
|
||||||
|
@ -947,8 +965,10 @@ 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"];
|
||||||
|
@ -962,8 +982,10 @@ r#"digraph single_edge {
|
||||||
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"];
|
||||||
|
@ -976,8 +998,10 @@ 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(..) => {;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue