use async_trait::async_trait;
use crate::Node;
use crate::NodeId;
#[async_trait]
pub trait DagBackend<Id, N>
where
N: Node,
Id: NodeId + Send,
{
type Error;
async fn get(&self, id: Id) -> Result<Option<(Id, N)>, Self::Error>;
async fn put(&mut self, node: N) -> Result<Id, Self::Error>;
}
#[cfg(test)]
mod tests {
use crate::test_impl as test;
use crate::*;
#[test]
fn test_backend_get() {
let b = test::Backend::new(vec![Some(test::Node {
parents: vec![],
data: 0,
})]);
let node = tokio_test::block_on(b.get(test::Id(0)));
assert!(node.is_ok());
let node = node.unwrap();
assert!(node.is_some());
let node = node.unwrap();
assert_eq!(node.0, test::Id(0));
assert!(node.1.parents.is_empty());
}
#[test]
fn test_backend_put() {
let mut b = test::Backend::new(vec![Some(test::Node {
parents: vec![],
data: 0,
})]);
let _ = tokio_test::block_on(b.put({
test::Node {
parents: vec![],
data: 1,
}
}));
{
let node = tokio_test::block_on(b.get(test::Id(0)));
assert!(node.is_ok());
let node = node.unwrap();
assert!(node.is_some());
let node = node.unwrap();
assert_eq!(node.0, test::Id(0));
assert!(node.1.parents.is_empty());
}
{
let node = tokio_test::block_on(b.get(test::Id(1)));
assert!(node.is_ok());
let node = node.unwrap();
assert!(node.is_some());
let node = node.unwrap();
assert_eq!(node.0, test::Id(1));
assert!(node.1.parents.is_empty());
}
{
let node = tokio_test::block_on(b.get(test::Id(2)));
assert!(node.is_ok());
let node = node.unwrap();
assert!(node.is_none());
}
}
}