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