catbuffer_rust/
embedded_namespace_metadata_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::embedded_transaction_builder::*;
23use super::embedded_transaction_helper::*;
24use super::entity_type_dto::*;
25use super::generator_utils::*;
26use super::key_dto::*;
27use super::namespace_id_dto::*;
28use super::namespace_metadata_transaction_body_builder::*;
29use super::network_type_dto::*;
30use super::unresolved_address_dto::*;
31
32/// Binary layout for an embedded namespace metadata transaction.
33#[derive(Debug, Clone)]
34pub struct EmbeddedNamespaceMetadataTransactionBuilder {
35    /// Embedded transaction.
36    pub super_object: EmbeddedTransactionBuilder,
37    /// Namespace metadata transaction body.
38    pub body: NamespaceMetadataTransactionBodyBuilder,
39}
40
41impl EmbeddedNamespaceMetadataTransactionBuilder {
42    const VERSION: u8 = 1;
43    const ENTITY_TYPE: u16 = 0x4344;
44
45
46    /// Creates an instance of EmbeddedNamespaceMetadataTransactionBuilder from binary payload.
47    /// payload: Byte payload to use to serialize the object.
48    /// # Returns
49    /// A EmbeddedNamespaceMetadataTransactionBuilder.
50    pub fn from_binary(payload: &[u8]) -> Self {
51        let mut _bytes = payload.to_vec();
52        let super_object = EmbeddedTransactionBuilder::from_binary(&_bytes);
53        assert_eq!(Self::VERSION, super_object.version, "Invalid entity version ({})", super_object.version);
54        assert_eq!(Self::ENTITY_TYPE, super_object._type.get_value(), "Invalid entity type ({:?})", super_object._type);
55        let mut _bytes = _bytes[super_object.get_size()..].to_vec();
56        let namespace_metadata_transaction_body = NamespaceMetadataTransactionBodyBuilder::from_binary(&_bytes); // kind:CUSTOM1
57        _bytes = _bytes[namespace_metadata_transaction_body.get_size()..].to_vec();
58        // create object and call.
59        EmbeddedNamespaceMetadataTransactionBuilder { super_object, body: namespace_metadata_transaction_body }  // Transaction
60        // nothing needed to copy into EmbeddedTransaction
61    }
62
63
64    pub fn get_target_address(&self) -> UnresolvedAddressDto {
65        self.body.target_address.clone()
66    }
67    pub fn set_target_address(&mut self, target_address: UnresolvedAddressDto) {
68        self.body.target_address = target_address;   // MARKER1 AttributeKind.CUSTOM
69    }
70
71
72    pub fn get_scoped_metadata_key(&self) -> u64 {
73        self.body.scoped_metadata_key.clone()
74    }
75    pub fn set_scoped_metadata_key(&mut self, scoped_metadata_key: u64) {
76        self.body.scoped_metadata_key = scoped_metadata_key;   // MARKER1 AttributeKind.SIMPLE
77    }
78
79
80    pub fn get_target_namespace_id(&self) -> NamespaceIdDto {
81        self.body.target_namespace_id.clone()
82    }
83    pub fn set_target_namespace_id(&mut self, target_namespace_id: NamespaceIdDto) {
84        self.body.target_namespace_id = target_namespace_id;   // MARKER1 AttributeKind.CUSTOM
85    }
86
87
88    pub fn get_value_size_delta(&self) -> u16 {
89        self.body.value_size_delta.clone()
90    }
91    pub fn set_value_size_delta(&mut self, value_size_delta: u16) {
92        self.body.value_size_delta = value_size_delta;   // MARKER1 AttributeKind.SIMPLE
93    }
94
95
96    pub fn get_value(&self) -> Vec<u8> {
97        self.body.value.clone()
98    }
99    pub fn set_value(&mut self, value: Vec<u8>) {
100        self.body.value = value;   // MARKER1 AttributeKind.BUFFER
101    }
102
103    /// Gets the size of the type.
104    ///
105    /// Returns:
106    /// A size in bytes.
107    pub fn get_size(&self) -> usize {
108        let mut size = self.super_object.get_size();
109        size += self.body.get_size();
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.get_size() as u32).to_le_bytes().to_vec());
120        buf.append(&mut self.super_object.serializer());
121        buf.append(&mut self.body.serializer()); // kind:CUSTOM TransactionBody
122        buf
123    }
124}
125
126impl EmbeddedTransactionHelper for EmbeddedNamespaceMetadataTransactionBuilder {
127    fn box_clone(&self) -> Box<dyn EmbeddedTransactionHelper> {
128        Box::new((*self).clone())
129    }
130
131    fn get_size(&self) -> usize {
132        self.get_size()
133    }
134
135    fn serializer(&self) -> Vec<u8> {
136        self.serializer()
137    }
138}
139