catbuffer_rust/
namespace_registration_transaction_body_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::block_duration_dto::*;
23use super::generator_utils::*;
24use super::namespace_id_dto::*;
25use super::namespace_registration_type_dto::*;
26
27/// Binary layout for a namespace registration transaction.
28#[derive(Debug, Clone)]
29pub struct NamespaceRegistrationTransactionBodyBuilder {
30    /// Namespace duration.
31    pub duration: Option<BlockDurationDto>,
32    /// Parent namespace identifier.
33    pub parent_id: Option<NamespaceIdDto>,
34    /// Namespace identifier.
35    pub id: NamespaceIdDto,
36    /// Namespace registration type.
37    pub registration_type: NamespaceRegistrationTypeDto,
38    /// Namespace name.
39    pub name: Vec<u8>,
40}
41
42impl NamespaceRegistrationTransactionBodyBuilder {
43    /// Creates an instance of NamespaceRegistrationTransactionBodyBuilder from binary payload.
44    /// payload: Byte payload to use to serialize the object.
45    /// # Returns
46    /// A NamespaceRegistrationTransactionBodyBuilder.
47    pub fn from_binary(payload: &[u8]) -> Self {
48        let mut _bytes = payload.to_vec();
49        let registration_type_condition = _bytes[0..8].to_vec();
50        _bytes = (&_bytes[8..]).to_vec();
51        let id = NamespaceIdDto::from_binary(&_bytes); // kind:CUSTOM1
52        _bytes = _bytes[id.get_size()..].to_vec();
53        let registration_type = NamespaceRegistrationTypeDto::from_binary(&_bytes); // kind:CUSTOM2
54        _bytes = (&_bytes[registration_type.get_size()..]).to_vec();
55        let buf = fixed_bytes::<1>(&_bytes);
56        let name_size = u8::from_le_bytes(buf); // kind:SIZE_FIELD
57        _bytes = (&_bytes[1..]).to_vec();
58        let name = (&_bytes[..name_size as usize]).to_vec(); // kind:BUFFER
59        _bytes = (&_bytes[name_size as usize..]).to_vec();
60        let mut duration = None;
61        if registration_type == NamespaceRegistrationTypeDto::ROOT {
62            duration = Some(BlockDurationDto::from_binary(&registration_type_condition)); // kind:CUSTOM3
63        }
64        let mut parent_id = None;
65        if registration_type == NamespaceRegistrationTypeDto::CHILD {
66            parent_id = Some(NamespaceIdDto::from_binary(&registration_type_condition)); // kind:CUSTOM3
67        }
68        // create object and call.
69        NamespaceRegistrationTransactionBodyBuilder { duration, parent_id, id, registration_type, name } // TransactionBody
70    }
71
72    /// Gets the size of the type.
73    ///
74    /// Returns:
75    /// A size in bytes.
76    pub fn get_size(&self) -> usize {
77        let mut size = 0;
78        if self.registration_type == NamespaceRegistrationTypeDto::ROOT {
79            size += self.duration.as_ref().unwrap().get_size() // Conditional;
80        }
81        if self.registration_type == NamespaceRegistrationTypeDto::CHILD {
82            size += self.parent_id.as_ref().unwrap().get_size() // Conditional;
83        }
84        size += self.id.get_size(); // id_size;
85        size += self.registration_type.get_size(); // registration_type_size;
86        size += 1;  // name_size;
87        size += self.name.len();
88        size
89    }
90
91    /// Serializes self to bytes.
92    ///
93    /// # Returns
94    /// A Serialized bytes.
95    pub fn serializer(&self) -> Vec<u8> {
96        let mut buf: Vec<u8> = vec![];
97        if self.registration_type == NamespaceRegistrationTypeDto::ROOT {
98            buf.append(&mut self.duration.as_ref().unwrap().serializer()); // kind:CUSTOM
99        }
100        if self.registration_type == NamespaceRegistrationTypeDto::CHILD {
101            buf.append(&mut self.parent_id.as_ref().unwrap().serializer()); // kind:CUSTOM
102        }
103        buf.append(&mut self.id.serializer()); // kind:CUSTOM
104        buf.append(&mut self.registration_type.serializer()); // kind:CUSTOM
105        let size_value: u8 = self.name.len() as u8;
106        buf.append(&mut size_value.to_le_bytes().to_vec()); // kind:SIZE_FIELD
107        buf.append(&mut self.name.clone()); // kind:BUFFER
108        buf
109    }
110}
111