catbuffer_rust/
embedded_namespace_registration_transaction_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::embedded_transaction_builder::*;
24use super::embedded_transaction_helper::*;
25use super::entity_type_dto::*;
26use super::generator_utils::*;
27use super::key_dto::*;
28use super::namespace_id_dto::*;
29use super::namespace_registration_transaction_body_builder::*;
30use super::namespace_registration_type_dto::*;
31use super::network_type_dto::*;
32
33/// Binary layout for an embedded namespace registration transaction.
34#[derive(Debug, Clone)]
35pub struct EmbeddedNamespaceRegistrationTransactionBuilder {
36    /// Embedded transaction.
37    pub super_object: EmbeddedTransactionBuilder,
38    /// Namespace registration transaction body.
39    pub body: NamespaceRegistrationTransactionBodyBuilder,
40}
41
42impl EmbeddedNamespaceRegistrationTransactionBuilder {
43    const VERSION: u8 = 1;
44    const ENTITY_TYPE: u16 = 0x414e;
45
46
47    /// Creates an instance of EmbeddedNamespaceRegistrationTransactionBuilder from binary payload.
48    /// payload: Byte payload to use to serialize the object.
49    /// # Returns
50    /// A EmbeddedNamespaceRegistrationTransactionBuilder.
51    pub fn from_binary(payload: &[u8]) -> Self {
52        let mut _bytes = payload.to_vec();
53        let super_object = EmbeddedTransactionBuilder::from_binary(&_bytes);
54        assert_eq!(Self::VERSION, super_object.version, "Invalid entity version ({})", super_object.version);
55        assert_eq!(Self::ENTITY_TYPE, super_object._type.get_value(), "Invalid entity type ({:?})", super_object._type);
56        let mut _bytes = _bytes[super_object.get_size()..].to_vec();
57        let namespace_registration_transaction_body = NamespaceRegistrationTransactionBodyBuilder::from_binary(&_bytes); // kind:CUSTOM1
58        _bytes = _bytes[namespace_registration_transaction_body.get_size()..].to_vec();
59        // create object and call.
60        EmbeddedNamespaceRegistrationTransactionBuilder { super_object, body: namespace_registration_transaction_body }  // Transaction
61        // nothing needed to copy into EmbeddedTransaction
62    }
63
64
65    pub fn get_duration(&self) -> Option<BlockDurationDto> {
66        self.body.duration.clone()
67    }
68    pub fn set_duration(&mut self, duration: BlockDurationDto) {
69        self.body.duration = Some(duration);   // MARKER1 AttributeKind.CUSTOM
70    }
71
72
73    pub fn get_parent_id(&self) -> Option<NamespaceIdDto> {
74        self.body.parent_id.clone()
75    }
76    pub fn set_parent_id(&mut self, parent_id: NamespaceIdDto) {
77        self.body.parent_id = Some(parent_id);   // MARKER1 AttributeKind.CUSTOM
78    }
79
80
81    pub fn get_id(&self) -> NamespaceIdDto {
82        self.body.id.clone()
83    }
84    pub fn set_id(&mut self, id: NamespaceIdDto) {
85        self.body.id = id;   // MARKER1 AttributeKind.CUSTOM
86    }
87
88
89    pub fn get_registration_type(&self) -> NamespaceRegistrationTypeDto {
90        self.body.registration_type.clone()
91    }
92    pub fn set_registration_type(&mut self, registration_type: NamespaceRegistrationTypeDto) {
93        self.body.registration_type = registration_type;   // MARKER1 AttributeKind.CUSTOM
94    }
95
96
97    pub fn get_name(&self) -> Vec<u8> {
98        self.body.name.clone()
99    }
100    pub fn set_name(&mut self, name: Vec<u8>) {
101        self.body.name = name;   // MARKER1 AttributeKind.BUFFER
102    }
103
104    /// Gets the size of the type.
105    ///
106    /// Returns:
107    /// A size in bytes.
108    pub fn get_size(&self) -> usize {
109        let mut size = self.super_object.get_size();
110        size += self.body.get_size();
111        size
112    }
113
114    /// Serializes self to bytes.
115    ///
116    /// # Returns
117    /// A Serialized bytes.
118    pub fn serializer(&self) -> Vec<u8> {
119        let mut buf: Vec<u8> = vec![];
120        buf.append(&mut (self.get_size() as u32).to_le_bytes().to_vec());
121        buf.append(&mut self.super_object.serializer());
122        buf.append(&mut self.body.serializer()); // kind:CUSTOM TransactionBody
123        buf
124    }
125}
126
127impl EmbeddedTransactionHelper for EmbeddedNamespaceRegistrationTransactionBuilder {
128    fn box_clone(&self) -> Box<dyn EmbeddedTransactionHelper> {
129        Box::new((*self).clone())
130    }
131
132    fn get_size(&self) -> usize {
133        self.get_size()
134    }
135
136    fn serializer(&self) -> Vec<u8> {
137        self.serializer()
138    }
139}
140