composable-indexes 0.9.0

In-memory collections with composable indexes
Documentation
use crate::core::{Index, Insert, Key, Remove, Seal, Update};
use alloc::vec::Vec;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Op<T> {
    Insert(Insert_<T>),
    Update(Update_<T>),
    Remove(Remove_<T>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Insert_<T> {
    pub key: Key,
    pub new: T,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Update_<T> {
    pub key: Key,
    pub new: T,
    pub existing: T,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Remove_<T> {
    pub key: Key,
    pub existing: T,
}

pub fn test_index<T: Clone>() -> TestIndex<T> {
    TestIndex::new()
}

pub struct TestIndex<T: Clone> {
    pub ops: Vec<Op<T>>,
}

impl<T: Clone> TestIndex<T> {
    pub fn new() -> Self {
        Self { ops: Vec::new() }
    }
}

impl Default for TestIndex<()> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Clone> Index<T> for TestIndex<T> {
    fn insert(&mut self, _seal: Seal, op: &Insert<T>) {
        self.ops.push(Op::Insert(Insert_ {
            key: op.key,
            new: op.new.clone(),
        }));
    }

    fn update(&mut self, _seal: Seal, op: &Update<T>) {
        self.ops.push(Op::Update(Update_ {
            key: op.key,
            new: op.new.clone(),
            existing: op.existing.clone(),
        }));
    }

    fn remove(&mut self, _seal: Seal, op: &Remove<T>) {
        self.ops.push(Op::Remove(Remove_ {
            key: op.key,
            existing: op.existing.clone(),
        }));
    }
}

impl<T: Clone> TestIndex<T> {
    pub fn operations(&self) -> &[Op<T>] {
        &self.ops
    }

    /// Get the number of operations (returns a Key which can be used with execute)
    pub fn op_count(&self) -> Key {
        Key::unsafe_from_u64(self.ops.len() as u64)
    }
}