catbuffer_rust/
account_operation_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::account_restriction_flags_dto::*;
23use super::entity_type_dto::*;
24use super::generator_utils::*;
25
26/// Binary layout for an account operation restriction transaction.
27#[derive(Debug, Clone)]
28pub struct AccountOperationRestrictionTransactionBodyBuilder {
29    /// Account restriction flags.
30    pub restriction_flags: Vec<AccountRestrictionFlagsDto>,
31    /// Account restriction additions.
32    pub restriction_additions: Vec<EntityTypeDto>,
33    /// Account restriction deletions.
34    pub restriction_deletions: Vec<EntityTypeDto>,
35}
36
37impl AccountOperationRestrictionTransactionBodyBuilder {
38    /// Creates an instance of AccountOperationRestrictionTransactionBodyBuilder from binary payload.
39    /// payload: Byte payload to use to serialize the object.
40    /// # Returns
41    /// A AccountOperationRestrictionTransactionBodyBuilder.
42    pub fn from_binary(payload: &[u8]) -> Self {
43        let mut _bytes = payload.to_vec();
44        let restriction_flags = AccountRestrictionFlagsDto::bytes_to_flags(&_bytes[..2]); // kind:FLAGS
45        let mut _bytes = (&_bytes[2..]).to_vec();
46        let buf = fixed_bytes::<1>(&_bytes);
47        let restriction_additions_count = u8::from_le_bytes(buf); // kind:SIZE_FIELD
48        _bytes = (&_bytes[1..]).to_vec();
49        let buf = fixed_bytes::<1>(&_bytes);
50        let restriction_deletions_count = u8::from_le_bytes(buf); // kind:SIZE_FIELD
51        _bytes = (&_bytes[1..]).to_vec();
52        let buf = fixed_bytes::<4>(&_bytes);
53        let _ = u32::from_le_bytes(buf); // kind:SIMPLE
54        _bytes = (&_bytes[4..]).to_vec();
55        let mut restriction_additions: Vec<EntityTypeDto> = vec![]; // kind:ARRAY
56        for _ in 0..restriction_additions_count {
57            let item = EntityTypeDto::from_binary(&_bytes);
58            restriction_additions.push(item.clone());
59            _bytes = (&_bytes[item.get_size()..]).to_vec();
60        }
61        let mut restriction_deletions: Vec<EntityTypeDto> = vec![]; // kind:ARRAY
62        for _ in 0..restriction_deletions_count {
63            let item = EntityTypeDto::from_binary(&_bytes);
64            restriction_deletions.push(item.clone());
65            _bytes = (&_bytes[item.get_size()..]).to_vec();
66        }
67        // create object and call.
68        AccountOperationRestrictionTransactionBodyBuilder { restriction_flags, restriction_additions, restriction_deletions } // TransactionBody
69    }
70
71    /// Gets the size of the type.
72    ///
73    /// Returns:
74    /// A size in bytes.
75    pub fn get_size(&self) -> usize {
76        let mut size = 0;
77        size += 2;  // restriction_flags;
78        size += 1;  // restriction_additions_count;
79        size += 1;  // restriction_deletions_count;
80        size += 4;  // account_restriction_transaction_body__reserved1;
81        for i in &self.restriction_additions {
82            size += i.get_size(); // FILL_ARRAY
83        };
84        for i in &self.restriction_deletions {
85            size += i.get_size(); // FILL_ARRAY
86        };
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 AccountRestrictionFlagsDto::flags_to_int(self.restriction_flags.clone()).to_le_bytes().to_vec()); // kind:FLAGS
97        let size_value: u8 = self.restriction_additions.len() as u8;
98        buf.append(&mut size_value.to_le_bytes().to_vec()); // kind:SIZE_FIELD
99        let size_value: u8 = self.restriction_deletions.len() as u8;
100        buf.append(&mut size_value.to_le_bytes().to_vec()); // kind:SIZE_FIELD
101        buf.append(&mut [0u8; 4].to_vec()); // kind:SIMPLE and is_reserved
102        for i in &self.restriction_additions {
103            buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
104        }
105        for i in &self.restriction_deletions {
106            buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
107        }
108        buf
109    }
110}
111