Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
dag: Add "dot" format output
cloudhead committed 2 years ago
commit bb3c1b56845cdde95bfbd61e18e8f514efed1f3d
parent 8657cd8fc8b13bbf2b2a3b73ab653504cf00f800
1 file changed +18 -0
modified radicle-dag/src/lib.rs
@@ -5,6 +5,7 @@ use std::{
    cmp::Ordering,
    collections::{BTreeMap, BTreeSet, VecDeque},
    fmt,
+
    fmt::Write,
    ops::{ControlFlow, Deref, Index},
};

@@ -313,6 +314,23 @@ impl<K: Ord + Copy, V> Dag<K, V> {
    }
}

+
impl<K: Ord + Copy + fmt::Display, V> Dag<K, V> {
+
    /// Return the graph in "dot" format.
+
    pub fn to_dot(&self) -> String {
+
        let mut output = String::new();
+

+
        writeln!(output, "digraph G {{").ok();
+
        for (k, v) in self.graph.iter() {
+
            for d in &v.dependencies {
+
                writeln!(output, "\t\"{k}\" -> \"{d}\";").ok();
+
            }
+
        }
+
        writeln!(output, "}}").ok();
+

+
        output
+
    }
+
}
+

impl<K: Ord + Copy + fmt::Debug, V> Index<&K> for Dag<K, V> {
    type Output = Node<K, V>;