catbuffer_rust/
namespace_path_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::generator_utils::*;
23use super::namespace_alias_builder::*;
24use super::namespace_id_dto::*;
25
26/// Binary layout for a namespace path.
27#[derive(Debug, Clone)]
28pub struct NamespacePathBuilder {
29    /// Namespace path (excluding root id).
30    path: Vec<NamespaceIdDto>,
31    /// Namespace alias.
32    alias: NamespaceAliasBuilder,
33}
34
35
36impl NamespacePathBuilder {
37    /// Creates an instance of NamespacePathBuilder from binary payload.
38    /// payload: Byte payload to use to serialize the object.
39    /// # Returns
40    /// A NamespacePathBuilder.
41    pub fn from_binary(_bytes: &[u8]) -> Self {
42        let buf = fixed_bytes::<1>(&_bytes);
43        let pathSize = u8::from_le_bytes(buf); // kind:SIZE_FIELD
44        let mut _bytes = (&_bytes[1..]).to_vec();
45        let mut path: Vec<NamespaceIdDto> = vec![]; // kind:ARRAY
46        let mut _bytes = _bytes.to_vec();
47        for _ in 0..pathSize {
48            let item = NamespaceIdDto::from_binary(&_bytes);
49            path.push(item.clone());
50            _bytes = (&_bytes[item.get_size()..]).to_vec();
51        }
52        let alias = NamespaceAliasBuilder::from_binary(&_bytes); // kind:CUSTOM1
53        let mut _bytes = _bytes[alias.get_size()..].to_vec();
54        NamespacePathBuilder { path, alias }
55    }
56
57    /// Gets namespace path (excluding root id).
58    ///
59    /// # Returns
60    /// A Namespace path (excluding root id).
61    pub fn get_path(&self) -> Vec<NamespaceIdDto> {
62        self.path.clone() // ARRAY or FILL_ARRAY
63    }
64
65    /// Gets namespace alias.
66    ///
67    /// # Returns
68    /// A Namespace alias.
69    pub fn get_alias(&self) -> NamespaceAliasBuilder {
70        self.alias.clone()
71    }
72
73    /// Gets the size of the type.
74    ///
75    /// Returns:
76    /// A size in bytes.
77    pub fn get_size(&self) -> usize {
78        let mut size = 0;
79        size += 1; // path_size;
80        size += self.path.iter().map(|item| item.get_size()).sum::<usize>(); // array or fill_array;
81        size += self.alias.get_size(); // alias;
82        size
83    }
84
85    /// Serializes self to bytes.
86    ///
87    /// # Returns
88    /// A Serialized bytes.
89    pub fn serializer(&self) -> Vec<u8> {
90        let mut buf: Vec<u8> = vec![];
91        buf.append(&mut (self.get_path().len() as u8).to_le_bytes().to_vec()); // kind:SIZE_FIELD
92        for i in &self.path {
93            buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
94        }
95        buf.append(&mut self.alias.serializer()); // kind:CUSTOM
96        buf
97    }
98}
99