Skip to content

Commit 70bf4f7

Browse files
oli-obkOliver Schneider
oli-obk
authored and
Oliver Schneider
committed
libgraphviz: Id::new returns Result<Id, ()> instead of panicking on error
creating a new Id object requires the format to match a subset of `ID` format defined by the DOT language. When the format did not match, the function called assert. This was not mentioned in the docs or the spec. I made the failure explicit by returning an Result<Id, ()>.
1 parent 0047dbe commit 70bf4f7

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/libgraphviz/lib.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ pub fn render_to<W:Writer>(output: &mut W) {
6060
}
6161
6262
impl<'a> dot::Labeller<'a, Nd, Ed> for Edges {
63-
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1") }
63+
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example1").unwrap() }
6464
6565
fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
66-
dot::Id::new(format!("N{}", *n))
66+
dot::Id::new(format!("N{}", *n)).unwrap()
6767
}
6868
}
6969
@@ -163,9 +163,9 @@ pub fn render_to<W:Writer>(output: &mut W) {
163163
}
164164
165165
impl<'a> dot::Labeller<'a, Nd, Ed<'a>> for Graph {
166-
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2") }
166+
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example2").unwrap() }
167167
fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
168-
dot::Id::new(format!("N{}", n))
168+
dot::Id::new(format!("N{}", n)).unwrap()
169169
}
170170
fn node_label<'a>(&'a self, n: &Nd) -> dot::LabelText<'a> {
171171
dot::LabelStr(str::Slice(self.nodes[*n].as_slice()))
@@ -219,9 +219,9 @@ pub fn render_to<W:Writer>(output: &mut W) {
219219
}
220220
221221
impl<'a> dot::Labeller<'a, Nd<'a>, Ed<'a>> for Graph {
222-
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3") }
222+
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new("example3").unwrap() }
223223
fn node_id(&'a self, n: &Nd<'a>) -> dot::Id<'a> {
224-
dot::Id::new(format!("N{:u}", n.val0()))
224+
dot::Id::new(format!("N{:u}", n.val0())).unwrap()
225225
}
226226
fn node_label<'a>(&'a self, n: &Nd<'a>) -> dot::LabelText<'a> {
227227
let &(i, _) = n;
@@ -351,14 +351,22 @@ impl<'a> Id<'a> {
351351
/// defined by the DOT language. This function may change in the
352352
/// future to accept a broader subset, or the entirety, of DOT's
353353
/// `ID` format.)
354-
pub fn new<Name:str::IntoMaybeOwned<'a>>(name: Name) -> Id<'a> {
354+
///
355+
/// Passing an invalid string (containing spaces, brackets,
356+
/// quotes, ...) will return an empty `Err` value.
357+
pub fn new<Name:str::IntoMaybeOwned<'a>>(name: Name) -> Result<Id<'a>, ()> {
355358
let name = name.into_maybe_owned();
356359
{
357360
let mut chars = name.as_slice().chars();
358-
assert!(is_letter_or_underscore(chars.next().unwrap()));
359-
assert!(chars.all(is_constituent));
361+
match chars.next() {
362+
Some(c) if is_letter_or_underscore(c) => { ; },
363+
_ => return Err(())
364+
}
365+
if !chars.all(is_constituent) {
366+
return Err(());
367+
}
360368
}
361-
return Id{ name: name };
369+
return Ok(Id{ name: name });
362370

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

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

629637
impl<'a> Labeller<'a, Node, &'a Edge> for LabelledGraph {
630638
fn graph_id(&'a self) -> Id<'a> {
631-
Id::new(self.name.as_slice())
639+
Id::new(self.name.as_slice()).unwrap()
632640
}
633641
fn node_id(&'a self, n: &Node) -> Id<'a> {
634642
id_name(n)
@@ -821,4 +829,22 @@ r#"digraph syntax_tree {
821829
}
822830
"#);
823831
}
832+
833+
#[test]
834+
fn simple_id_construction() {
835+
let id1 = dot::Id::new("hello");
836+
match id1 {
837+
Ok(_) => {;},
838+
Err(_) => panic!("'hello' is not a valid value for id anymore")
839+
}
840+
}
841+
842+
#[test]
843+
fn badly_formatted_id() {
844+
let id2 = dot::Id::new("Weird { struct : ure } !!!");
845+
match id2 {
846+
Ok(_) => panic!("graphviz id suddenly allows spaces, brackets and stuff"),
847+
Err(_) => {;}
848+
}
849+
}
824850
}

src/librustc/middle/cfg/graphviz.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ fn replace_newline_with_backslash_l(s: String) -> String {
5050
}
5151

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

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

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

0 commit comments

Comments
 (0)