composable_indexes/testutils/
test_index.rs

1use crate::core::{Index, Insert, Key, Remove, Seal, Update};
2use alloc::vec::Vec;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum Op<T> {
6    Insert(Insert_<T>),
7    Update(Update_<T>),
8    Remove(Remove_<T>),
9}
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Insert_<T> {
13    pub key: Key,
14    pub new: T,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct Update_<T> {
19    pub key: Key,
20    pub new: T,
21    pub existing: T,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct Remove_<T> {
26    pub key: Key,
27    pub existing: T,
28}
29
30pub fn test_index<T: Clone>() -> TestIndex<T> {
31    TestIndex::new()
32}
33
34pub struct TestIndex<T: Clone> {
35    pub ops: Vec<Op<T>>,
36}
37
38impl<T: Clone> TestIndex<T> {
39    pub fn new() -> Self {
40        Self { ops: Vec::new() }
41    }
42}
43
44impl Default for TestIndex<()> {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl<T: Clone> Index<T> for TestIndex<T> {
51    fn insert(&mut self, _seal: Seal, op: &Insert<T>) {
52        self.ops.push(Op::Insert(Insert_ {
53            key: op.key,
54            new: op.new.clone(),
55        }));
56    }
57
58    fn update(&mut self, _seal: Seal, op: &Update<T>) {
59        self.ops.push(Op::Update(Update_ {
60            key: op.key,
61            new: op.new.clone(),
62            existing: op.existing.clone(),
63        }));
64    }
65
66    fn remove(&mut self, _seal: Seal, op: &Remove<T>) {
67        self.ops.push(Op::Remove(Remove_ {
68            key: op.key,
69            existing: op.existing.clone(),
70        }));
71    }
72}
73
74impl<T: Clone> TestIndex<T> {
75    pub fn operations(&self) -> &[Op<T>] {
76        &self.ops
77    }
78
79    /// Get the number of operations (returns a Key which can be used with execute)
80    pub fn op_count(&self) -> Key {
81        Key::unsafe_from_u64(self.ops.len() as u64)
82    }
83}