Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup for libgraphviz #46784

Merged
merged 1 commit into from
Dec 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 24 additions & 59 deletions src/libgraphviz/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,27 +413,14 @@ impl<'a> Id<'a> {
/// quotes, ...) will return an empty `Err` value.
pub fn new<Name: IntoCow<'a, str>>(name: Name) -> Result<Id<'a>, ()> {
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 {
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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: Write>(w: &mut W, arg: &[&str]) -> io::Result<()> {
for &s in arg {
w.write_all(s.as_bytes())?;
}
write!(w, "\n")
}

fn indent<W: Write>(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, " ")?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(personally I usually prefer a function like indent over having to double-check that the string contents all match up, but since there are only two calls to indent I'm not going to make a fuss)

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 {
Expand Down