p2panda_auth/test_utils/
group_store.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use std::cell::RefCell;
4use std::collections::HashMap;
5use std::fmt::Debug;
6use std::rc::Rc;
7
8use thiserror::Error;
9
10use crate::test_utils::{
11    Conditions, MemberId, MessageId, TestGroupState, TestOrderer, TestResolver,
12};
13use crate::traits::GroupStore;
14
15#[derive(Debug, Error)]
16pub enum GroupStoreError {}
17
18#[derive(Clone, Debug)]
19pub struct TestGroupStore(Rc<RefCell<HashMap<MemberId, TestGroupState>>>);
20
21impl Default for TestGroupStore {
22    fn default() -> Self {
23        Self(Rc::new(RefCell::new(HashMap::new())))
24    }
25}
26
27impl GroupStore<MemberId, MessageId, Conditions, TestResolver, TestOrderer> for TestGroupStore {
28    type Error = GroupStoreError;
29
30    fn get(&self, id: &char) -> Result<Option<TestGroupState>, Self::Error> {
31        let store = self.0.borrow();
32        let group_y = store.get(id);
33        Ok(group_y.cloned())
34    }
35
36    fn insert(&self, id: &char, group: &TestGroupState) -> Result<(), Self::Error> {
37        {
38            let mut store = self.0.borrow_mut();
39            store.insert(*id, group.clone());
40        }
41        Ok(())
42    }
43}