use crate::cdk::structures::btreemap::BTreeMap as StableBtreeMap;
use crate::{
cdk::structures::{DefaultMemoryImpl, memory::VirtualMemory},
role_contract::allocation::memory::topology::APP_REGISTRY_ID,
storage::prelude::*,
};
use std::cell::RefCell;
eager_static! {
static APP_REGISTRY: RefCell<StableBtreeMap<Principal, Principal, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(StableBtreeMap::init(crate::ic_memory_key!(authority = CANIC_CORE_MEMORY_AUTHORITY, key = "canic.core.app_registry.v1", ty = AppRegistry, id = APP_REGISTRY_ID)));
}
#[derive(Clone, Debug)]
pub struct AppRegistryEntryRecord {
pub subnet_pid: Principal,
pub root_pid: Principal,
}
impl AppRegistryEntryRecord {
pub const STATE_CONTRACT_NAME: &'static str = "AppRegistryEntryRecord";
}
#[derive(Clone, Debug)]
pub struct AppRegistryData {
pub entries: Vec<AppRegistryEntryRecord>,
}
impl AppRegistryData {
pub const STATE_CONTRACT_NAME: &'static str = "AppRegistryData";
}
pub struct AppRegistry;
impl AppRegistry {
pub(crate) fn upsert(subnet_pid: Principal, root_pid: Principal) {
APP_REGISTRY.with_borrow_mut(|map| {
map.insert(subnet_pid, root_pid);
});
}
#[must_use]
pub(crate) fn export() -> AppRegistryData {
AppRegistryData {
entries: APP_REGISTRY.with_borrow(|map| {
map.iter()
.map(|entry| AppRegistryEntryRecord {
subnet_pid: *entry.key(),
root_pid: entry.value(),
})
.collect()
}),
}
}
}