use serde::{Deserialize, Serialize};
use std::str::FromStr;
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SubnetKind {
Application,
CloudEngine,
System,
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,
];
#[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
}
#[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",
}
}
#[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"
)),
}
}
}