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