canic_core/ops/storage/
env.rs

1use crate::{
2    Error, ThisError,
3    cdk::types::Principal,
4    ids::{CanisterRole, SubnetRole},
5    model::memory::Env,
6    ops::storage::StorageOpsError,
7};
8
9pub use crate::model::memory::env::EnvData;
10
11///
12/// EnvOpsError
13///
14
15#[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        StorageOpsError::from(err).into()
36    }
37}
38
39///
40/// EnvOps
41///
42
43pub struct EnvOps;
44
45impl EnvOps {
46    pub fn import(env: EnvData) {
47        Env::import(env);
48    }
49
50    pub fn set_prime_root_pid(pid: Principal) {
51        Env::set_prime_root_pid(pid);
52    }
53
54    pub fn set_subnet_role(role: SubnetRole) {
55        Env::set_subnet_role(role);
56    }
57
58    pub fn set_subnet_pid(pid: Principal) {
59        Env::set_subnet_pid(pid);
60    }
61
62    pub fn set_root_pid(pid: Principal) {
63        Env::set_root_pid(pid);
64    }
65
66    pub fn set_canister_role(role: CanisterRole) {
67        Env::set_canister_role(role);
68    }
69
70    #[must_use]
71    pub fn is_prime_root() -> bool {
72        Env::is_prime_root()
73    }
74
75    #[must_use]
76    pub fn is_prime_subnet() -> bool {
77        Env::is_prime_subnet()
78    }
79
80    #[must_use]
81    pub fn is_root() -> bool {
82        Env::is_root()
83    }
84
85    pub fn try_get_subnet_role() -> Result<SubnetRole, Error> {
86        let ty = Env::get_subnet_role().ok_or(EnvOpsError::SubnetRoleUnavailable)?;
87
88        Ok(ty)
89    }
90
91    pub fn try_get_canister_role() -> Result<CanisterRole, Error> {
92        let ty = Env::get_canister_role().ok_or(EnvOpsError::CanisterRoleUnavailable)?;
93
94        Ok(ty)
95    }
96
97    pub fn try_get_subnet_pid() -> Result<Principal, Error> {
98        let pid = Env::get_subnet_pid().ok_or(EnvOpsError::SubnetPidUnavailable)?;
99
100        Ok(pid)
101    }
102
103    pub fn try_get_root_pid() -> Result<Principal, Error> {
104        let pid = Env::get_root_pid().ok_or(EnvOpsError::RootPidUnavailable)?;
105
106        Ok(pid)
107    }
108
109    pub fn try_get_prime_root_pid() -> Result<Principal, Error> {
110        let pid = Env::get_prime_root_pid().ok_or(EnvOpsError::RootPidUnavailable)?;
111
112        Ok(pid)
113    }
114
115    pub fn try_get_parent_pid() -> Result<Principal, Error> {
116        let pid = Env::get_parent_pid().ok_or(EnvOpsError::ParentPidUnavailable)?;
117
118        Ok(pid)
119    }
120
121    /// Export a snapshot of the current environment metadata.
122    #[must_use]
123    pub fn export() -> EnvData {
124        Env::export()
125    }
126}