catbuffer_rust/root_namespace_history_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::*;
24use super::namespace_alias_builder::*;
25use super::namespace_id_dto::*;
26use super::namespace_lifetime_builder::*;
27use super::namespace_path_builder::*;
28use super::state_header_builder::*;
29
30/// Binary layout for non-historical root namespace history.
31#[derive(Debug, Clone)]
32pub struct RootNamespaceHistoryBuilder {
33 /// State header.
34 super_object: StateHeaderBuilder,
35 /// Id of the root namespace history.
36 id: NamespaceIdDto,
37 /// Namespace owner address.
38 owner_address: AddressDto,
39 /// Lifetime in blocks.
40 lifetime: NamespaceLifetimeBuilder,
41 /// Root namespace alias.
42 root_alias: NamespaceAliasBuilder,
43 /// Save child sub-namespace paths.
44 paths: Vec<NamespacePathBuilder>,
45}
46
47
48impl RootNamespaceHistoryBuilder {
49 /// Creates an instance of RootNamespaceHistoryBuilder from binary payload.
50 /// payload: Byte payload to use to serialize the object.
51 /// # Returns
52 /// A RootNamespaceHistoryBuilder.
53 pub fn from_binary(_bytes: &[u8]) -> Self {
54 let super_object = StateHeaderBuilder::from_binary(_bytes);
55 let mut _bytes = _bytes[super_object.get_size()..].to_vec();
56 let id = NamespaceIdDto::from_binary(&_bytes); // kind:CUSTOM1
57 let mut _bytes = _bytes[id.get_size()..].to_vec();
58 let owner_address = AddressDto::from_binary(&_bytes); // kind:CUSTOM1
59 let mut _bytes = _bytes[owner_address.get_size()..].to_vec();
60 let lifetime = NamespaceLifetimeBuilder::from_binary(&_bytes); // kind:CUSTOM1
61 let mut _bytes = _bytes[lifetime.get_size()..].to_vec();
62 let root_alias = NamespaceAliasBuilder::from_binary(&_bytes); // kind:CUSTOM1
63 let mut _bytes = _bytes[root_alias.get_size()..].to_vec();
64 let buf = fixed_bytes::<8>(&_bytes);
65 let childrenCount = u64::from_le_bytes(buf); // kind:SIZE_FIELD
66 let mut _bytes = (&_bytes[8..]).to_vec();
67 let mut paths: Vec<NamespacePathBuilder> = vec![]; // kind:ARRAY
68 let mut _bytes = _bytes.to_vec();
69 for _ in 0..childrenCount {
70 let item = NamespacePathBuilder::from_binary(&_bytes);
71 paths.push(item.clone());
72 _bytes = (&_bytes[item.get_size()..]).to_vec();
73 }
74 RootNamespaceHistoryBuilder { super_object, id, owner_address, lifetime, root_alias, paths }
75 }
76
77 /// Gets id of the root namespace history.
78 ///
79 /// # Returns
80 /// A Id of the root namespace history.
81 pub fn get_id(&self) -> NamespaceIdDto {
82 self.id.clone()
83 }
84
85 /// Gets namespace owner address.
86 ///
87 /// # Returns
88 /// A Namespace owner address.
89 pub fn get_owner_address(&self) -> AddressDto {
90 self.owner_address.clone()
91 }
92
93 /// Gets lifetime in blocks.
94 ///
95 /// # Returns
96 /// A Lifetime in blocks.
97 pub fn get_lifetime(&self) -> NamespaceLifetimeBuilder {
98 self.lifetime.clone()
99 }
100
101 /// Gets root namespace alias.
102 ///
103 /// # Returns
104 /// A Root namespace alias.
105 pub fn get_root_alias(&self) -> NamespaceAliasBuilder {
106 self.root_alias.clone()
107 }
108
109 /// Gets save child sub-namespace paths.
110 ///
111 /// # Returns
112 /// A Save child sub-namespace paths.
113 pub fn get_paths(&self) -> Vec<NamespacePathBuilder> {
114 self.paths.clone() // ARRAY or FILL_ARRAY
115 }
116
117 /// Gets the size of the type.
118 ///
119 /// Returns:
120 /// A size in bytes.
121 pub fn get_size(&self) -> usize {
122 let mut size = self.super_object.get_size();
123 size += self.id.get_size(); // id;
124 size += self.owner_address.get_size(); // owner_address;
125 size += self.lifetime.get_size(); // lifetime;
126 size += self.root_alias.get_size(); // root_alias;
127 size += 8; // children_count;
128 size += self.paths.iter().map(|item| item.get_size()).sum::<usize>(); // array or fill_array;
129 size
130 }
131
132 /// Serializes self to bytes.
133 ///
134 /// # Returns
135 /// A Serialized bytes.
136 pub fn serializer(&self) -> Vec<u8> {
137 let mut buf: Vec<u8> = vec![];
138 buf.append(&mut self.super_object.serializer());
139 buf.append(&mut self.id.serializer()); // kind:CUSTOM
140 buf.append(&mut self.owner_address.serializer()); // kind:CUSTOM
141 buf.append(&mut self.lifetime.serializer()); // kind:CUSTOM
142 buf.append(&mut self.root_alias.serializer()); // kind:CUSTOM
143 buf.append(&mut (self.get_paths().len() as u64).to_le_bytes().to_vec()); // kind:SIZE_FIELD
144 for i in &self.paths {
145 buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
146 }
147 buf
148 }
149}
150