From 2e2defdfce2495adbf4f3815525d9c9f08ea4707 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 17 Dec 2017 13:04:42 +0100 Subject: [PATCH] Cleanup for libgraphviz --- src/libgraphviz/lib.rs | 83 ++++++++++++------------------------------ 1 file changed, 24 insertions(+), 59 deletions(-) diff --git a/src/libgraphviz/lib.rs b/src/libgraphviz/lib.rs index 5b1cf2dee9a83..110493bbec159 100644 --- a/src/libgraphviz/lib.rs +++ b/src/libgraphviz/lib.rs @@ -413,27 +413,14 @@ impl<'a> Id<'a> { /// quotes, ...) will return an empty `Err` value. pub fn new>(name: Name) -> Result, ()> { let name = name.into_cow(); - { - let mut chars = name.chars(); - match chars.next() { - Some(c) if is_letter_or_underscore(c) => {} - _ => return Err(()), - } - if !chars.all(is_constituent) { - return Err(()); - } - } - return Ok(Id { name: name }); - - fn is_letter_or_underscore(c: char) -> bool { - in_range('a', c, 'z') || in_range('A', c, 'Z') || c == '_' - } - fn is_constituent(c: char) -> bool { - is_letter_or_underscore(c) || in_range('0', c, '9') + match name.chars().next() { + Some(c) if c.is_ascii_alphabetic() || c == '_' => {} + _ => return Err(()), } - fn in_range(low: char, c: char, high: char) -> bool { - low as usize <= c as usize && c as usize <= high as usize + if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' ) { + return Err(()); } + return Ok(Id { name: name }); } pub fn as_slice(&'a self) -> &'a str { @@ -484,8 +471,7 @@ pub trait Labeller<'a> { /// Maps `e` to a label that will be used in the rendered output. /// The label need not be unique, and may be the empty string; the /// default is in fact the empty string. - fn edge_label(&'a self, e: &Self::Edge) -> LabelText<'a> { - let _ignored = e; + fn edge_label(&'a self, _e: &Self::Edge) -> LabelText<'a> { LabelStr("".into_cow()) } @@ -655,79 +641,58 @@ pub fn render_opts<'a, N, E, G, W>(g: &'a G, G: Labeller<'a, Node=N, Edge=E> + GraphWalk<'a, Node=N, Edge=E>, W: Write { - fn writeln(w: &mut W, arg: &[&str]) -> io::Result<()> { - for &s in arg { - w.write_all(s.as_bytes())?; - } - write!(w, "\n") - } - - fn indent(w: &mut W) -> io::Result<()> { - w.write_all(b" ") - } - - writeln(w, &["digraph ", g.graph_id().as_slice(), " {"])?; + writeln!(w, "digraph {} {{", g.graph_id().as_slice())?; for n in g.nodes().iter() { - indent(w)?; + write!(w, " ")?; let id = g.node_id(n); let escaped = &g.node_label(n).to_dot_string(); - let shape; - let mut text = vec![id.as_slice()]; + let mut text = Vec::new(); + write!(text, "{}", id.as_slice()).unwrap(); if !options.contains(&RenderOption::NoNodeLabels) { - text.push("[label="); - text.push(escaped); - text.push("]"); + write!(text, "[label={}]", escaped).unwrap(); } let style = g.node_style(n); if !options.contains(&RenderOption::NoNodeStyles) && style != Style::None { - text.push("[style=\""); - text.push(style.as_slice()); - text.push("\"]"); + write!(text, "[style=\"{}\"]", style.as_slice()).unwrap(); } if let Some(s) = g.node_shape(n) { - shape = s.to_dot_string(); - text.push("[shape="); - text.push(&shape); - text.push("]"); + write!(text, "[shape={}]", &s.to_dot_string()).unwrap(); } - text.push(";"); - writeln(w, &text)?; + writeln!(text, ";").unwrap(); + w.write_all(&text[..])?; } for e in g.edges().iter() { let escaped_label = &g.edge_label(e).to_dot_string(); - indent(w)?; + write!(w, " ")?; let source = g.source(e); let target = g.target(e); let source_id = g.node_id(&source); let target_id = g.node_id(&target); - let mut text = vec![source_id.as_slice(), " -> ", target_id.as_slice()]; + let mut text = Vec::new(); + write!(text, "{} -> {}", source_id.as_slice(), target_id.as_slice()).unwrap(); if !options.contains(&RenderOption::NoEdgeLabels) { - text.push("[label="); - text.push(escaped_label); - text.push("]"); + write!(text, "[label={}]", escaped_label).unwrap(); } let style = g.edge_style(e); if !options.contains(&RenderOption::NoEdgeStyles) && style != Style::None { - text.push("[style=\""); - text.push(style.as_slice()); - text.push("\"]"); + write!(text, "[style=\"{}\"]", style.as_slice()).unwrap(); } - text.push(";"); - writeln(w, &text)?; + writeln!(text, ";").unwrap(); + w.write_all(&text[..])?; } - writeln(w, &["}"]) + writeln!(w, "}}") } pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {