1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::graph::Graph;
use crate::graph::GraphErr;

#[cfg(feature = "no_std")]
use core::io::Write;

#[cfg(not(feature = "no_std"))]
use std::io::Write;

#[cfg(feature = "no_std")]
use core::borrow::Cow;

#[cfg(not(feature = "no_std"))]
use std::borrow::Cow;

#[cfg(feature = "no_std")]
use core::fmt::Debug;

#[cfg(not(feature = "no_std"))]
use std::fmt::Debug;

/*
   Bounds on types throw warnings
   type Nd<D: Clone + Debug> = D;
   type Ed<D: Clone + Debug> = (D, D);
   */
type Nd = String;
type Ed = (String, String);

pub(crate) struct Edges<'a> {
    pub(crate) edges: Vec<Ed>,
    pub(crate) graph_name: dot::Id<'a>,
}

impl<'a> Edges<'a> {
    pub fn new(edges: Vec<Ed>, graph_name: &'a str) -> Result<Edges<'a>, GraphErr> {
        let graph_name = dot::Id::new(graph_name).map_err(|_| GraphErr::InvalidGraphName)?;

        Ok(Edges {
            edges,
            graph_name,
        })
    }
}

impl<'a> dot::Labeller<'a, Nd, Ed> for Edges<'a> {
    fn graph_id(&'a self) -> dot::Id { dot::Id::new(self.graph_name.as_slice()).unwrap() }

    fn node_id(&'a self, n: &Nd) -> dot::Id {
        dot::Id::new(n.clone()).unwrap()
    }
}

impl<'a> dot::GraphWalk<'a, Nd, Ed> for Edges<'a> {
    fn nodes(&self) -> dot::Nodes<'a, Nd> {
        let &Edges { edges: ref v, .. } = self;
        let mut nodes = Vec::with_capacity(v.len());

        for (s, t) in v.iter() {
            nodes.push(s.clone()); 
            nodes.push(t.clone());
        }

        Cow::Owned(nodes)
    }

    fn edges(&'a self) -> dot::Edges<'a, Ed> {
        let &Edges { edges: ref edges, .. } = self;
        Cow::Borrowed(&edges[..])
    }

    fn source(&self, e: &Ed) -> Nd { e.0.clone() }
    fn target(&self, e: &Ed) -> Nd { e.1.clone() }
}