1macro_rules! include_proto {
3 ($package:tt) => {
4 include!(concat!(env!("OUT_DIR"), "/", $package, ".rs"));
5 };
6}
7
8pub mod arcadius2 {
9 include_proto!("m10.arcadius2");
10
11 use crate::{Collection, Pack};
12
13 impl From<prost::bytes::Bytes> for Value {
14 fn from(bytes: prost::bytes::Bytes) -> Value {
15 Value {
16 value: Some(value::Value::BytesValue(bytes)),
17 }
18 }
19 }
20
21 pub const FILE_DESCRIPTOR_SET_BYTES: &[u8] =
22 include_bytes!(concat!(env!("OUT_DIR"), "/m10.arcadius2.bin"));
23 pub static FILE_DESCRIPTOR_SET: once_cell::sync::Lazy<prost_types::FileDescriptorSet> =
24 once_cell::sync::Lazy::new(|| {
25 prost::Message::decode(FILE_DESCRIPTOR_SET_BYTES).expect("file descriptor parse failed")
26 });
27
28 impl Operation {
29 pub fn insert<D: Pack>(document: D) -> Self {
30 Self {
31 operation: Some(operation::Operation::InsertDocument(
32 operation::InsertDocument {
33 collection: D::COLLECTION.to_string(),
34 document: document.pack(),
35 },
36 )),
37 }
38 }
39
40 pub fn delete<D: Pack>(id: Vec<u8>) -> Self {
41 Self {
42 operation: Some(operation::Operation::DeleteDocument(
43 operation::DeleteDocument {
44 collection: D::COLLECTION.to_string(),
45 primary_key: Some(bytes::Bytes::from(id).into()),
46 },
47 )),
48 }
49 }
50
51 pub fn new_index<D: Pack>(path: Vec<String>) -> Self {
52 Self {
53 operation: Some(operation::Operation::InsertIndex(operation::InsertIndex {
54 collection: D::COLLECTION.to_string(),
55 path: path.join("."),
56 })),
57 }
58 }
59
60 pub fn new_collection(
61 name: String,
62 descriptor_name: String,
63 index_metadata: Vec<IndexMetadata>,
64 ) -> Self {
65 Self {
66 operation: Some(operation::Operation::InsertCollection(CollectionMetadata {
67 name,
68 descriptor_name,
69 file_descriptor_set: Some(crate::sdk::FILE_DESCRIPTOR_SET.clone()),
70 primary_key_path: "id".to_string(),
71 index_metadata,
72 })),
73 }
74 }
75 }
76
77 impl Pack for RoleBinding {
78 const COLLECTION: Collection = Collection::RoleBindings;
79 fn set_id(&mut self, id: Vec<u8>) {
80 self.id = bytes::Bytes::from(id);
81 }
82 fn id(&self) -> &[u8] {
83 &self.id
84 }
85 }
86
87 impl Pack for Role {
88 const COLLECTION: Collection = Collection::Roles;
89 fn set_id(&mut self, id: Vec<u8>) {
90 self.id = bytes::Bytes::from(id);
91 }
92 fn id(&self) -> &[u8] {
93 &self.id
94 }
95 }
96}
97
98pub mod directory {
99 include_proto!("m10.directory");
100 use core::fmt;
101 use core::str::FromStr;
102
103 #[derive(Debug)]
104 pub struct InvalidAliasType();
105
106 impl fmt::Display for InvalidAliasType {
107 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
108 f.write_str("invalid alias type")
109 }
110 }
111
112 impl std::error::Error for InvalidAliasType {}
113
114 impl FromStr for alias::Type {
115 type Err = InvalidAliasType;
116 fn from_str(s: &str) -> Result<Self, Self::Err> {
117 match s {
118 "handle" => Ok(alias::Type::Handle),
119 "email" => Ok(alias::Type::Email),
120 "phone" => Ok(alias::Type::Phone),
121 _ => Err(InvalidAliasType()),
122 }
123 }
124 }
125
126 impl AsRef<str> for alias::Type {
127 fn as_ref(&self) -> &str {
128 match self {
129 alias::Type::Handle => "handle",
130 alias::Type::Email => "email",
131 alias::Type::Phone => "phone",
132 }
133 }
134 }
135
136 impl fmt::Display for alias::Type {
137 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
138 f.write_str(self.as_ref())
139 }
140 }
141}
142
143pub mod sdk {
144 include_proto!("m10.sdk");
145
146 pub mod model {
147 include_proto!("m10.sdk.model");
148 pub const FILE_DESCRIPTOR_SET_BYTES: &[u8] =
149 include_bytes!(concat!(env!("OUT_DIR"), "/m10.model.pb"));
150 pub static FILE_DESCRIPTOR_SET: once_cell::sync::Lazy<prost_types::FileDescriptorSet> =
151 once_cell::sync::Lazy::new(|| {
152 prost::Message::decode(FILE_DESCRIPTOR_SET_BYTES)
153 .expect("file descriptor parse failed")
154 });
155 }
156 pub mod transaction {
157 include_proto!("m10.sdk.transaction");
158 }
159 pub mod metadata {
160 include_proto!("m10.sdk.metadata");
161 }
162 pub use metadata::*;
163 pub use model::*;
164 use prost::Message;
165 pub use transaction::*;
166
167 use core::{fmt, str};
168 use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
169
170 use crate::{arcadius2, Collection, Pack};
171
172 impl str::FromStr for AccountRef {
173 type Err = AccountRefParseError;
174
175 fn from_str(s: &str) -> Result<Self, Self::Err> {
176 let mut s = s.split('/');
177 let ledger_id = s.next().ok_or(AccountRefParseError())?.to_string();
178 let account_id = s.next().ok_or(AccountRefParseError())?;
179 let account_id = hex::decode(account_id).map_err(|_| AccountRefParseError())?;
180 Ok(Self {
181 ledger_id,
182 account_id,
183 })
184 }
185 }
186
187 impl Serialize for AccountRef {
188 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
189 where
190 S: Serializer,
191 {
192 serializer.collect_str(self)
193 }
194 }
195
196 impl<'de> Deserialize<'de> for AccountRef {
197 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
198 where
199 D: Deserializer<'de>,
200 {
201 let s = String::deserialize(deserializer)?;
202 s.parse().map_err(de::Error::custom)
203 }
204 }
205
206 impl fmt::Display for AccountRef {
207 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208 write!(f, "{}/{}", self.ledger_id, hex::encode(&self.account_id))
209 }
210 }
211
212 #[derive(Debug)]
213 pub struct AccountRefParseError();
214
215 impl fmt::Display for AccountRefParseError {
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 f.write_str("failed to parse account ref")
218 }
219 }
220
221 impl std::error::Error for AccountRefParseError {}
222
223 impl Pack for AccountSet {
224 const COLLECTION: Collection = Collection::AccountSets;
225 fn set_id(&mut self, id: Vec<u8>) {
226 self.id = id;
227 }
228 fn id(&self) -> &[u8] {
229 &self.id
230 }
231 }
232
233 impl Pack for Account {
234 const COLLECTION: Collection = Collection::Accounts;
235 fn set_id(&mut self, id: Vec<u8>) {
236 self.id = id;
237 }
238 fn id(&self) -> &[u8] {
239 &self.id
240 }
241 }
242
243 impl From<CreateTransfer> for transaction_request_payload::Data {
244 fn from(create_transfer: CreateTransfer) -> Self {
245 Self::Transfer(create_transfer)
246 }
247 }
248
249 impl From<CreateLedgerAccount> for transaction_request_payload::Data {
250 fn from(request: CreateLedgerAccount) -> Self {
251 Self::CreateLedgerAccount(request)
252 }
253 }
254
255 impl From<SetFreezeState> for transaction_request_payload::Data {
256 fn from(request: SetFreezeState) -> Self {
257 Self::SetFreezeState(request)
258 }
259 }
260
261 impl From<InvokeAction> for transaction_request_payload::Data {
262 fn from(request: InvokeAction) -> Self {
263 Self::InvokeAction(request)
264 }
265 }
266
267 impl From<CommitTransfer> for transaction_request_payload::Data {
268 fn from(request: CommitTransfer) -> Self {
269 Self::CommitTransfer(request)
270 }
271 }
272
273 impl From<arcadius2::DocumentOperations> for transaction_request_payload::Data {
274 fn from(operations: arcadius2::DocumentOperations) -> Self {
275 Self::DocumentOperations(operations)
276 }
277 }
278
279 impl From<Vec<arcadius2::Operation>> for transaction_request_payload::Data {
280 fn from(operations: Vec<arcadius2::Operation>) -> Self {
281 Self::from(arcadius2::DocumentOperations { operations })
282 }
283 }
284
285 impl From<arcadius2::Operation> for transaction_request_payload::Data {
286 fn from(operation: arcadius2::Operation) -> Self {
287 Self::from(vec![operation])
288 }
289 }
290
291 impl From<CreateLedgerTransfers> for Contract {
292 fn from(transfers: CreateLedgerTransfers) -> Self {
293 Self {
294 transactions: transfers.encode_to_vec(),
295 ..Default::default()
296 }
297 }
298 }
299
300 impl TransactionResponse {
301 pub fn tx_error(self) -> Result<Self, TransactionError> {
302 match self.error {
303 Some(err) => Err(err),
304 None => Ok(self),
305 }
306 }
307 }
308
309 impl fmt::Display for TransactionError {
310 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
311 write!(f, "{:?}: {}", self.code(), self.message)
312 }
313 }
314
315 impl std::error::Error for TransactionError {}
316}
317
318pub mod health {
319 include_proto!("grpc.health.v1");
320}
321
322mod metadata;
323mod pack;
324
325pub mod prost {
327 pub use prost::*;
328 pub use prost_types::*;
329}
330pub use metadata::*;
331pub use pack::{Collection, Pack};
332
333use prost_types::Any;
334use serde::{Deserialize, Serialize};
335use serde_with::{DeserializeAs, SerializeAs};
336
337pub struct AnySerDeCompat;
338
339#[derive(Serialize)]
340struct AnySerializeWrapper<'a> {
341 pub type_url: &'a str,
342 pub value: &'a [u8],
343}
344
345#[derive(Deserialize)]
346struct AnyDeserializeWrapper {
347 pub type_url: String,
348 pub value: Vec<u8>,
349}
350
351impl SerializeAs<Any> for AnySerDeCompat {
352 fn serialize_as<S>(source: &Any, serializer: S) -> Result<S::Ok, S::Error>
353 where
354 S: serde::Serializer,
355 {
356 AnySerializeWrapper {
357 type_url: &source.type_url,
358 value: &source.value,
359 }
360 .serialize(serializer)
361 }
362}
363
364impl<'de> DeserializeAs<'de, Any> for AnySerDeCompat {
365 fn deserialize_as<D>(deserializer: D) -> Result<Any, D::Error>
366 where
367 D: serde::Deserializer<'de>,
368 {
369 let AnyDeserializeWrapper { type_url, value } =
370 AnyDeserializeWrapper::deserialize(deserializer)?;
371 Ok(Any { type_url, value })
372 }
373}