catbuffer_rust/
account_restrictions_info_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_address_value_builder::*;
23use super::account_restriction_flags_dto::*;
24use super::account_restriction_mosaic_value_builder::*;
25use super::account_restriction_transaction_type_value_builder::*;
26use super::generator_utils::*;
27
28/// Binary layout for account restrictions.
29#[derive(Debug, Clone)]
30pub struct AccountRestrictionsInfoBuilder {
31    /// Raw restriction flags.
32    restriction_flags: Vec<AccountRestrictionFlagsDto>,
33    /// Address restrictions.
34    address_restrictions: Option<AccountRestrictionAddressValueBuilder>,
35    /// Mosaic identifier restrictions.
36    mosaic_id_restrictions: Option<AccountRestrictionMosaicValueBuilder>,
37    /// Transaction type restrictions.
38    transaction_type_restrictions: Option<AccountRestrictionTransactionTypeValueBuilder>,
39}
40
41
42impl AccountRestrictionsInfoBuilder {
43    /// Creates an instance of AccountRestrictionsInfoBuilder from binary payload.
44    /// payload: Byte payload to use to serialize the object.
45    /// # Returns
46    /// A AccountRestrictionsInfoBuilder.
47    pub fn from_binary(_bytes: &[u8]) -> Self {
48        let restriction_flags = AccountRestrictionFlagsDto::bytes_to_flags(&_bytes[..2]); // kind:FLAGS
49        let mut _bytes = (&_bytes[2..]).to_vec();
50        let mut address_restrictions = None;
51        if restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::ADDRESS) {
52            let raw_address_restrictions = AccountRestrictionAddressValueBuilder::from_binary(&_bytes);
53            _bytes = (&_bytes[raw_address_restrictions.get_size()..]).to_vec();
54            address_restrictions = Some(raw_address_restrictions); // kind:CUSTOM1
55        }
56        let mut mosaic_id_restrictions = None;
57        if restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::MOSAIC_ID) {
58            let raw_mosaic_id_restrictions = AccountRestrictionMosaicValueBuilder::from_binary(&_bytes);
59            _bytes = (&_bytes[raw_mosaic_id_restrictions.get_size()..]).to_vec();
60            mosaic_id_restrictions = Some(raw_mosaic_id_restrictions); // kind:CUSTOM1
61        }
62        let mut transaction_type_restrictions = None;
63        if restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::TRANSACTION_TYPE) {
64            let raw_transaction_type_restrictions = AccountRestrictionTransactionTypeValueBuilder::from_binary(&_bytes);
65            _bytes = (&_bytes[raw_transaction_type_restrictions.get_size()..]).to_vec();
66            transaction_type_restrictions = Some(raw_transaction_type_restrictions); // kind:CUSTOM1
67        }
68        AccountRestrictionsInfoBuilder { restriction_flags, address_restrictions, mosaic_id_restrictions, transaction_type_restrictions }
69    }
70
71    /// Gets raw restriction flags.
72    ///
73    /// # Returns
74    /// A Raw restriction flags.
75    pub fn get_restriction_flags(&self) -> Vec<AccountRestrictionFlagsDto> {
76        self.restriction_flags.clone()
77    }
78
79    /// Gets address restrictions.
80    ///
81    /// # Returns
82    /// A Address restrictions.
83    pub fn get_address_restrictions(&self) -> Option<AccountRestrictionAddressValueBuilder> {
84        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::ADDRESS) {
85            panic!("restrictionFlags is not set to ADDRESS.")
86        };
87        self.address_restrictions.clone()
88    }
89
90    /// Gets mosaic identifier restrictions.
91    ///
92    /// # Returns
93    /// A Mosaic identifier restrictions.
94    pub fn get_mosaic_id_restrictions(&self) -> Option<AccountRestrictionMosaicValueBuilder> {
95        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::MOSAIC_ID) {
96            panic!("restrictionFlags is not set to MOSAIC_ID.")
97        };
98        self.mosaic_id_restrictions.clone()
99    }
100
101    /// Gets transaction type restrictions.
102    ///
103    /// # Returns
104    /// A Transaction type restrictions.
105    pub fn get_transaction_type_restrictions(&self) -> Option<AccountRestrictionTransactionTypeValueBuilder> {
106        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::TRANSACTION_TYPE) {
107            panic!("restrictionFlags is not set to TRANSACTION_TYPE.")
108        };
109        self.transaction_type_restrictions.clone()
110    }
111
112    /// Gets the size of the type.
113    ///
114    /// Returns:
115    /// A size in bytes.
116    pub fn get_size(&self) -> usize {
117        let mut size = 0;
118        size += 2; // restriction_flags;
119        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::ADDRESS) {
120            size += self.address_restrictions.as_ref().unwrap().get_size(); // address_restrictions
121        }
122        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::MOSAIC_ID) {
123            size += self.mosaic_id_restrictions.as_ref().unwrap().get_size(); // mosaic_id_restrictions
124        }
125        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::TRANSACTION_TYPE) {
126            size += self.transaction_type_restrictions.as_ref().unwrap().get_size(); // transaction_type_restrictions
127        }
128        size
129    }
130
131    /// Serializes self to bytes.
132    ///
133    /// # Returns
134    /// A Serialized bytes.
135    pub fn serializer(&self) -> Vec<u8> {
136        let mut buf: Vec<u8> = vec![];
137        buf.append(&mut AccountRestrictionFlagsDto::flags_to_int(self.get_restriction_flags()).to_le_bytes().to_vec()); // kind:FLAGS
138        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::ADDRESS) {
139            buf.append(&mut self.address_restrictions.as_ref().unwrap().serializer()); // kind:CUSTOM
140        };
141        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::MOSAIC_ID) {
142            buf.append(&mut self.mosaic_id_restrictions.as_ref().unwrap().serializer()); // kind:CUSTOM
143        };
144        if self.restriction_flags.iter().any(|&i| i == AccountRestrictionFlagsDto::TRANSACTION_TYPE) {
145            buf.append(&mut self.transaction_type_restrictions.as_ref().unwrap().serializer()); // kind:CUSTOM
146        };
147        buf
148    }
149}
150