catbuffer_rust/state_header_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/// Header common to all serialized states.
25#[derive(Debug, Clone)]
26pub struct StateHeaderBuilder {
27 /// Serialization version.
28 version: u16,
29}
30
31
32impl StateHeaderBuilder {
33 /// Creates an instance of StateHeaderBuilder from binary payload.
34 /// payload: Byte payload to use to serialize the object.
35 /// # Returns
36 /// A StateHeaderBuilder.
37 pub fn from_binary(_bytes: &[u8]) -> Self {
38 let buf = fixed_bytes::<2>(&_bytes);
39 let version = u16::from_le_bytes(buf); // kind:SIMPLE
40 let _bytes = (&_bytes[2..]).to_vec();
41 StateHeaderBuilder { version }
42 }
43
44 /// Gets serialization version.
45 ///
46 /// # Returns
47 /// A Serialization version.
48 pub fn get_version(&self) -> u16 {
49 self.version.clone()
50 }
51
52 /// Gets the size of the type.
53 ///
54 /// Returns:
55 /// A size in bytes.
56 pub fn get_size(&self) -> usize {
57 let mut size = 0;
58 size += 2; // version;
59 size
60 }
61
62 /// Serializes self to bytes.
63 ///
64 /// # Returns
65 /// A Serialized bytes.
66 pub fn serializer(&self) -> Vec<u8> {
67 let mut buf: Vec<u8> = vec![];
68 buf.append(&mut self.get_version().to_le_bytes().to_vec()); // kind:SIMPLE
69 buf
70 }
71}
72