catbuffer_rust/mosaic_entry_builder.rs
1/*
2 * // Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp.
3 * // Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie.
4 * // All rights reserved.
5 * //
6 * // This file is part of Catapult.
7 * //
8 * // Catapult is free software: you can redistribute it and/or modify
9 * // it under the terms of the GNU Lesser General Public License as published by
10 * // the Free Software Foundation, either version 3 of the License, or
11 * // (at your option) any later version.
12 * //
13 * // Catapult is distributed in the hope that it will be useful,
14 * // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * // GNU Lesser General Public License for more details.
17 * //
18 * // You should have received a copy of the GNU Lesser General Public License
19 * // along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22use super::amount_dto::*;
23use super::generator_utils::*;
24use super::mosaic_definition_builder::*;
25use super::mosaic_id_dto::*;
26use super::state_header_builder::*;
27
28/// Binary layout for mosaic entry.
29#[derive(Debug, Clone)]
30pub struct MosaicEntryBuilder {
31 /// State header.
32 super_object: StateHeaderBuilder,
33 /// Entry id.
34 mosaic_id: MosaicIdDto,
35 /// Total supply amount.
36 supply: AmountDto,
37 /// Definition comprised of entry properties.
38 definition: MosaicDefinitionBuilder,
39}
40
41
42impl MosaicEntryBuilder {
43 /// Creates an instance of MosaicEntryBuilder from binary payload.
44 /// payload: Byte payload to use to serialize the object.
45 /// # Returns
46 /// A MosaicEntryBuilder.
47 pub fn from_binary(_bytes: &[u8]) -> Self {
48 let super_object = StateHeaderBuilder::from_binary(_bytes);
49 let mut _bytes = _bytes[super_object.get_size()..].to_vec();
50 let mosaic_id = MosaicIdDto::from_binary(&_bytes); // kind:CUSTOM1
51 let mut _bytes = _bytes[mosaic_id.get_size()..].to_vec();
52 let supply = AmountDto::from_binary(&_bytes); // kind:CUSTOM1
53 let mut _bytes = _bytes[supply.get_size()..].to_vec();
54 let definition = MosaicDefinitionBuilder::from_binary(&_bytes); // kind:CUSTOM1
55 let mut _bytes = _bytes[definition.get_size()..].to_vec();
56 MosaicEntryBuilder { super_object, mosaic_id, supply, definition }
57 }
58
59 /// Gets entry id.
60 ///
61 /// # Returns
62 /// A Entry id.
63 pub fn get_mosaic_id(&self) -> MosaicIdDto {
64 self.mosaic_id.clone()
65 }
66
67 /// Gets total supply amount.
68 ///
69 /// # Returns
70 /// A Total supply amount.
71 pub fn get_supply(&self) -> AmountDto {
72 self.supply.clone()
73 }
74
75 /// Gets definition comprised of entry properties.
76 ///
77 /// # Returns
78 /// A Definition comprised of entry properties.
79 pub fn get_definition(&self) -> MosaicDefinitionBuilder {
80 self.definition.clone()
81 }
82
83 /// Gets the size of the type.
84 ///
85 /// Returns:
86 /// A size in bytes.
87 pub fn get_size(&self) -> usize {
88 let mut size = self.super_object.get_size();
89 size += self.mosaic_id.get_size(); // mosaic_id;
90 size += self.supply.get_size(); // supply;
91 size += self.definition.get_size(); // definition;
92 size
93 }
94
95 /// Serializes self to bytes.
96 ///
97 /// # Returns
98 /// A Serialized bytes.
99 pub fn serializer(&self) -> Vec<u8> {
100 let mut buf: Vec<u8> = vec![];
101 buf.append(&mut self.super_object.serializer());
102 buf.append(&mut self.mosaic_id.serializer()); // kind:CUSTOM
103 buf.append(&mut self.supply.serializer()); // kind:CUSTOM
104 buf.append(&mut self.definition.serializer()); // kind:CUSTOM
105 buf
106 }
107}
108