1- use crate :: graph:: Graph ;
2- use crate :: graph:: GraphErr ;
1+ use crate :: { Graph , GraphErr , VertexId } ;
32
43#[ cfg( feature = "no_std" ) ]
54use core:: io:: Write ;
@@ -19,61 +18,65 @@ use core::fmt::Debug;
1918#[ cfg( not( feature = "no_std" ) ) ]
2019use std:: fmt:: Debug ;
2120
22- /*
23- Bounds on types throw warnings
24- type Nd<D: Clone + Debug> = D;
25- type Ed<D: Clone + Debug> = (D, D);
26- */
27- type Nd = String ;
28- type Ed = ( String , String ) ;
29-
30- pub ( crate ) struct Edges < ' a > {
31- pub ( crate ) edges : Vec < Ed > ,
32- pub ( crate ) graph_name : dot:: Id < ' a > ,
21+ type Nd = VertexId ;
22+ type Ed < ' a > = ( & ' a VertexId , & ' a VertexId ) ;
23+
24+
25+ pub ( crate ) struct DotGraph < ' a , T > {
26+ name : dot:: Id < ' a > ,
27+ graph : & ' a Graph < T > ,
3328}
3429
35- impl < ' a > Edges < ' a > {
36- pub fn new ( edges : Vec < Ed > , graph_name : & ' a str ) -> Result < Edges < ' a > , GraphErr > {
37- let graph_name = dot:: Id :: new ( graph_name) . map_err ( |_| GraphErr :: InvalidGraphName ) ?;
3830
39- Ok ( Edges { edges, graph_name } )
31+ impl < ' a , T > DotGraph < ' a , T > {
32+ pub fn new ( graph : & ' a Graph < T > , name : & ' a str ) -> Result < DotGraph < ' a , T > , GraphErr > {
33+ let name = dot:: Id :: new ( name)
34+ . map_err ( |_| GraphErr :: InvalidGraphName ) ?;
35+ Ok ( DotGraph { name, graph } )
4036 }
4137}
4238
43- impl < ' a > dot:: Labeller < ' a , Nd , Ed > for Edges < ' a > {
44- fn graph_id ( & ' a self ) -> dot:: Id {
45- dot:: Id :: new ( self . graph_name . as_slice ( ) ) . unwrap ( )
39+
40+ impl < ' a , T > dot:: Labeller < ' a , Nd , Ed < ' a > > for DotGraph < ' a , T > {
41+ fn graph_id ( & ' a self ) -> dot:: Id < ' a > {
42+ dot:: Id :: new ( self . name . as_slice ( ) ) . unwrap ( )
4643 }
4744
48- fn node_id ( & ' a self , n : & Nd ) -> dot:: Id {
49- dot:: Id :: new ( n. clone ( ) ) . unwrap ( )
45+ fn node_id ( & ' a self , n : & Nd ) -> dot:: Id < ' a > {
46+ let hex = format ! ( "N{}" , hex:: encode( n. bytes( ) ) ) ;
47+ dot:: Id :: new ( hex) . unwrap ( )
5048 }
51- }
5249
53- impl < ' a > dot:: GraphWalk < ' a , Nd , Ed > for Edges < ' a > {
54- fn nodes ( & self ) -> dot:: Nodes < ' a , Nd > {
55- let & Edges { edges : ref v, .. } = self ;
56- let mut nodes = Vec :: with_capacity ( v. len ( ) ) ;
50+ fn node_label < ' b > ( & ' b self , n : & Nd ) -> dot:: LabelText < ' b > {
51+ let label = self . graph . vertex_label ( n) . unwrap ( ) ;
52+ dot:: LabelText :: label ( Cow :: Borrowed ( label) )
53+ }
54+
55+ fn edge_label < ' b > ( & ' b self , e : & Ed ) -> dot:: LabelText < ' b > {
56+ let label = self . graph . edge_label ( e. 0 , e. 1 ) . unwrap ( ) ;
57+ dot:: LabelText :: LabelStr ( Cow :: Borrowed ( label) )
58+ }
59+ }
5760
58- for ( s, t) in v. iter ( ) {
59- nodes. push ( s. clone ( ) ) ;
60- nodes. push ( t. clone ( ) ) ;
61- }
6261
62+ impl < ' a , T > dot:: GraphWalk < ' a , Nd , Ed < ' a > > for DotGraph < ' a , T > {
63+ fn nodes ( & self ) -> dot:: Nodes < ' a , Nd > {
64+ let nodes = self . graph . vertices ( ) . cloned ( ) . collect ( ) ;
6365 Cow :: Owned ( nodes)
6466 }
6567
66- fn edges ( & ' a self ) -> dot:: Edges < ' a , Ed > {
67- let & Edges {
68- edges : ref edges, ..
69- } = self ;
70- Cow :: Borrowed ( & edges[ ..] )
68+ fn edges ( & ' a self ) -> dot:: Edges < ' a , Ed < ' a > > {
69+ self . graph . edges ( )
70+ . map ( |e| ( e. 1 , e. 0 ) )
71+ . collect ( )
7172 }
7273
7374 fn source ( & self , e : & Ed ) -> Nd {
74- e. 0 . clone ( )
75+ * e. 0
7576 }
77+
7678 fn target ( & self , e : & Ed ) -> Nd {
77- e. 1 . clone ( )
79+ * e. 1
7880 }
7981}
82+
0 commit comments