Skip to main content

hiero_sdk/mirror_query/
any.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use futures_core::future::BoxFuture;
4use futures_core::stream::BoxStream;
5use futures_util::TryStreamExt;
6
7use super::subscribe::MirrorQueryExecute;
8use crate::topic::TopicMessageQueryData;
9use crate::{
10    MirrorQuery,
11    NodeAddress,
12    NodeAddressBookQueryData,
13    TopicMessage,
14};
15
16/// Represents any possible query to the mirror network.
17pub type AnyMirrorQuery = MirrorQuery<AnyMirrorQueryData>;
18
19#[derive(Debug, Clone)]
20pub enum AnyMirrorQueryData {
21    NodeAddressBook(NodeAddressBookQueryData),
22    TopicMessage(TopicMessageQueryData),
23}
24
25#[derive(Debug, Clone)]
26pub enum AnyMirrorQueryMessage {
27    NodeAddressBook(NodeAddress),
28    TopicMessage(TopicMessage),
29}
30
31/// Represents the response of any possible query to the mirror network.
32pub enum AnyMirrorQueryResponse {
33    /// Response for `AnyMirrorQuery::NodeAddressBook`.
34    NodeAddressBook(<NodeAddressBookQueryData as MirrorQueryExecute>::Response),
35    /// Response for `AnyMirrorQuery::TopicMessage`.
36    TopicMessage(<TopicMessageQueryData as MirrorQueryExecute>::Response),
37}
38
39impl MirrorQueryExecute for AnyMirrorQueryData {
40    type Item = AnyMirrorQueryMessage;
41
42    type Response = AnyMirrorQueryResponse;
43
44    type ItemStream<'a>
45        = BoxStream<'a, crate::Result<Self::Item>>
46    where
47        Self: 'a;
48
49    fn subscribe_with_optional_timeout<'a>(
50        &self,
51        params: &crate::mirror_query::MirrorQueryCommon,
52        client: &'a crate::Client,
53        timeout: Option<std::time::Duration>,
54    ) -> Self::ItemStream<'a>
55    where
56        Self: 'a,
57    {
58        match self {
59            AnyMirrorQueryData::NodeAddressBook(it) => Box::pin(
60                it.subscribe_with_optional_timeout(params, client, timeout)
61                    .map_ok(Self::Item::from),
62            ),
63            AnyMirrorQueryData::TopicMessage(it) => Box::pin(
64                it.subscribe_with_optional_timeout(params, client, timeout)
65                    .map_ok(Self::Item::from),
66            ),
67        }
68    }
69
70    fn execute_with_optional_timeout<'a>(
71        &'a self,
72        params: &'a super::MirrorQueryCommon,
73        client: &'a crate::Client,
74        timeout: Option<std::time::Duration>,
75    ) -> BoxFuture<'a, crate::Result<Self::Response>> {
76        match self {
77            AnyMirrorQueryData::NodeAddressBook(it) => Box::pin(async move {
78                it.execute_with_optional_timeout(params, client, timeout)
79                    .await
80                    .map(Self::Response::from)
81            }),
82            AnyMirrorQueryData::TopicMessage(it) => Box::pin(async move {
83                it.execute_with_optional_timeout(params, client, timeout)
84                    .await
85                    .map(Self::Response::from)
86            }),
87        }
88    }
89}