ic-query 0.89.0

Internet Computer query library for CloudEngine, NNS, SNS, ICRC, system canisters, and public network metadata
Documentation
//! Module: subnet_catalog::model::classification::kind
//!
//! Defines stable subnet-kind labels and their default charging meaning.

use serde::{Deserialize, Serialize};
use std::str::FromStr;

///
/// SubnetKind
///
/// Subnet execution kind from registry or derived catalog classification.
///

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SubnetKind {
    /// Application subnet.
    Application,
    /// Cloud Engine subnet.
    CloudEngine,
    /// System subnet.
    System,
    /// Unknown or unclassified subnet kind.
    Unknown,
}

pub(in crate::subnet_catalog) const REGISTRY_SUBNET_KINDS: &[(i32, SubnetKind)] = &[
    (0, SubnetKind::Unknown),
    (1, SubnetKind::Application),
    (2, SubnetKind::System),
    (4, SubnetKind::Application),
    (5, SubnetKind::CloudEngine),
];

impl SubnetKind {
    #[cfg(feature = "subnet-catalog-host")]
    pub(in crate::subnet_catalog) const ALL: [Self; 4] = [
        Self::Application,
        Self::CloudEngine,
        Self::System,
        Self::Unknown,
    ];
    /// Convert one raw Registry `SubnetType` numeric discriminant.
    #[must_use]
    pub const fn from_registry_subnet_type(raw_subnet_type: i32) -> Self {
        let mut index = 0;
        while index < REGISTRY_SUBNET_KINDS.len() {
            let (raw, kind) = REGISTRY_SUBNET_KINDS[index];
            if raw == raw_subnet_type {
                return kind;
            }
            index += 1;
        }
        Self::Unknown
    }

    /// Returns the stable snake_case value used in CLI filters and text output.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Application => "application",
            Self::CloudEngine => "cloud_engine",
            Self::System => "system",
            Self::Unknown => "unknown",
        }
    }

    /// Returns whether a subject on this subnet kind normally incurs application charges.
    #[must_use]
    pub const fn charges_apply_by_default(self) -> bool {
        matches!(self, Self::Application | Self::CloudEngine)
    }
}

impl FromStr for SubnetKind {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "application" => Ok(Self::Application),
            "cloud_engine" => Ok(Self::CloudEngine),
            "system" => Ok(Self::System),
            "unknown" => Ok(Self::Unknown),
            other => Err(format!(
                "invalid value {other}; use application, cloud_engine, system, or unknown"
            )),
        }
    }
}