summaryrefslogtreecommitdiffstats
path: root/src/test_impl.rs
blob: ae7ce0ca005a5694137bfff0d3bc911ad63047cd (plain)
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
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

use anyhow::Result;
use async_trait::async_trait;

#[derive(Copy, Clone, Eq, PartialEq, std::hash::Hash, Debug)]
pub struct Id(pub(crate) usize);

impl crate::NodeId for Id {}

#[derive(Clone, Debug)]
pub struct Node {
    pub(crate) id: Id,
    pub(crate) parents: Vec<Id>,
    pub(crate) data: u8,
}

impl crate::Node for Node {
    type Id = Id;

    fn id(&self) -> &Self::Id {
        &self.id
    }

    fn parent_ids(&self) -> Vec<Self::Id> {
        self.parents.clone()
    }
}

/// The backend for the tests
///
/// This is `Clone` because we can test branching only with a clonable backend.
/// A real backend would not implement the storage itself, but rather a way to retrieve the data
/// from some storage mechansim (think IPFS), and thus `Clone`ing a backend is nothing esotheric.
#[derive(Clone, Debug)]
pub struct Backend(pub(crate) Vec<Option<Node>>);

#[async_trait]
impl crate::DagBackend<Id, Node> for Backend {
    async fn get(&self, id: Id) -> Result<Option<Node>> {
        if self.0.len() < id.0 + 1 {
            Ok(None)
        } else {
            Ok(self.0[id.0].clone())
        }
    }

    async fn put(&mut self, node: Node) -> Result<Id> {
        while self.0.len() < node.id.0 + 1 {
            self.0.push(None)
        }

        let idx = node.id.0;
        self.0[idx] = Some(node);
        Ok(Id(idx))
    }
}