Skip to content

Commit eb6419c

Browse files
authored
Merge pull request #51 from matt-snider/feature/improve-labels
Improve labeling to support edges and use dot's labels instead of IDs
2 parents d05d58c + 35442f2 commit eb6419c

4 files changed

Lines changed: 207 additions & 121 deletions

File tree

examples/dot.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,27 @@ pub fn main() {
1616

1717
#[cfg(feature = "dot")]
1818
{
19-
graph.label_vertex(&v1, "test1").unwrap();
20-
graph.label_vertex(&v2, "test2").unwrap();
21-
graph.label_vertex(&v3, "test3").unwrap();
22-
graph.label_vertex(&v4, "test4").unwrap();
23-
graph.label_vertex(&v5, "test5").unwrap();
24-
graph.label_vertex(&v6, "test6").unwrap();
19+
graph.add_vertex_label(&v1, "label: test1").unwrap();
20+
graph.add_vertex_label(&v2, "label: test2").unwrap();
21+
graph.add_vertex_label(&v3, "label: test3").unwrap();
22+
graph.add_vertex_label(&v4, "label: test4").unwrap();
23+
graph.add_vertex_label(&v5, "label: test5").unwrap();
24+
graph.add_vertex_label(&v6, "label: test6").unwrap();
2525
}
2626

2727
graph.add_edge(&v1, &v2).unwrap();
2828
graph.add_edge(&v3, &v1).unwrap();
2929
graph.add_edge(&v1, &v4).unwrap();
3030
graph.add_edge(&v5, &v6).unwrap();
3131

32+
#[cfg(feature = "dot")]
33+
{
34+
graph.add_edge_label(&v1, &v2, "V1→V2").unwrap();
35+
graph.add_edge_label(&v3, &v1, "V3→V1").unwrap();
36+
graph.add_edge_label(&v1, &v4, "V1→V4").unwrap();
37+
graph.add_edge_label(&v5, &v6, "V5→V6").unwrap();
38+
}
39+
3240
#[cfg(feature = "dot")]
3341
graph.to_dot("example1", &mut f).unwrap();
3442
}

src/dot.rs

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::graph::Graph;
2-
use crate::graph::GraphErr;
1+
use crate::{Graph, GraphErr, VertexId};
32

43
#[cfg(feature = "no_std")]
54
use core::io::Write;
@@ -19,61 +18,65 @@ use core::fmt::Debug;
1918
#[cfg(not(feature = "no_std"))]
2019
use 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

Comments
 (0)