catbuffer_rust/
mosaic_definition_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::address_dto::*;
23use super::generator_utils::*;
24use super::height_dto::*;
25use super::mosaic_properties_builder::*;
26
27/// Binary layout for mosaic definition.
28#[derive(Debug, Clone)]
29pub struct MosaicDefinitionBuilder {
30    /// Block height.
31    start_height: HeightDto,
32    /// Mosaic owner.
33    owner_address: AddressDto,
34    /// Revision.
35    revision: u32,
36    /// Properties.
37    properties: MosaicPropertiesBuilder,
38}
39
40
41impl MosaicDefinitionBuilder {
42    /// Creates an instance of MosaicDefinitionBuilder from binary payload.
43    /// payload: Byte payload to use to serialize the object.
44    /// # Returns
45    /// A MosaicDefinitionBuilder.
46    pub fn from_binary(_bytes: &[u8]) -> Self {
47        let start_height = HeightDto::from_binary(&_bytes); // kind:CUSTOM1
48        let mut _bytes = _bytes[start_height.get_size()..].to_vec();
49        let owner_address = AddressDto::from_binary(&_bytes); // kind:CUSTOM1
50        let mut _bytes = _bytes[owner_address.get_size()..].to_vec();
51        let buf = fixed_bytes::<4>(&_bytes);
52        let revision = u32::from_le_bytes(buf); // kind:SIMPLE
53        let _bytes = (&_bytes[4..]).to_vec();
54        let properties = MosaicPropertiesBuilder::from_binary(&_bytes); // kind:CUSTOM1
55        let mut _bytes = _bytes[properties.get_size()..].to_vec();
56        MosaicDefinitionBuilder { start_height, owner_address, revision, properties }
57    }
58
59    /// Gets block height.
60    ///
61    /// # Returns
62    /// A Block height.
63    pub fn get_start_height(&self) -> HeightDto {
64        self.start_height.clone()
65    }
66
67    /// Gets mosaic owner.
68    ///
69    /// # Returns
70    /// A Mosaic owner.
71    pub fn get_owner_address(&self) -> AddressDto {
72        self.owner_address.clone()
73    }
74
75    /// Gets revision.
76    ///
77    /// # Returns
78    /// A Revision.
79    pub fn get_revision(&self) -> u32 {
80        self.revision.clone()
81    }
82
83    /// Gets properties.
84    ///
85    /// # Returns
86    /// A Properties.
87    pub fn get_properties(&self) -> MosaicPropertiesBuilder {
88        self.properties.clone()
89    }
90
91    /// Gets the size of the type.
92    ///
93    /// Returns:
94    /// A size in bytes.
95    pub fn get_size(&self) -> usize {
96        let mut size = 0;
97        size += self.start_height.get_size(); // start_height;
98        size += self.owner_address.get_size(); // owner_address;
99        size += 4; // revision;
100        size += self.properties.get_size(); // properties;
101        size
102    }
103
104    /// Serializes self to bytes.
105    ///
106    /// # Returns
107    /// A Serialized bytes.
108    pub fn serializer(&self) -> Vec<u8> {
109        let mut buf: Vec<u8> = vec![];
110        buf.append(&mut self.start_height.serializer()); // kind:CUSTOM
111        buf.append(&mut self.owner_address.serializer()); // kind:CUSTOM
112        buf.append(&mut self.get_revision().to_le_bytes().to_vec()); // kind:SIMPLE
113        buf.append(&mut self.properties.serializer()); // kind:CUSTOM
114        buf
115    }
116}
117