mod mapper;
use crate::{
dto::canister::CanisterInfo,
ops::{
ic::IcOps, prelude::*, runtime::env::EnvOps, storage::registry::subnet::SubnetRegistryOps,
},
storage::{
canister::CanisterRecord,
stable::children::{CanisterChildren, CanisterChildrenRecord},
},
};
use mapper::CanisterRecordMapper;
pub struct CanisterChildrenOps;
impl CanisterChildrenOps {
#[must_use]
pub fn get(pid: Principal) -> Option<CanisterRecord> {
if EnvOps::is_root() {
SubnetRegistryOps::get(pid)
} else {
CanisterChildren::get(pid)
}
}
#[must_use]
pub fn contains_pid(pid: &Principal) -> bool {
if EnvOps::is_root() {
SubnetRegistryOps::children(IcOps::canister_self())
.iter()
.any(|(child_pid, _)| child_pid == pid)
} else {
Self::data().entries.iter().any(|(p, _)| p == pid)
}
}
#[must_use]
pub fn infos() -> Vec<CanisterInfo> {
Self::records()
.into_iter()
.map(|(pid, record)| CanisterRecordMapper::record_to_response(pid, record))
.collect()
}
#[must_use]
fn records() -> Vec<(Principal, CanisterRecord)> {
if EnvOps::is_root() {
SubnetRegistryOps::children(IcOps::canister_self())
} else {
Self::data().entries
}
}
#[must_use]
pub fn pids() -> Vec<Principal> {
Self::records().into_iter().map(|(pid, _)| pid).collect()
}
#[must_use]
pub fn data() -> CanisterChildrenRecord {
CanisterChildren::export()
}
pub(crate) fn import_direct_children(
parent_pid: Principal,
children: Vec<(Principal, CanisterRole)>,
) {
let data = CanisterChildrenRecord {
entries: children
.into_iter()
.map(|(pid, role)| {
(
pid,
CanisterRecord {
role,
parent_pid: Some(parent_pid),
module_hash: None,
created_at: 0,
},
)
})
.collect(),
};
CanisterChildren::import(data);
}
}