use crate::{
cdk::structures::{DefaultMemoryImpl, memory::VirtualMemory},
role_contract::allocation::memory::topology::APP_INDEX_ID,
storage::prelude::*,
};
use ic_memory::stable_structures::btreemap::BTreeMap as StableBtreeMap;
use std::cell::RefCell;
eager_static! {
static APP_INDEX: RefCell<StableBtreeMap<CanisterRole, Principal, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(StableBtreeMap::init(crate::ic_memory_key!("canic.core.app_index.v1", AppIndex, APP_INDEX_ID)));
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AppIndexData {
pub entries: Vec<super::IndexEntryRecord>,
}
impl AppIndexData {
pub const STATE_CONTRACT_NAME: &'static str = "AppIndexData";
}
pub struct AppIndex;
impl AppIndex {
#[must_use]
pub(crate) fn export() -> AppIndexData {
AppIndexData {
entries: APP_INDEX.with_borrow(|map| {
map.iter()
.map(|entry| super::IndexEntryRecord {
role: entry.key().clone(),
pid: entry.value(),
})
.collect()
}),
}
}
pub(crate) fn import(data: AppIndexData) {
APP_INDEX.with_borrow_mut(|map| {
map.clear_new();
for entry in data.entries {
map.insert(entry.role, entry.pid);
}
});
}
}