catbuffer_rust/
mosaic_address_restriction_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::generator_utils::*;
23use super::unresolved_address_dto::*;
24use super::unresolved_mosaic_id_dto::*;
25
26/// Binary layout for a mosaic address restriction transaction.
27#[derive(Debug, Clone)]
28pub struct MosaicAddressRestrictionTransactionBodyBuilder {
29    /// Identifier of the mosaic to which the restriction applies.
30    pub mosaic_id: UnresolvedMosaicIdDto,
31    /// Restriction key.
32    pub restriction_key: u64,
33    /// Previous restriction value.
34    pub previous_restriction_value: u64,
35    /// New restriction value.
36    pub new_restriction_value: u64,
37    /// Address being restricted.
38    pub target_address: UnresolvedAddressDto,
39}
40
41impl MosaicAddressRestrictionTransactionBodyBuilder {
42    /// Creates an instance of MosaicAddressRestrictionTransactionBodyBuilder from binary payload.
43    /// payload: Byte payload to use to serialize the object.
44    /// # Returns
45    /// A MosaicAddressRestrictionTransactionBodyBuilder.
46    pub fn from_binary(payload: &[u8]) -> Self {
47        let mut _bytes = payload.to_vec();
48        let mosaic_id = UnresolvedMosaicIdDto::from_binary(&_bytes); // kind:CUSTOM1
49        _bytes = _bytes[mosaic_id.get_size()..].to_vec();
50        let buf = fixed_bytes::<8>(&_bytes);
51        let restriction_key = u64::from_le_bytes(buf); // kind:SIMPLE
52        _bytes = (&_bytes[8..]).to_vec();
53        let buf = fixed_bytes::<8>(&_bytes);
54        let previous_restriction_value = u64::from_le_bytes(buf); // kind:SIMPLE
55        _bytes = (&_bytes[8..]).to_vec();
56        let buf = fixed_bytes::<8>(&_bytes);
57        let new_restriction_value = u64::from_le_bytes(buf); // kind:SIMPLE
58        _bytes = (&_bytes[8..]).to_vec();
59        let target_address = UnresolvedAddressDto::from_binary(&_bytes); // kind:CUSTOM1
60        _bytes = _bytes[target_address.get_size()..].to_vec();
61        // create object and call.
62        MosaicAddressRestrictionTransactionBodyBuilder { mosaic_id, restriction_key, previous_restriction_value, new_restriction_value, target_address } // 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.mosaic_id.get_size(); // mosaic_id_size;
72        size += 8;  // restriction_key;
73        size += 8;  // previous_restriction_value;
74        size += 8;  // new_restriction_value;
75        size += self.target_address.get_size(); // target_address_size;
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.mosaic_id.serializer()); // kind:CUSTOM
86        buf.append(&mut self.restriction_key.to_le_bytes().to_vec()); // serial_kind:SIMPLE
87        buf.append(&mut self.previous_restriction_value.to_le_bytes().to_vec()); // serial_kind:SIMPLE
88        buf.append(&mut self.new_restriction_value.to_le_bytes().to_vec()); // serial_kind:SIMPLE
89        buf.append(&mut self.target_address.serializer()); // kind:CUSTOM
90        buf
91    }
92}
93