1use std::collections::HashMap;
9use std::collections::HashSet;
10
11use crate::namedag::MemNameDag;
12use crate::ops::Parents;
13use crate::Result;
14use crate::Vertex;
15
16pub struct DrawDag {
18 parents: HashMap<Vertex, Vec<Vertex>>,
19}
20
21impl<'a> From<&'a str> for DrawDag {
22 fn from(text: &'a str) -> Self {
23 let parents = ::drawdag::parse(text);
24 let v = |s: String| Vertex::copy_from(s.as_bytes());
25 let parents = parents
26 .into_iter()
27 .map(|(k, vs)| (v(k), vs.into_iter().map(v).collect()))
28 .collect();
29 Self { parents }
30 }
31}
32
33impl DrawDag {
34 pub fn heads(&self) -> Vec<Vertex> {
36 let mut heads = self
37 .parents
38 .keys()
39 .collect::<HashSet<_>>()
40 .difference(&self.parents.values().flatten().collect())
41 .map(|&v| v.clone())
42 .collect::<Vec<_>>();
43 heads.sort();
44 heads
45 }
46}
47
48#[async_trait::async_trait]
49impl Parents for DrawDag {
50 async fn parent_names(&self, name: Vertex) -> Result<Vec<Vertex>> {
51 Parents::parent_names(&self.parents, name).await
52 }
53
54 async fn hint_subdag_for_insertion(&self, heads: &[Vertex]) -> Result<MemNameDag> {
55 Parents::hint_subdag_for_insertion(&self.parents, heads).await
56 }
57}