use crate::{Binding, Network};
pub fn graph(network: &Network) -> String {
let mut out: String = "flowchart TD\n".into();
for delegation in network.delegations.values().flatten() {
let regexes = if !delegation.regexes.is_empty() {
let mut regexes = delegation
.regexes
.iter()
.map(|r| &r.0)
.map(|s| {
s.strip_prefix("<[^>]+[@.]")
.unwrap()
.strip_suffix(">$")
.unwrap()
})
.map(|s| s.replace("\\", ""))
.collect::<Vec<_>>()
.join(",");
regexes.insert(0, '/');
regexes
} else {
"".to_string()
};
out.push_str(&format!(
" {} -->|{}/{}{}| {}\n",
&delegation.issuer.0,
delegation.trust_amount,
u8::from(delegation.trust_depth),
regexes,
&delegation.target.0,
))
}
out.push('\n');
for (i, (Binding { cert, identity }, certifications)) in
network.certifications.iter().enumerate()
{
let shorthand = format!("b{}", i);
for certification in certifications {
out.push_str(&format!(
" {} -..-> {}\n",
&certification.issuer.0, shorthand
))
}
out.push_str(&format!(
" {}([{}, {}])\n",
shorthand,
&cert.0,
identity
.0
.replace("<", "")
.replace(">", "")
.replace("@", "\\@")
.replace("(", "_")
.replace(")", "_")
.replace("\"", "'")
));
out.push('\n');
}
out.push_str("classDef bind stroke-dasharray: 5 5\n");
let binds: String = (0..network.certifications.len())
.map(|i| format!("b{}", i))
.collect::<Vec<_>>()
.join(",");
out.push_str(&format!("class {} bind;", binds));
out
}