catbuffer_rust/
account_restrictions_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_restrictions_info_builder::*;
23use super::address_dto::*;
24use super::generator_utils::*;
25use super::state_header_builder::*;
26
27/// Binary layout for account restrictions.
28#[derive(Debug, Clone)]
29pub struct AccountRestrictionsBuilder {
30    /// State header.
31    super_object: StateHeaderBuilder,
32    /// Address on which restrictions are placed.
33    address: AddressDto,
34    /// Account restrictions.
35    restrictions: Vec<AccountRestrictionsInfoBuilder>,
36}
37
38
39impl AccountRestrictionsBuilder {
40    /// Creates an instance of AccountRestrictionsBuilder from binary payload.
41    /// payload: Byte payload to use to serialize the object.
42    /// # Returns
43    /// A AccountRestrictionsBuilder.
44    pub fn from_binary(_bytes: &[u8]) -> Self {
45        let super_object = StateHeaderBuilder::from_binary(_bytes);
46        let mut _bytes = _bytes[super_object.get_size()..].to_vec();
47        let address = AddressDto::from_binary(&_bytes); // kind:CUSTOM1
48        let mut _bytes = _bytes[address.get_size()..].to_vec();
49        let buf = fixed_bytes::<8>(&_bytes);
50        let restrictionsCount = u64::from_le_bytes(buf); // kind:SIZE_FIELD
51        let mut _bytes = (&_bytes[8..]).to_vec();
52        let mut restrictions: Vec<AccountRestrictionsInfoBuilder> = vec![]; // kind:ARRAY
53        let mut _bytes = _bytes.to_vec();
54        for _ in 0..restrictionsCount {
55            let item = AccountRestrictionsInfoBuilder::from_binary(&_bytes);
56            restrictions.push(item.clone());
57            _bytes = (&_bytes[item.get_size()..]).to_vec();
58        }
59        AccountRestrictionsBuilder { super_object, address, restrictions }
60    }
61
62    /// Gets address on which restrictions are placed.
63    ///
64    /// # Returns
65    /// A Address on which restrictions are placed.
66    pub fn get_address(&self) -> AddressDto {
67        self.address.clone()
68    }
69
70    /// Gets account restrictions.
71    ///
72    /// # Returns
73    /// A Account restrictions.
74    pub fn get_restrictions(&self) -> Vec<AccountRestrictionsInfoBuilder> {
75        self.restrictions.clone() // ARRAY or FILL_ARRAY
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 = self.super_object.get_size();
84        size += self.address.get_size(); // address;
85        size += 8; // restrictions_count;
86        size += self.restrictions.iter().map(|item| item.get_size()).sum::<usize>(); // array or fill_array;
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 self.super_object.serializer());
97        buf.append(&mut self.address.serializer()); // kind:CUSTOM
98        buf.append(&mut (self.get_restrictions().len() as u64).to_le_bytes().to_vec()); // kind:SIZE_FIELD
99        for i in &self.restrictions {
100            buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
101        }
102        buf
103    }
104}
105