canic_core/ops/ic/
mod.rs

1//! IC-related ops helpers.
2//!
3//! This module groups low-level IC concerns (management canister calls, ICC call
4//! wrappers, HTTP outcalls, timers) under a single namespace to keep `ops/`
5//! navigable.
6
7pub mod call;
8pub mod cmc;
9pub mod http;
10pub mod ledger;
11pub mod mgmt;
12pub mod payment;
13pub mod provision;
14pub mod signature;
15pub mod timer;
16pub mod xrc;
17
18pub use mgmt::*;
19
20use crate::{Error, ThisError, ops::OpsError};
21
22///
23/// IcOpsError
24///
25
26#[derive(Debug, ThisError)]
27pub enum IcOpsError {
28    #[error(transparent)]
29    ProvisionOpsError(#[from] provision::ProvisionOpsError),
30
31    #[error(transparent)]
32    SignatureOpsError(#[from] signature::SignatureOpsError),
33}
34
35impl From<IcOpsError> for Error {
36    fn from(err: IcOpsError) -> Self {
37        OpsError::from(err).into()
38    }
39}
40
41///
42/// Network
43/// Identifies the environment the canister believes it runs in.
44///
45
46#[derive(Clone, Copy, Debug, Eq, PartialEq)]
47pub enum Network {
48    Ic,
49    Local,
50}
51
52impl Network {
53    #[must_use]
54    pub const fn as_str(self) -> &'static str {
55        match self {
56            Self::Ic => "ic",
57            Self::Local => "local",
58        }
59    }
60}
61
62impl core::fmt::Display for Network {
63    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
64        f.write_str(self.as_str())
65    }
66}
67
68///
69/// build_network_from_dfx_network
70/// Pure helper for `build_network()`, exposed for testing and reuse.
71///
72
73#[must_use]
74pub fn build_network_from_dfx_network(dfx_network: Option<&'static str>) -> Option<Network> {
75    match dfx_network {
76        Some("local") => Some(Network::Local),
77        Some("ic") => Some(Network::Ic),
78
79        _ => None,
80    }
81}
82
83///
84/// build_network
85/// Returns the network inferred at *build time* from `DFX_NETWORK`.
86/// This value is baked into the Wasm and does not reflect runtime state.
87///
88/// ChatGPT 5.2 Final, Precise Verdict
89///
90/// ✅ Yes, this works exactly as you say
91/// ✅ It is valid IC/Wasm code
92/// ❌ It is not runtime detection
93/// ⚠️ The danger is semantic, not technical
94/// ✅ Safe if treated as a build-time constant
95/// ❌ Dangerous if treated as authoritative runtime truth
96///
97
98#[must_use]
99pub fn build_network() -> Option<Network> {
100    build_network_from_dfx_network(option_env!("DFX_NETWORK"))
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn build_network_from_dfx_network_parses_ic() {
109        assert_eq!(
110            build_network_from_dfx_network(Some("ic")),
111            Some(Network::Ic)
112        );
113    }
114
115    #[test]
116    fn build_network_from_dfx_network_parses_local() {
117        assert_eq!(
118            build_network_from_dfx_network(Some("local")),
119            Some(Network::Local)
120        );
121    }
122
123    #[test]
124    fn build_network_from_dfx_network_rejects_unknown() {
125        assert_eq!(build_network_from_dfx_network(Some("nope")), None);
126    }
127
128    #[test]
129    fn build_network_from_dfx_network_handles_missing() {
130        assert_eq!(build_network_from_dfx_network(None), None);
131    }
132}