ic-query 0.30.0

Internet Computer query library for NNS, SNS, ICRC, system canisters, and public network metadata
Documentation
//! Module: subnet_catalog::resolver::model
//!
//! Defines resolver options and outputs for subnet catalog lookups.

use crate::subnet_catalog::{RoutingRange, SubnetCatalogProvenance, SubnetInfo};
use candid::Principal;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

///
/// ResolveAs
///
/// Caller-requested interpretation for an ambiguous principal input.
///

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ResolveAs {
    /// Resolve the input as a subnet principal.
    Subnet,
    /// Resolve the input as a canister principal.
    Canister,
}

impl ResolveAs {
    /// Returns the stable snake_case value used in CLI options and reports.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Subnet => "subnet",
            Self::Canister => "canister",
        }
    }
}

impl FromStr for ResolveAs {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "subnet" => Ok(Self::Subnet),
            "canister" => Ok(Self::Canister),
            other => Err(format!("invalid value {other}; use subnet or canister")),
        }
    }
}

///
/// ResolvedSubnetSubject
///
/// Subject type chosen by the resolver.
///

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ResolvedSubnetSubject {
    /// Resolver matched a subnet principal.
    Subnet,
    /// Resolver matched a canister principal through routing ranges.
    Canister,
}

impl ResolvedSubnetSubject {
    /// Returns the stable snake_case value used in reports.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Subnet => "subnet",
            Self::Canister => "canister",
        }
    }
}

///
/// ResolvedSubnet
///
/// Resolved subnet match and the evidence used to produce it.
///

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ResolvedSubnet {
    pub input_principal: String,
    pub resolved_as: ResolvedSubnetSubject,
    pub resolved_from: String,
    pub subnet: SubnetInfo,
    pub matched_canister_principal: Option<String>,
    pub matched_routing_range: Option<RoutingRange>,
    /// Lowercase SHA-256 digest of the raw catalog supplying the match.
    pub catalog_digest: String,
    /// Complete provenance supplied by the raw catalog.
    pub provenance: SubnetCatalogProvenance,
}

///
/// ResolvedCanisterRoute
///
/// Authority-bearing route tied to the exact validated catalog that proved it.
///

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ResolvedCanisterRoute {
    /// Canonical canister principal resolved through the routing table.
    pub canister: Principal,
    /// Canonical target Subnet principal.
    pub subnet: Principal,
    /// Complete validated Subnet classification matched by the route.
    pub subnet_info: SubnetInfo,
    /// Inclusive validated range containing the canister principal.
    pub matched_range: RoutingRange,
    /// Exact Registry version of the validated catalog.
    pub registry_version: u64,
    /// Binary SHA-256 digest of the validated catalog authority payload.
    pub catalog_digest: [u8; 32],
    /// Complete provenance of the validated catalog.
    pub provenance: SubnetCatalogProvenance,
}