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