catbuffer_rust/receipt_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::receipt_type_dto::*;
24
25/// Binary layout for a receipt entity.
26#[derive(Debug, Clone)]
27pub struct ReceiptBuilder {
28 /// Receipt version.
29 version: u16,
30 /// Receipt type.
31 _type: ReceiptTypeDto,
32}
33
34
35impl ReceiptBuilder {
36 /// Creates an instance of ReceiptBuilder from binary payload.
37 /// payload: Byte payload to use to serialize the object.
38 /// # Returns
39 /// A ReceiptBuilder.
40 pub fn from_binary(_bytes: &[u8]) -> Self {
41 let buf = fixed_bytes::<2>(&_bytes);
42 let version = u16::from_le_bytes(buf); // kind:SIMPLE
43 let _bytes = (&_bytes[2..]).to_vec();
44 let _type = ReceiptTypeDto::from_binary(&_bytes); // kind:CUSTOM2
45 let _bytes = (&_bytes[_type.get_size()..]).to_vec();
46 ReceiptBuilder { version, _type }
47 }
48
49 /// Gets receipt version.
50 ///
51 /// # Returns
52 /// A Receipt version.
53 pub fn get_version(&self) -> u16 {
54 self.version.clone()
55 }
56
57 /// Gets receipt type.
58 ///
59 /// # Returns
60 /// A Receipt type.
61 pub fn get_type(&self) -> ReceiptTypeDto {
62 self._type
63 }
64
65 /// Gets the size of the type.
66 ///
67 /// Returns:
68 /// A size in bytes.
69 pub fn get_size(&self) -> usize {
70 let mut size = 0;
71 size += 4; // size;
72 size += 2; // version;
73 size += self._type.get_size(); // type;
74 size
75 }
76
77 /// Serializes self to bytes.
78 ///
79 /// # Returns
80 /// A Serialized bytes.
81 pub fn serializer(&self) -> Vec<u8> {
82 let mut buf: Vec<u8> = vec![];
83 // Ignored serialization: size AttributeKind.SIMPLE
84 buf.append(&mut self.get_version().to_le_bytes().to_vec()); // kind:SIMPLE
85 buf.append(&mut self._type.serializer()); // kind:CUSTOM
86 buf
87 }
88}
89