canic_core/ops/model/memory/
env.rs1use crate::{
2 Error, ThisError,
3 ids::{CanisterRole, SubnetRole},
4 model::memory::Env,
5 ops::model::memory::MemoryOpsError,
6 types::Principal,
7};
8
9pub use crate::model::memory::env::EnvData;
10
11#[derive(Debug, ThisError)]
16pub enum EnvOpsError {
17 #[error("failed to determine current canister type")]
18 CanisterRoleUnavailable,
19
20 #[error("failed to determine current parent principal")]
21 ParentPidUnavailable,
22
23 #[error("failed to determine current subnet principal")]
24 SubnetPidUnavailable,
25
26 #[error("failed to determine current subnet type")]
27 SubnetRoleUnavailable,
28
29 #[error("failed to determine current root principal")]
30 RootPidUnavailable,
31}
32
33impl From<EnvOpsError> for Error {
34 fn from(err: EnvOpsError) -> Self {
35 MemoryOpsError::from(err).into()
36 }
37}
38
39pub struct EnvOps;
44
45impl EnvOps {
46 pub fn import(env: EnvData) {
47 Env::import(env);
48 }
49
50 pub fn set_canister_type(ty: CanisterRole) {
51 Env::set_canister_type(ty);
52 }
53
54 pub fn set_root_pid(pid: Principal) {
55 Env::set_root_pid(pid);
56 }
57
58 pub fn set_prime_root_pid(pid: Principal) {
59 Env::set_prime_root_pid(pid);
60 }
61
62 pub fn set_subnet_pid(pid: Principal) {
63 Env::set_subnet_pid(pid);
64 }
65
66 pub fn set_subnet_type(ty: SubnetRole) {
67 Env::set_subnet_type(ty);
68 }
69
70 #[must_use]
71 pub fn is_root() -> bool {
72 Env::is_root()
73 }
74
75 #[must_use]
76 pub fn is_prime_root() -> bool {
77 Env::is_prime_root()
78 }
79
80 pub fn try_get_subnet_type() -> Result<SubnetRole, Error> {
81 let ty = Env::get_subnet_type().ok_or(EnvOpsError::SubnetRoleUnavailable)?;
82
83 Ok(ty)
84 }
85
86 pub fn try_get_canister_type() -> Result<CanisterRole, Error> {
87 let ty = Env::get_canister_type().ok_or(EnvOpsError::CanisterRoleUnavailable)?;
88
89 Ok(ty)
90 }
91
92 pub fn try_get_subnet_pid() -> Result<Principal, Error> {
93 let pid = Env::get_subnet_pid().ok_or(EnvOpsError::SubnetPidUnavailable)?;
94
95 Ok(pid)
96 }
97
98 pub fn try_get_root_pid() -> Result<Principal, Error> {
99 let pid = Env::get_root_pid().ok_or(EnvOpsError::RootPidUnavailable)?;
100
101 Ok(pid)
102 }
103
104 pub fn try_get_prime_root_pid() -> Result<Principal, Error> {
105 let pid = Env::get_prime_root_pid().ok_or(EnvOpsError::RootPidUnavailable)?;
106
107 Ok(pid)
108 }
109
110 pub fn try_get_parent_pid() -> Result<Principal, Error> {
111 let pid = Env::get_parent_pid().ok_or(EnvOpsError::ParentPidUnavailable)?;
112
113 Ok(pid)
114 }
115
116 #[must_use]
118 pub fn export() -> EnvData {
119 Env::export()
120 }
121}