catbuffer_rust/account_restriction_address_value_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::address_dto::*;
23use super::generator_utils::*;
24
25/// Binary layout for address based account restriction.
26#[derive(Debug, Clone)]
27pub struct AccountRestrictionAddressValueBuilder {
28 /// Restriction values.
29 restriction_values: Vec<AddressDto>,
30}
31
32
33impl AccountRestrictionAddressValueBuilder {
34 /// Creates an instance of AccountRestrictionAddressValueBuilder from binary payload.
35 /// payload: Byte payload to use to serialize the object.
36 /// # Returns
37 /// A AccountRestrictionAddressValueBuilder.
38 pub fn from_binary(_bytes: &[u8]) -> Self {
39 let buf = fixed_bytes::<8>(&_bytes);
40 let restrictionValuesCount = u64::from_le_bytes(buf); // kind:SIZE_FIELD
41 let mut _bytes = (&_bytes[8..]).to_vec();
42 let mut restriction_values: Vec<AddressDto> = vec![]; // kind:ARRAY
43 let mut _bytes = _bytes.to_vec();
44 for _ in 0..restrictionValuesCount {
45 let item = AddressDto::from_binary(&_bytes);
46 restriction_values.push(item.clone());
47 _bytes = (&_bytes[item.get_size()..]).to_vec();
48 }
49 AccountRestrictionAddressValueBuilder { restriction_values }
50 }
51
52 /// Gets restriction values.
53 ///
54 /// # Returns
55 /// A Restriction values.
56 pub fn get_restriction_values(&self) -> Vec<AddressDto> {
57 self.restriction_values.clone() // ARRAY or FILL_ARRAY
58 }
59
60 /// Gets the size of the type.
61 ///
62 /// Returns:
63 /// A size in bytes.
64 pub fn get_size(&self) -> usize {
65 let mut size = 0;
66 size += 8; // restriction_values_count;
67 size += self.restriction_values.iter().map(|item| item.get_size()).sum::<usize>(); // array or fill_array;
68 size
69 }
70
71 /// Serializes self to bytes.
72 ///
73 /// # Returns
74 /// A Serialized bytes.
75 pub fn serializer(&self) -> Vec<u8> {
76 let mut buf: Vec<u8> = vec![];
77 buf.append(&mut (self.get_restriction_values().len() as u64).to_le_bytes().to_vec()); // kind:SIZE_FIELD
78 for i in &self.restriction_values {
79 buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
80 }
81 buf
82 }
83}
84