catbuffer_rust/
metadata_value_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::*;
23
24/// Binary layout of a metadata entry value.
25#[derive(Debug, Clone)]
26pub struct MetadataValueBuilder {
27    /// Data of the value.
28    data: Vec<u8>,
29}
30
31
32impl MetadataValueBuilder {
33    /// Creates an instance of MetadataValueBuilder from binary payload.
34    /// payload: Byte payload to use to serialize the object.
35    /// # Returns
36    /// A MetadataValueBuilder.
37    pub fn from_binary(_bytes: &[u8]) -> Self {
38        let buf = fixed_bytes::<2>(&_bytes);
39        let size = u16::from_le_bytes(buf); // kind:SIZE_FIELD
40        let mut _bytes = (&_bytes[2..]).to_vec();
41        let data = (&_bytes[..size as usize]).to_vec(); // kind:BUFFER
42        let _bytes = (&_bytes[size as usize..]).to_vec();
43        MetadataValueBuilder { data }
44    }
45
46    /// Gets data of the value.
47    ///
48    /// # Returns
49    /// A Data of the value.
50    pub fn get_data(&self) -> Vec<u8> {
51        self.data.clone() // ARRAY or FILL_ARRAY
52    }
53
54    /// Gets the size of the type.
55    ///
56    /// Returns:
57    /// A size in bytes.
58    pub fn get_size(&self) -> usize {
59        let mut size = 0;
60        size += 2; // size;
61        size += self.data.len(); // data;
62        size
63    }
64
65    /// Serializes self to bytes.
66    ///
67    /// # Returns
68    /// A Serialized bytes.
69    pub fn serializer(&self) -> Vec<u8> {
70        let mut buf: Vec<u8> = vec![];
71        buf.append(&mut (self.get_data().len() as u16).to_le_bytes().to_vec()); // kind:SIZE_FIELD
72        buf.append(&mut self.data.clone()); // kind:BUFFER
73        buf
74    }
75}
76