Skip to content

Commit 2e2defd

Browse files
committed
Cleanup for libgraphviz
1 parent af57ace commit 2e2defd

File tree

1 file changed

+24
-59
lines changed

1 file changed

+24
-59
lines changed

src/libgraphviz/lib.rs

+24-59
Original file line numberDiff line numberDiff line change
@@ -413,27 +413,14 @@ impl<'a> Id<'a> {
413413
/// quotes, ...) will return an empty `Err` value.
414414
pub fn new<Name: IntoCow<'a, str>>(name: Name) -> Result<Id<'a>, ()> {
415415
let name = name.into_cow();
416-
{
417-
let mut chars = name.chars();
418-
match chars.next() {
419-
Some(c) if is_letter_or_underscore(c) => {}
420-
_ => return Err(()),
421-
}
422-
if !chars.all(is_constituent) {
423-
return Err(());
424-
}
425-
}
426-
return Ok(Id { name: name });
427-
428-
fn is_letter_or_underscore(c: char) -> bool {
429-
in_range('a', c, 'z') || in_range('A', c, 'Z') || c == '_'
430-
}
431-
fn is_constituent(c: char) -> bool {
432-
is_letter_or_underscore(c) || in_range('0', c, '9')
416+
match name.chars().next() {
417+
Some(c) if c.is_ascii_alphabetic() || c == '_' => {}
418+
_ => return Err(()),
433419
}
434-
fn in_range(low: char, c: char, high: char) -> bool {
435-
low as usize <= c as usize && c as usize <= high as usize
420+
if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' ) {
421+
return Err(());
436422
}
423+
return Ok(Id { name: name });
437424
}
438425

439426
pub fn as_slice(&'a self) -> &'a str {
@@ -484,8 +471,7 @@ pub trait Labeller<'a> {
484471
/// Maps `e` to a label that will be used in the rendered output.
485472
/// The label need not be unique, and may be the empty string; the
486473
/// default is in fact the empty string.
487-
fn edge_label(&'a self, e: &Self::Edge) -> LabelText<'a> {
488-
let _ignored = e;
474+
fn edge_label(&'a self, _e: &Self::Edge) -> LabelText<'a> {
489475
LabelStr("".into_cow())
490476
}
491477

@@ -655,79 +641,58 @@ pub fn render_opts<'a, N, E, G, W>(g: &'a G,
655641
G: Labeller<'a, Node=N, Edge=E> + GraphWalk<'a, Node=N, Edge=E>,
656642
W: Write
657643
{
658-
fn writeln<W: Write>(w: &mut W, arg: &[&str]) -> io::Result<()> {
659-
for &s in arg {
660-
w.write_all(s.as_bytes())?;
661-
}
662-
write!(w, "\n")
663-
}
664-
665-
fn indent<W: Write>(w: &mut W) -> io::Result<()> {
666-
w.write_all(b" ")
667-
}
668-
669-
writeln(w, &["digraph ", g.graph_id().as_slice(), " {"])?;
644+
writeln!(w, "digraph {} {{", g.graph_id().as_slice())?;
670645
for n in g.nodes().iter() {
671-
indent(w)?;
646+
write!(w, " ")?;
672647
let id = g.node_id(n);
673648

674649
let escaped = &g.node_label(n).to_dot_string();
675-
let shape;
676650

677-
let mut text = vec![id.as_slice()];
651+
let mut text = Vec::new();
652+
write!(text, "{}", id.as_slice()).unwrap();
678653

679654
if !options.contains(&RenderOption::NoNodeLabels) {
680-
text.push("[label=");
681-
text.push(escaped);
682-
text.push("]");
655+
write!(text, "[label={}]", escaped).unwrap();
683656
}
684657

685658
let style = g.node_style(n);
686659
if !options.contains(&RenderOption::NoNodeStyles) && style != Style::None {
687-
text.push("[style=\"");
688-
text.push(style.as_slice());
689-
text.push("\"]");
660+
write!(text, "[style=\"{}\"]", style.as_slice()).unwrap();
690661
}
691662

692663
if let Some(s) = g.node_shape(n) {
693-
shape = s.to_dot_string();
694-
text.push("[shape=");
695-
text.push(&shape);
696-
text.push("]");
664+
write!(text, "[shape={}]", &s.to_dot_string()).unwrap();
697665
}
698666

699-
text.push(";");
700-
writeln(w, &text)?;
667+
writeln!(text, ";").unwrap();
668+
w.write_all(&text[..])?;
701669
}
702670

703671
for e in g.edges().iter() {
704672
let escaped_label = &g.edge_label(e).to_dot_string();
705-
indent(w)?;
673+
write!(w, " ")?;
706674
let source = g.source(e);
707675
let target = g.target(e);
708676
let source_id = g.node_id(&source);
709677
let target_id = g.node_id(&target);
710678

711-
let mut text = vec![source_id.as_slice(), " -> ", target_id.as_slice()];
679+
let mut text = Vec::new();
680+
write!(text, "{} -> {}", source_id.as_slice(), target_id.as_slice()).unwrap();
712681

713682
if !options.contains(&RenderOption::NoEdgeLabels) {
714-
text.push("[label=");
715-
text.push(escaped_label);
716-
text.push("]");
683+
write!(text, "[label={}]", escaped_label).unwrap();
717684
}
718685

719686
let style = g.edge_style(e);
720687
if !options.contains(&RenderOption::NoEdgeStyles) && style != Style::None {
721-
text.push("[style=\"");
722-
text.push(style.as_slice());
723-
text.push("\"]");
688+
write!(text, "[style=\"{}\"]", style.as_slice()).unwrap();
724689
}
725690

726-
text.push(";");
727-
writeln(w, &text)?;
691+
writeln!(text, ";").unwrap();
692+
w.write_all(&text[..])?;
728693
}
729694

730-
writeln(w, &["}"])
695+
writeln!(w, "}}")
731696
}
732697

733698
pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {

0 commit comments

Comments
 (0)