Skip to content

libgraphviz: Id::new returns Result<Id, ()> instead of panicking on error #18921

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

Merged
Show file tree
Hide file tree
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
50 changes: 38 additions & 12 deletions src/libgraphviz/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ pub fn render_to<W:Writer>(output: &mut W) {
}

impl<'a> dot::Labeller<'a, Nd, Ed> for Edges {
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1") }
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1").unwrap() }

fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
dot::Id::new(format!("N{}", *n))
dot::Id::new(format!("N{}", *n)).unwrap()
}
}

Expand Down Expand Up @@ -163,9 +163,9 @@ pub fn render_to<W:Writer>(output: &mut W) {
}

impl<'a> dot::Labeller<'a, Nd, Ed<'a>> for Graph {
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2") }
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2").unwrap() }
fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
dot::Id::new(format!("N{}", n))
dot::Id::new(format!("N{}", n)).unwrap()
}
fn node_label<'a>(&'a self, n: &Nd) -> dot::LabelText<'a> {
dot::LabelStr(str::Slice(self.nodes[*n].as_slice()))
Expand Down Expand Up @@ -219,9 +219,9 @@ pub fn render_to<W:Writer>(output: &mut W) {
}

impl<'a> dot::Labeller<'a, Nd<'a>, Ed<'a>> for Graph {
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3") }
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3").unwrap() }
fn node_id(&'a self, n: &Nd<'a>) -> dot::Id<'a> {
dot::Id::new(format!("N{:u}", n.val0()))
dot::Id::new(format!("N{:u}", n.val0())).unwrap()
}
fn node_label<'a>(&'a self, n: &Nd<'a>) -> dot::LabelText<'a> {
let &(i, _) = n;
Expand Down Expand Up @@ -351,14 +351,22 @@ impl<'a> Id<'a> {
/// defined by the DOT language. This function may change in the
/// future to accept a broader subset, or the entirety, of DOT's
/// `ID` format.)
pub fn new<Name:str::IntoMaybeOwned<'a>>(name: Name) -> Id<'a> {
///
/// Passing an invalid string (containing spaces, brackets,
/// quotes, ...) will return an empty `Err` value.
pub fn new<Name:str::IntoMaybeOwned<'a>>(name: Name) -> Result<Id<'a>, ()> {
let name = name.into_maybe_owned();
{
let mut chars = name.as_slice().chars();
assert!(is_letter_or_underscore(chars.next().unwrap()));
assert!(chars.all(is_constituent));
match chars.next() {
Some(c) if is_letter_or_underscore(c) => { ; },
_ => return Err(())
}
if !chars.all(is_constituent) {
return Err(());
}
}
return Id{ name: name };
return Ok(Id{ name: name });

fn is_letter_or_underscore(c: char) -> bool {
in_range('a', c, 'z') || in_range('A', c, 'Z') || c == '_'
Expand Down Expand Up @@ -623,12 +631,12 @@ mod tests {
}

fn id_name<'a>(n: &Node) -> Id<'a> {
Id::new(format!("N{:u}", *n))
Id::new(format!("N{:u}", *n)).unwrap()
}

impl<'a> Labeller<'a, Node, &'a Edge> for LabelledGraph {
fn graph_id(&'a self) -> Id<'a> {
Id::new(self.name.as_slice())
Id::new(self.name.as_slice()).unwrap()
}
fn node_id(&'a self, n: &Node) -> Id<'a> {
id_name(n)
Expand Down Expand Up @@ -821,4 +829,22 @@ r#"digraph syntax_tree {
}
"#);
}

#[test]
fn simple_id_construction() {
let id1 = dot::Id::new("hello");
match id1 {
Ok(_) => {;},
Err(_) => panic!("'hello' is not a valid value for id anymore")
}
}

#[test]
fn badly_formatted_id() {
let id2 = dot::Id::new("Weird { struct : ure } !!!");
match id2 {
Ok(_) => panic!("graphviz id suddenly allows spaces, brackets and stuff"),
Err(_) => {;}
}
}
}
4 changes: 2 additions & 2 deletions src/librustc/middle/cfg/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ fn replace_newline_with_backslash_l(s: String) -> String {
}

impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> {
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(self.name.as_slice()) }
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(self.name.as_slice()).unwrap() }

fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
dot::Id::new(format!("N{:u}", i.node_id()))
dot::Id::new(format!("N{:u}", i.node_id())).unwrap()
}

fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> {
Expand Down