@@ -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;
@@ -354,14 +354,22 @@ impl<'a> Id<'a> {
354
354
/// defined by the DOT language. This function may change in the
355
355
/// future to accept a broader subset, or the entirety, of DOT's
356
356
/// `ID` format.)
357
- pub fn new < Name : str:: IntoMaybeOwned < ' a > > ( name : Name ) -> Id < ' a > {
357
+ ///
358
+ /// Passing an invalid string (containing spaces, brackets,
359
+ /// quotes, ...) will return an empty `Err` value.
360
+ pub fn new < Name : str:: IntoMaybeOwned < ' a > > ( name : Name ) -> Result < Id < ' a > , ( ) > {
358
361
let name = name. into_maybe_owned ( ) ;
359
362
{
360
363
let mut chars = name. as_slice ( ) . chars ( ) ;
361
- assert ! ( is_letter_or_underscore( chars. next( ) . unwrap( ) ) ) ;
362
- assert ! ( chars. all( is_constituent) ) ;
364
+ match chars. next ( ) {
365
+ Some ( c) if is_letter_or_underscore ( c) => { ; } ,
366
+ _ => return Err ( ( ) )
367
+ }
368
+ if !chars. all ( is_constituent) {
369
+ return Err ( ( ) ) ;
370
+ }
363
371
}
364
- return Id { name : name } ;
372
+ return Ok ( Id { name : name } ) ;
365
373
366
374
fn is_letter_or_underscore ( c : char ) -> bool {
367
375
in_range ( 'a' , c, 'z' ) || in_range ( 'A' , c, 'Z' ) || c == '_'
@@ -627,12 +635,12 @@ mod tests {
627
635
}
628
636
629
637
fn id_name < ' a > ( n : & Node ) -> Id < ' a > {
630
- Id :: new ( format ! ( "N{:u}" , * n) )
638
+ Id :: new ( format ! ( "N{:u}" , * n) ) . unwrap ( )
631
639
}
632
640
633
641
impl < ' a > Labeller < ' a , Node , & ' a Edge > for LabelledGraph {
634
642
fn graph_id ( & ' a self ) -> Id < ' a > {
635
- Id :: new ( self . name . as_slice ( ) )
643
+ Id :: new ( self . name . as_slice ( ) ) . unwrap ( )
636
644
}
637
645
fn node_id ( & ' a self , n : & Node ) -> Id < ' a > {
638
646
id_name ( n)
@@ -825,4 +833,22 @@ r#"digraph syntax_tree {
825
833
}
826
834
"# ) ;
827
835
}
836
+
837
+ #[ test]
838
+ fn simple_id_construction ( ) {
839
+ let id1 = dot:: Id :: new ( "hello" ) ;
840
+ match id1 {
841
+ Ok ( _) => { ; } ,
842
+ Err ( _) => panic ! ( "'hello' is not a valid value for id anymore" )
843
+ }
844
+ }
845
+
846
+ #[ test]
847
+ fn badly_formatted_id ( ) {
848
+ let id2 = dot:: Id :: new ( "Weird { struct : ure } !!!" ) ;
849
+ match id2 {
850
+ Ok ( _) => panic ! ( "graphviz id suddenly allows spaces, brackets and stuff" ) ,
851
+ Err ( _) => { ; }
852
+ }
853
+ }
828
854
}
0 commit comments