@@ -60,10 +60,10 @@ pub fn render_to<W:Writer>(output: &mut W) {
60
60
}
61
61
62
62
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() }
64
64
65
65
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()
67
67
}
68
68
}
69
69
@@ -163,9 +163,9 @@ pub fn render_to<W:Writer>(output: &mut W) {
163
163
}
164
164
165
165
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() }
167
167
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()
169
169
}
170
170
fn node_label<'a>(&'a self, n: &Nd) -> dot::LabelText<'a> {
171
171
dot::LabelStr(str::Slice(self.nodes[*n].as_slice()))
@@ -219,9 +219,9 @@ pub fn render_to<W:Writer>(output: &mut W) {
219
219
}
220
220
221
221
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() }
223
223
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()
225
225
}
226
226
fn node_label<'a>(&'a self, n: &Nd<'a>) -> dot::LabelText<'a> {
227
227
let &(i, _) = n;
@@ -351,14 +351,22 @@ impl<'a> Id<'a> {
351
351
/// defined by the DOT language. This function may change in the
352
352
/// future to accept a broader subset, or the entirety, of DOT's
353
353
/// `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 > , ( ) > {
355
358
let name = name. into_maybe_owned ( ) ;
356
359
{
357
360
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
+ }
360
368
}
361
- return Id { name : name } ;
369
+ return Ok ( Id { name : name } ) ;
362
370
363
371
fn is_letter_or_underscore ( c : char ) -> bool {
364
372
in_range ( 'a' , c, 'z' ) || in_range ( 'A' , c, 'Z' ) || c == '_'
@@ -623,12 +631,12 @@ mod tests {
623
631
}
624
632
625
633
fn id_name < ' a > ( n : & Node ) -> Id < ' a > {
626
- Id :: new ( format ! ( "N{:u}" , * n) )
634
+ Id :: new ( format ! ( "N{:u}" , * n) ) . unwrap ( )
627
635
}
628
636
629
637
impl < ' a > Labeller < ' a , Node , & ' a Edge > for LabelledGraph {
630
638
fn graph_id ( & ' a self ) -> Id < ' a > {
631
- Id :: new ( self . name . as_slice ( ) )
639
+ Id :: new ( self . name . as_slice ( ) ) . unwrap ( )
632
640
}
633
641
fn node_id ( & ' a self , n : & Node ) -> Id < ' a > {
634
642
id_name ( n)
@@ -821,4 +829,22 @@ r#"digraph syntax_tree {
821
829
}
822
830
"# ) ;
823
831
}
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
+ }
824
850
}
0 commit comments