Skip to main content

ic_query/subnet_catalog/resolver/
model.rs

1//! Module: subnet_catalog::resolver::model
2//!
3//! Defines resolver options and outputs for subnet catalog lookups.
4
5use crate::subnet_catalog::{RoutingRange, SubnetInfo};
6use serde::{Deserialize, Serialize};
7use std::str::FromStr;
8
9///
10/// ResolveAs
11///
12/// Caller-requested interpretation for an ambiguous principal input.
13///
14
15#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
16#[serde(rename_all = "snake_case")]
17pub enum ResolveAs {
18    /// Resolve the input as a subnet principal.
19    Subnet,
20    /// Resolve the input as a canister principal.
21    Canister,
22}
23
24impl ResolveAs {
25    /// Returns the stable snake_case value used in CLI options and reports.
26    #[must_use]
27    pub const fn as_str(self) -> &'static str {
28        match self {
29            Self::Subnet => "subnet",
30            Self::Canister => "canister",
31        }
32    }
33}
34
35impl FromStr for ResolveAs {
36    type Err = String;
37
38    fn from_str(value: &str) -> Result<Self, Self::Err> {
39        match value {
40            "subnet" => Ok(Self::Subnet),
41            "canister" => Ok(Self::Canister),
42            other => Err(format!("invalid value {other}; use subnet or canister")),
43        }
44    }
45}
46
47///
48/// ResolvedSubnetSubject
49///
50/// Subject type chosen by the resolver.
51///
52
53#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
54#[serde(rename_all = "snake_case")]
55pub enum ResolvedSubnetSubject {
56    /// Resolver matched a subnet principal.
57    Subnet,
58    /// Resolver matched a canister principal through routing ranges.
59    Canister,
60}
61
62impl ResolvedSubnetSubject {
63    /// Returns the stable snake_case value used in reports.
64    #[must_use]
65    pub const fn as_str(self) -> &'static str {
66        match self {
67            Self::Subnet => "subnet",
68            Self::Canister => "canister",
69        }
70    }
71}
72
73///
74/// ResolvedSubnet
75///
76/// Resolved subnet match and the evidence used to produce it.
77///
78
79#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
80pub struct ResolvedSubnet {
81    pub input_principal: String,
82    pub resolved_as: ResolvedSubnetSubject,
83    pub resolved_from: String,
84    pub subnet: SubnetInfo,
85    pub matched_canister_principal: Option<String>,
86    pub matched_routing_range: Option<RoutingRange>,
87}