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