hiero_sdk/file/
file_contents_query.rs1use hiero_sdk_proto::services;
4use hiero_sdk_proto::services::file_service_client::FileServiceClient;
5use tonic::transport::Channel;
6
7use crate::ledger_id::RefLedgerId;
8use crate::query::{
9 AnyQueryData,
10 Query,
11 QueryExecute,
12 ToQueryProtobuf,
13};
14use crate::{
15 BoxGrpcFuture,
16 Error,
17 FileContentsResponse,
18 FileId,
19 ToProtobuf,
20 ValidateChecksums,
21};
22
23pub type FileContentsQuery = Query<FileContentsQueryData>;
25
26#[derive(Clone, Default, Debug)]
27pub struct FileContentsQueryData {
28 file_id: Option<FileId>,
30}
31
32impl From<FileContentsQueryData> for AnyQueryData {
33 #[inline]
34 fn from(data: FileContentsQueryData) -> Self {
35 Self::FileContents(data)
36 }
37}
38
39impl FileContentsQuery {
40 #[must_use]
42 pub fn get_file_id(&self) -> Option<FileId> {
43 self.data.file_id
44 }
45
46 pub fn file_id(&mut self, id: impl Into<FileId>) -> &mut Self {
48 self.data.file_id = Some(id.into());
49 self
50 }
51}
52
53impl ToQueryProtobuf for FileContentsQueryData {
54 fn to_query_protobuf(&self, header: services::QueryHeader) -> services::Query {
55 services::Query {
56 query: Some(services::query::Query::FileGetContents(services::FileGetContentsQuery {
57 header: Some(header),
58 file_id: self.file_id.to_protobuf(),
59 })),
60 }
61 }
62}
63
64impl QueryExecute for FileContentsQueryData {
65 type Response = FileContentsResponse;
66
67 fn execute(
68 &self,
69 channel: Channel,
70 request: services::Query,
71 ) -> BoxGrpcFuture<'_, services::Response> {
72 Box::pin(async { FileServiceClient::new(channel).get_file_content(request).await })
73 }
74}
75
76impl ValidateChecksums for FileContentsQueryData {
77 fn validate_checksums(&self, ledger_id: &RefLedgerId) -> Result<(), Error> {
78 self.file_id.validate_checksums(ledger_id)
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use expect_test::expect;
85
86 use crate::query::ToQueryProtobuf;
87 use crate::{
88 FileContentsQuery,
89 FileId,
90 Hbar,
91 };
92
93 #[test]
94 fn serialize() {
95 expect![[r#"
96 Query {
97 query: Some(
98 FileGetContents(
99 FileGetContentsQuery {
100 header: Some(
101 QueryHeader {
102 payment: None,
103 response_type: AnswerOnly,
104 },
105 ),
106 file_id: Some(
107 FileId {
108 shard_num: 0,
109 realm_num: 0,
110 file_num: 5005,
111 },
112 ),
113 },
114 ),
115 ),
116 }
117 "#]]
118 .assert_debug_eq(
119 &FileContentsQuery::new()
120 .file_id(FileId::new(0, 0, 5005))
121 .max_payment_amount(Hbar::from_tinybars(100_000))
122 .data
123 .to_query_protobuf(Default::default()),
124 );
125 }
126
127 #[test]
128 fn get_set_file_id() {
129 let mut query = FileContentsQuery::new();
130 query.file_id(FileId::new(0, 0, 5005));
131
132 assert_eq!(query.get_file_id(), Some(FileId::new(0, 0, 5005)));
133 }
134}