ic_query/sns/report/model/requests/lookup/
mod.rs1use ic_platform::candid::contract::Principal;
8use thiserror::Error as ThisError;
9
10#[derive(Clone, Debug, Eq, PartialEq, ThisError)]
17#[error("invalid SNS root principal: {input}")]
18pub struct SnsLookupError {
19 pub input: String,
21}
22
23#[derive(Clone, Debug, Eq, PartialEq)]
24enum SnsLookupTarget {
25 Id(usize),
26 Root(Principal),
27}
28
29#[derive(Clone, Debug, Eq, PartialEq)]
36pub struct SnsLookupRequest {
37 pub network: String,
38 pub source_endpoint: String,
39 pub now_unix_secs: u64,
40 input: String,
41 target: SnsLookupTarget,
42}
43
44impl SnsLookupRequest {
45 pub fn new(
47 network: impl Into<String>,
48 source_endpoint: impl Into<String>,
49 now_unix_secs: u64,
50 input: impl Into<String>,
51 ) -> Result<Self, SnsLookupError> {
52 let input = input.into();
53 let target = match input.parse::<usize>() {
54 Ok(id) => SnsLookupTarget::Id(id),
55 Err(_) => {
56 SnsLookupTarget::Root(Principal::from_text(&input).map_err(|_| SnsLookupError {
57 input: input.clone(),
58 })?)
59 }
60 };
61 Ok(Self {
62 network: network.into(),
63 source_endpoint: source_endpoint.into(),
64 now_unix_secs,
65 input,
66 target,
67 })
68 }
69
70 #[must_use]
72 pub fn input(&self) -> &str {
73 &self.input
74 }
75
76 #[must_use]
78 pub const fn id(&self) -> Option<usize> {
79 match self.target {
80 SnsLookupTarget::Id(id) => Some(id),
81 SnsLookupTarget::Root(_) => None,
82 }
83 }
84
85 #[must_use]
87 pub const fn root_canister_id(&self) -> Option<&Principal> {
88 match &self.target {
89 SnsLookupTarget::Root(root) => Some(root),
90 SnsLookupTarget::Id(_) => None,
91 }
92 }
93}
94
95#[cfg(test)]
96mod tests;