1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use async_trait::async_trait;
use hedera_proto::services;
use hedera_proto::services::file_service_client::FileServiceClient;
use tonic::transport::Channel;
use crate::protobuf::ToProtobuf;
use crate::transaction::{
AnyTransactionData,
ToTransactionDataProtobuf,
TransactionExecute,
};
use crate::{
AccountId,
FileId,
Transaction,
TransactionId,
};
pub type FileDeleteTransaction = Transaction<FileDeleteTransactionData>;
#[cfg_attr(feature = "ffi", serde_with::skip_serializing_none)]
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "ffi", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ffi", serde(rename_all = "camelCase"))]
pub struct FileDeleteTransactionData {
file_id: Option<FileId>,
}
impl FileDeleteTransaction {
pub fn file_id(&mut self, id: impl Into<FileId>) -> &mut Self {
self.body.data.file_id = Some(id.into());
self
}
}
#[async_trait]
impl TransactionExecute for FileDeleteTransactionData {
async fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> Result<tonic::Response<services::TransactionResponse>, tonic::Status> {
FileServiceClient::new(channel).delete_file(request).await
}
}
impl ToTransactionDataProtobuf for FileDeleteTransactionData {
fn to_transaction_data_protobuf(
&self,
_node_account_id: AccountId,
_transaction_id: &TransactionId,
) -> services::transaction_body::Data {
let file_id = self.file_id.as_ref().map(FileId::to_protobuf);
services::transaction_body::Data::FileDelete(services::FileDeleteTransactionBody {
file_id,
})
}
}
impl From<FileDeleteTransactionData> for AnyTransactionData {
fn from(transaction: FileDeleteTransactionData) -> Self {
Self::FileDelete(transaction)
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "ffi")]
mod ffi {
use assert_matches::assert_matches;
use crate::transaction::{
AnyTransaction,
AnyTransactionData,
};
use crate::{
FileDeleteTransaction,
FileId,
};
const FILE_DELETE_TRANSACTION_JSON: &str = r#"{
"$type": "fileDelete",
"fileId": "0.0.1001"
}"#;
#[test]
fn it_should_serialize() -> anyhow::Result<()> {
let mut transaction = FileDeleteTransaction::new();
transaction.file_id(FileId::from(1001));
let transaction_json = serde_json::to_string_pretty(&transaction)?;
assert_eq!(transaction_json, FILE_DELETE_TRANSACTION_JSON);
Ok(())
}
#[test]
fn it_should_deserialize() -> anyhow::Result<()> {
let transaction: AnyTransaction = serde_json::from_str(FILE_DELETE_TRANSACTION_JSON)?;
let data = assert_matches!(transaction.body.data, AnyTransactionData::FileDelete(transaction) => transaction);
assert_eq!(data.file_id.unwrap(), FileId::from(1001));
Ok(())
}
}
}