catbuffer_rust/mosaic_properties_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::block_duration_dto::*;
23use super::generator_utils::*;
24use super::mosaic_flags_dto::*;
25
26/// Binary layout for mosaic properties.
27#[derive(Debug, Clone)]
28pub struct MosaicPropertiesBuilder {
29 /// Mosaic flags.
30 flags: Vec<MosaicFlagsDto>,
31 /// Mosaic divisibility.
32 divisibility: u8,
33 /// Mosaic duration.
34 duration: BlockDurationDto,
35}
36
37
38impl MosaicPropertiesBuilder {
39 /// Creates an instance of MosaicPropertiesBuilder from binary payload.
40 /// payload: Byte payload to use to serialize the object.
41 /// # Returns
42 /// A MosaicPropertiesBuilder.
43 pub fn from_binary(_bytes: &[u8]) -> Self {
44 let flags = MosaicFlagsDto::bytes_to_flags(&_bytes[..1]); // kind:FLAGS
45 let mut _bytes = (&_bytes[1..]).to_vec();
46 let buf = fixed_bytes::<1>(&_bytes);
47 let divisibility = u8::from_le_bytes(buf); // kind:SIMPLE
48 let _bytes = (&_bytes[1..]).to_vec();
49 let duration = BlockDurationDto::from_binary(&_bytes); // kind:CUSTOM1
50 let mut _bytes = _bytes[duration.get_size()..].to_vec();
51 MosaicPropertiesBuilder { flags, divisibility, duration }
52 }
53
54 /// Gets mosaic flags.
55 ///
56 /// # Returns
57 /// A Mosaic flags.
58 pub fn get_flags(&self) -> Vec<MosaicFlagsDto> {
59 self.flags.clone()
60 }
61
62 /// Gets mosaic divisibility.
63 ///
64 /// # Returns
65 /// A Mosaic divisibility.
66 pub fn get_divisibility(&self) -> u8 {
67 self.divisibility.clone()
68 }
69
70 /// Gets mosaic duration.
71 ///
72 /// # Returns
73 /// A Mosaic duration.
74 pub fn get_duration(&self) -> BlockDurationDto {
75 self.duration.clone()
76 }
77
78 /// Gets the size of the type.
79 ///
80 /// Returns:
81 /// A size in bytes.
82 pub fn get_size(&self) -> usize {
83 let mut size = 0;
84 size += 1; // flags;
85 size += 1; // divisibility;
86 size += self.duration.get_size(); // duration;
87 size
88 }
89
90 /// Serializes self to bytes.
91 ///
92 /// # Returns
93 /// A Serialized bytes.
94 pub fn serializer(&self) -> Vec<u8> {
95 let mut buf: Vec<u8> = vec![];
96 buf.append(&mut MosaicFlagsDto::flags_to_int(self.get_flags()).to_le_bytes().to_vec()); // kind:FLAGS
97 buf.append(&mut self.get_divisibility().to_le_bytes().to_vec()); // kind:SIMPLE
98 buf.append(&mut self.duration.serializer()); // kind:CUSTOM
99 buf
100 }
101}
102