catbuffer_rust/
mosaic_restriction_entry_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_address_restriction_entry_builder::*;
24use super::mosaic_global_restriction_entry_builder::*;
25use super::mosaic_restriction_entry_type_dto::*;
26use super::state_header_builder::*;
27
28/// Binary layout for a mosaic restriction.
29#[derive(Debug, Clone)]
30pub struct MosaicRestrictionEntryBuilder {
31    /// State header.
32    super_object: StateHeaderBuilder,
33    /// Type of restriction being placed upon the entity.
34    entry_type: MosaicRestrictionEntryTypeDto,
35    /// Address restriction rule.
36    address_entry: Option<MosaicAddressRestrictionEntryBuilder>,
37    /// Global mosaic rule.
38    global_entry: Option<MosaicGlobalRestrictionEntryBuilder>,
39}
40
41
42impl MosaicRestrictionEntryBuilder {
43    /// Creates an instance of MosaicRestrictionEntryBuilder from binary payload.
44    /// payload: Byte payload to use to serialize the object.
45    /// # Returns
46    /// A MosaicRestrictionEntryBuilder.
47    pub fn from_binary(_bytes: &[u8]) -> Self {
48        let super_object = StateHeaderBuilder::from_binary(_bytes);
49        let mut _bytes = _bytes[super_object.get_size()..].to_vec();
50        let entry_type = MosaicRestrictionEntryTypeDto::from_binary(&_bytes); // kind:CUSTOM2
51        let mut _bytes = _bytes[entry_type.get_size()..].to_vec();
52        let mut address_entry = None;
53        if entry_type == MosaicRestrictionEntryTypeDto::ADDRESS {
54            let raw_address_entry = MosaicAddressRestrictionEntryBuilder::from_binary(&_bytes);
55            _bytes = (&_bytes[raw_address_entry.get_size()..]).to_vec();
56            address_entry = Some(raw_address_entry); // kind:CUSTOM1
57        }
58        let mut global_entry = None;
59        if entry_type == MosaicRestrictionEntryTypeDto::GLOBAL {
60            let raw_global_entry = MosaicGlobalRestrictionEntryBuilder::from_binary(&_bytes);
61            _bytes = (&_bytes[raw_global_entry.get_size()..]).to_vec();
62            global_entry = Some(raw_global_entry); // kind:CUSTOM1
63        }
64        MosaicRestrictionEntryBuilder { super_object, entry_type, address_entry, global_entry }
65    }
66
67    /// Gets type of restriction being placed upon the entity.
68    ///
69    /// # Returns
70    /// A Type of restriction being placed upon the entity.
71    pub fn get_entry_type(&self) -> MosaicRestrictionEntryTypeDto {
72        self.entry_type.clone()
73    }
74
75    /// Gets address restriction rule.
76    ///
77    /// # Returns
78    /// A Address restriction rule.
79    pub fn get_address_entry(&self) -> Option<MosaicAddressRestrictionEntryBuilder> {
80        if self.entry_type != MosaicRestrictionEntryTypeDto::ADDRESS {
81            panic!("entryType is not set to ADDRESS.")
82        };
83        self.address_entry.clone()
84    }
85
86    /// Gets global mosaic rule.
87    ///
88    /// # Returns
89    /// A Global mosaic rule.
90    pub fn get_global_entry(&self) -> Option<MosaicGlobalRestrictionEntryBuilder> {
91        if self.entry_type != MosaicRestrictionEntryTypeDto::GLOBAL {
92            panic!("entryType is not set to GLOBAL.")
93        };
94        self.global_entry.clone()
95    }
96
97    /// Gets the size of the type.
98    ///
99    /// Returns:
100    /// A size in bytes.
101    pub fn get_size(&self) -> usize {
102        let mut size = self.super_object.get_size();
103        size += self.entry_type.get_size(); // entry_type;
104        if self.entry_type == MosaicRestrictionEntryTypeDto::ADDRESS {
105            size += self.address_entry.as_ref().unwrap().get_size(); // address_entry
106        }
107        if self.entry_type == MosaicRestrictionEntryTypeDto::GLOBAL {
108            size += self.global_entry.as_ref().unwrap().get_size(); // global_entry
109        }
110        size
111    }
112
113    /// Serializes self to bytes.
114    ///
115    /// # Returns
116    /// A Serialized bytes.
117    pub fn serializer(&self) -> Vec<u8> {
118        let mut buf: Vec<u8> = vec![];
119        buf.append(&mut self.super_object.serializer());
120        buf.append(&mut self.entry_type.serializer()); // kind:CUSTOM
121        if self.entry_type == MosaicRestrictionEntryTypeDto::ADDRESS {
122            buf.append(&mut self.address_entry.as_ref().unwrap().serializer()); // kind:CUSTOM
123        };
124        if self.entry_type == MosaicRestrictionEntryTypeDto::GLOBAL {
125            buf.append(&mut self.global_entry.as_ref().unwrap().serializer()); // kind:CUSTOM
126        };
127        buf
128    }
129}
130