dag/tests/
drawdag.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8use std::collections::HashMap;
9use std::collections::HashSet;
10
11use crate::namedag::MemNameDag;
12use crate::ops::Parents;
13use crate::Result;
14use crate::Vertex;
15
16/// Represents a graph from ASCII parsed by `drawdag`.
17pub 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    /// Heads in the graph. Vertexes that are not parents of other vertexes.
35    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}