xpx_chain_sdk/models/metadata/
metadata.rs1use std::collections::HashMap;
8use std::fmt;
9
10use {
11 num_enum::IntoPrimitive,
12 serde::{Serialize, Serializer},
13};
14
15use crate::models::{
16 account::Address, consts::SIZE_SIZE, mosaic::MosaicId, namespace::NamespaceId,
17};
18use crate::mosaic::UnresolvedMosaicId;
19
20#[derive(Debug, Clone, Copy, PartialEq, Deserialize, IntoPrimitive)]
25#[repr(u8)]
26pub enum MetadataType {
27 MetadataNone,
28 MetadataAddressType,
29 MetadataMosaicType,
30 MetadataNamespaceType,
31}
32
33impl MetadataType {
34 pub fn value(self) -> u8 {
35 self.into()
36 }
37
38 pub fn to_bytes(&self) -> [u8; 1] {
39 [self.value()]
40 }
41}
42
43impl From<u8> for MetadataType {
44 fn from(num: u8) -> Self {
45 use MetadataType::*;
46 match num {
47 1 => MetadataAddressType,
48 2 => MetadataMosaicType,
49 3 => MetadataNamespaceType,
50 _ => MetadataNone,
51 }
52 }
53}
54
55impl fmt::Display for MetadataType {
56 fn fmt(&self, e: &mut fmt::Formatter) -> fmt::Result {
57 write!(e, "{:?}", &self)
58 }
59}
60
61impl Serialize for MetadataType {
62 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
63 where
64 S: Serializer,
65 {
66 serializer.serialize_u8(self.value())
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize, IntoPrimitive)]
74#[repr(u8)]
75pub enum MetadataModificationType {
76 #[serde(rename = "0")]
77 Add,
78 #[serde(rename = "1")]
79 Remove,
80 NotSupported,
81}
82
83impl MetadataModificationType {
84 pub fn value(self) -> u8 {
85 self.into()
86 }
87
88 pub fn to_bytes(self) -> [u8; 1] {
89 [self.value()]
90 }
91}
92
93impl From<u8> for MetadataModificationType {
94 fn from(num: u8) -> Self {
95 use MetadataModificationType::*;
96 match num {
97 0 => Add,
98 1 => Remove,
99 _ => NotSupported,
100 }
101 }
102}
103
104impl fmt::Display for MetadataModificationType {
105 fn fmt(&self, e: &mut fmt::Formatter) -> fmt::Result {
106 write!(e, "{:?}", &self)
107 }
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct MetadataInfo {
112 #[serde(rename = "metadataId")]
113 pub r#type: MetadataType,
114 pub fields: HashMap<String, String>,
115}
116
117#[derive(Debug, Clone, Serialize, Deserialize)]
118pub struct AddressMetadataInfo {
119 pub info: MetadataInfo,
120 pub address: Address,
121}
122
123impl fmt::Display for AddressMetadataInfo {
124 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125 write!(f, "{}", serde_json::to_string_pretty(&self).unwrap_or_default())
126 }
127}
128
129#[derive(Debug, Clone, Serialize, Deserialize)]
130pub struct MosaicMetadataInfo {
131 pub info: MetadataInfo,
132 pub mosaic_id: MosaicId,
133}
134
135impl fmt::Display for MosaicMetadataInfo {
136 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137 write!(f, "{}", serde_json::to_string_pretty(&self).unwrap_or_default())
138 }
139}
140
141#[derive(Debug, Clone, Serialize, Deserialize)]
142pub struct NamespaceMetadataInfo {
143 pub info: MetadataInfo,
144 pub namespace_id: NamespaceId,
145}
146
147impl fmt::Display for NamespaceMetadataInfo {
148 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
149 write!(f, "{}", serde_json::to_string_pretty(&self).unwrap_or_default())
150 }
151}
152
153#[derive(Default, Serialize)]
154#[serde(rename_all = "camelCase")]
155pub struct MetadataIds {
156 #[serde(rename = "metadataIds", skip_serializing_if = "Option::is_none")]
158 pub ids: Option<Vec<String>>,
159}
160
161impl From<Vec<&str>> for MetadataIds {
162 #[inline]
163 fn from(ids: Vec<&str>) -> Self {
164 let mut metadata_ids = MetadataIds::default();
165
166 let addresses: Vec<String> =
167 ids.into_iter().map(|id| id.trim().replace("-", "").to_uppercase()).collect();
168
169 if !addresses.is_empty() {
170 metadata_ids.ids = Some(addresses);
171 }
172 metadata_ids
173 }
174}
175
176impl From<Vec<MosaicId>> for MetadataIds {
177 #[inline]
178 fn from(ids: Vec<MosaicId>) -> Self {
179 let mut metadata_ids = MetadataIds::default();
180
181 let mosaic_ids: Vec<String> = ids.into_iter().map(|id| id.to_hex()).collect();
182
183 if !mosaic_ids.is_empty() {
184 metadata_ids.ids = Some(mosaic_ids);
185 }
186 metadata_ids
187 }
188}
189
190impl From<Vec<NamespaceId>> for MetadataIds {
191 #[inline]
192 fn from(ids: Vec<NamespaceId>) -> Self {
193 let mut metadata_ids = MetadataIds::default();
194
195 let namespace_ids: Vec<String> = ids.into_iter().map(|id| id.to_hex()).collect();
196
197 if !namespace_ids.is_empty() {
198 metadata_ids.ids = Some(namespace_ids);
199 }
200
201 metadata_ids
202 }
203}
204
205#[derive(Debug, Clone, Deserialize, Serialize)]
206pub struct MetadataModification {
207 pub r#type: MetadataModificationType,
208 pub key: String,
209 pub value: String,
210}
211
212impl MetadataModification {
213 pub fn new(modification_type: MetadataModificationType, key: &str, value: &str) -> Self {
214 Self { r#type: modification_type, key: key.to_string(), value: value.to_string() }
215 }
216
217 pub fn size(&self) -> usize {
218 SIZE_SIZE
219 + 1 + 1 + 2 + self.key.len()
223 + self.value.len()
224 }
225}
226
227impl fmt::Display for MetadataModification {
228 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
229 write!(f, "{}", serde_json::to_string_pretty(&self).unwrap_or_default())
230 }
231}