catbuffer_rust/
mosaic_global_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::mosaic_restriction_type_dto::*;
24use super::unresolved_mosaic_id_dto::*;
25
26/// Binary layout for a mosaic global restriction transaction.
27#[derive(Debug, Clone)]
28pub struct MosaicGlobalRestrictionTransactionBodyBuilder {
29    /// Identifier of the mosaic being restricted.
30    pub mosaic_id: UnresolvedMosaicIdDto,
31    /// Identifier of the mosaic providing the restriction key.
32    pub reference_mosaic_id: UnresolvedMosaicIdDto,
33    /// Restriction key relative to the reference mosaic identifier.
34    pub restriction_key: u64,
35    /// Previous restriction value.
36    pub previous_restriction_value: u64,
37    /// New restriction value.
38    pub new_restriction_value: u64,
39    /// Previous restriction type.
40    pub previous_restriction_type: MosaicRestrictionTypeDto,
41    /// New restriction type.
42    pub new_restriction_type: MosaicRestrictionTypeDto,
43}
44
45impl MosaicGlobalRestrictionTransactionBodyBuilder {
46    /// Creates an instance of MosaicGlobalRestrictionTransactionBodyBuilder from binary payload.
47    /// payload: Byte payload to use to serialize the object.
48    /// # Returns
49    /// A MosaicGlobalRestrictionTransactionBodyBuilder.
50    pub fn from_binary(payload: &[u8]) -> Self {
51        let mut _bytes = payload.to_vec();
52        let mosaic_id = UnresolvedMosaicIdDto::from_binary(&_bytes); // kind:CUSTOM1
53        _bytes = _bytes[mosaic_id.get_size()..].to_vec();
54        let reference_mosaic_id = UnresolvedMosaicIdDto::from_binary(&_bytes); // kind:CUSTOM1
55        _bytes = _bytes[reference_mosaic_id.get_size()..].to_vec();
56        let buf = fixed_bytes::<8>(&_bytes);
57        let restriction_key = u64::from_le_bytes(buf); // kind:SIMPLE
58        _bytes = (&_bytes[8..]).to_vec();
59        let buf = fixed_bytes::<8>(&_bytes);
60        let previous_restriction_value = u64::from_le_bytes(buf); // kind:SIMPLE
61        _bytes = (&_bytes[8..]).to_vec();
62        let buf = fixed_bytes::<8>(&_bytes);
63        let new_restriction_value = u64::from_le_bytes(buf); // kind:SIMPLE
64        _bytes = (&_bytes[8..]).to_vec();
65        let previous_restriction_type = MosaicRestrictionTypeDto::from_binary(&_bytes); // kind:CUSTOM2
66        _bytes = (&_bytes[previous_restriction_type.get_size()..]).to_vec();
67        let new_restriction_type = MosaicRestrictionTypeDto::from_binary(&_bytes); // kind:CUSTOM2
68        _bytes = (&_bytes[new_restriction_type.get_size()..]).to_vec();
69        // create object and call.
70        MosaicGlobalRestrictionTransactionBodyBuilder { mosaic_id, reference_mosaic_id, restriction_key, previous_restriction_value, new_restriction_value, previous_restriction_type, new_restriction_type } // TransactionBody
71    }
72
73    /// Gets the size of the type.
74    ///
75    /// Returns:
76    /// A size in bytes.
77    pub fn get_size(&self) -> usize {
78        let mut size = 0;
79        size += self.mosaic_id.get_size(); // mosaic_id_size;
80        size += self.reference_mosaic_id.get_size(); // reference_mosaic_id_size;
81        size += 8;  // restriction_key;
82        size += 8;  // previous_restriction_value;
83        size += 8;  // new_restriction_value;
84        size += self.previous_restriction_type.get_size(); // previous_restriction_type_size;
85        size += self.new_restriction_type.get_size(); // new_restriction_type_size;
86        size
87    }
88
89    /// Serializes self to bytes.
90    ///
91    /// # Returns
92    /// A Serialized bytes.
93    pub fn serializer(&self) -> Vec<u8> {
94        let mut buf: Vec<u8> = vec![];
95        buf.append(&mut self.mosaic_id.serializer()); // kind:CUSTOM
96        buf.append(&mut self.reference_mosaic_id.serializer()); // kind:CUSTOM
97        buf.append(&mut self.restriction_key.to_le_bytes().to_vec()); // serial_kind:SIMPLE
98        buf.append(&mut self.previous_restriction_value.to_le_bytes().to_vec()); // serial_kind:SIMPLE
99        buf.append(&mut self.new_restriction_value.to_le_bytes().to_vec()); // serial_kind:SIMPLE
100        buf.append(&mut self.previous_restriction_type.serializer()); // kind:CUSTOM
101        buf.append(&mut self.new_restriction_type.serializer()); // kind:CUSTOM
102        buf
103    }
104}
105