catbuffer_rust/
namespace_alias_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::mosaic_id_dto::*;
25use super::namespace_alias_type_dto::*;
26
27/// Binary layout for alias.
28#[derive(Debug, Clone)]
29pub struct NamespaceAliasBuilder {
30    /// Namespace alias type.
31    namespace_alias_type: NamespaceAliasTypeDto,
32    /// Mosaic alias.
33    mosaic_alias: Option<MosaicIdDto>,
34    /// Address alias.
35    address_alias: Option<AddressDto>,
36}
37
38
39impl NamespaceAliasBuilder {
40    /// Creates an instance of NamespaceAliasBuilder from binary payload.
41    /// payload: Byte payload to use to serialize the object.
42    /// # Returns
43    /// A NamespaceAliasBuilder.
44    pub fn from_binary(_bytes: &[u8]) -> Self {
45        let namespace_alias_type = NamespaceAliasTypeDto::from_binary(&_bytes); // kind:CUSTOM2
46        let mut _bytes = _bytes[namespace_alias_type.get_size()..].to_vec();
47        let mut mosaic_alias = None;
48        if namespace_alias_type == NamespaceAliasTypeDto::MOSAIC_ID {
49            let raw_mosaic_alias = MosaicIdDto::from_binary(&_bytes);
50            _bytes = (&_bytes[raw_mosaic_alias.get_size()..]).to_vec();
51            mosaic_alias = Some(raw_mosaic_alias); // kind:CUSTOM1
52        }
53        let mut address_alias = None;
54        if namespace_alias_type == NamespaceAliasTypeDto::ADDRESS {
55            let raw_address_alias = AddressDto::from_binary(&_bytes);
56            _bytes = (&_bytes[raw_address_alias.get_size()..]).to_vec();
57            address_alias = Some(raw_address_alias); // kind:CUSTOM1
58        }
59        NamespaceAliasBuilder { namespace_alias_type, mosaic_alias, address_alias }
60    }
61
62    /// Gets namespace alias type.
63    ///
64    /// # Returns
65    /// A Namespace alias type.
66    pub fn get_namespace_alias_type(&self) -> NamespaceAliasTypeDto {
67        self.namespace_alias_type.clone()
68    }
69
70    /// Gets mosaic alias.
71    ///
72    /// # Returns
73    /// A Mosaic alias.
74    pub fn get_mosaic_alias(&self) -> Option<MosaicIdDto> {
75        if self.namespace_alias_type != NamespaceAliasTypeDto::MOSAIC_ID {
76            panic!("namespaceAliasType is not set to MOSAIC_ID.")
77        };
78        self.mosaic_alias.clone()
79    }
80
81    /// Gets address alias.
82    ///
83    /// # Returns
84    /// A Address alias.
85    pub fn get_address_alias(&self) -> Option<AddressDto> {
86        if self.namespace_alias_type != NamespaceAliasTypeDto::ADDRESS {
87            panic!("namespaceAliasType is not set to ADDRESS.")
88        };
89        self.address_alias.clone()
90    }
91
92    /// Gets the size of the type.
93    ///
94    /// Returns:
95    /// A size in bytes.
96    pub fn get_size(&self) -> usize {
97        let mut size = 0;
98        size += self.namespace_alias_type.get_size(); // namespace_alias_type;
99        if self.namespace_alias_type == NamespaceAliasTypeDto::MOSAIC_ID {
100            size += self.mosaic_alias.as_ref().unwrap().get_size(); // mosaic_alias
101        }
102        if self.namespace_alias_type == NamespaceAliasTypeDto::ADDRESS {
103            size += self.address_alias.as_ref().unwrap().get_size(); // address_alias
104        }
105        size
106    }
107
108    /// Serializes self to bytes.
109    ///
110    /// # Returns
111    /// A Serialized bytes.
112    pub fn serializer(&self) -> Vec<u8> {
113        let mut buf: Vec<u8> = vec![];
114        buf.append(&mut self.namespace_alias_type.serializer()); // kind:CUSTOM
115        if self.namespace_alias_type == NamespaceAliasTypeDto::MOSAIC_ID {
116            buf.append(&mut self.mosaic_alias.as_ref().unwrap().serializer()); // kind:CUSTOM
117        };
118        if self.namespace_alias_type == NamespaceAliasTypeDto::ADDRESS {
119            buf.append(&mut self.address_alias.as_ref().unwrap().serializer()); // kind:CUSTOM
120        };
121        buf
122    }
123}
124