use crate::{
cdk::structures::{BTreeMap, DefaultMemoryImpl, memory::VirtualMemory},
storage::{prelude::*, stable::memory::topology::APP_INDEX_ID},
};
use std::cell::RefCell;
eager_static! {
static APP_INDEX: RefCell<BTreeMap<CanisterRole, Principal, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(BTreeMap::init(ic_memory!(AppIndex, APP_INDEX_ID)));
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AppIndexRecord {
pub entries: Vec<(CanisterRole, Principal)>,
}
pub struct AppIndex;
impl AppIndex {
#[must_use]
pub(crate) fn export() -> AppIndexRecord {
AppIndexRecord {
entries: APP_INDEX.with_borrow(|map| {
map.iter()
.map(|entry| (entry.key().clone(), entry.value()))
.collect()
}),
}
}
pub(crate) fn import(data: AppIndexRecord) {
APP_INDEX.with_borrow_mut(|map| {
map.clear();
for (role, pid) in data.entries {
map.insert(role, pid);
}
});
}
}