Skip to main content

canic_host/canister_ready/
mod.rs

1//! Module: canister_ready
2//!
3//! Responsibility: query the maintained Canic readiness endpoint.
4//! Does not own: readiness state, local replica transport, or install orchestration.
5//! Boundary: selects one transport and decodes the canonical boolean response.
6
7use crate::{
8    icp::{IcpCli, IcpCommandError, IcpJsonResponseError, decode_json_response},
9    replica_query::{self, ReplicaQueryError},
10};
11use std::path::Path;
12use thiserror::Error as ThisError;
13
14const CANIC_READY_METHOD: &str = "canic_ready";
15const ICP_JSON_OUTPUT: &str = "json";
16
17///
18/// CanisterReadyQueryError
19///
20
21#[derive(Debug, ThisError)]
22pub enum CanisterReadyQueryError {
23    #[error(transparent)]
24    Icp(#[from] IcpCommandError),
25
26    #[error(transparent)]
27    Replica(#[from] ReplicaQueryError),
28
29    #[error(transparent)]
30    Response(#[from] IcpJsonResponseError),
31}
32
33/// Query `canic_ready`, using the local replica API for local environment targets.
34pub fn query_canister_ready(
35    icp: &IcpCli,
36    canister_id: &str,
37    environment: &str,
38    icp_root: Option<&Path>,
39    candid_path: Option<&Path>,
40) -> Result<bool, CanisterReadyQueryError> {
41    if replica_query::should_use_local_replica_query(Some(environment)) {
42        return query_local_canister_ready(environment, canister_id, icp_root).map_err(Into::into);
43    }
44
45    query_canister_ready_with_icp(icp, canister_id, candid_path)
46}
47
48/// Query `canic_ready` directly through the local replica API.
49pub fn query_local_canister_ready(
50    environment: &str,
51    canister_id: &str,
52    icp_root: Option<&Path>,
53) -> Result<bool, ReplicaQueryError> {
54    replica_query::query_ready(Some(environment), canister_id, icp_root)
55}
56
57fn query_canister_ready_with_icp(
58    icp: &IcpCli,
59    canister_id: &str,
60    candid_path: Option<&Path>,
61) -> Result<bool, CanisterReadyQueryError> {
62    let output = icp.canister_query_output_with_candid(
63        canister_id,
64        CANIC_READY_METHOD,
65        Some(ICP_JSON_OUTPUT),
66        candid_path,
67    )?;
68    decode_json_response(&output).map_err(Into::into)
69}