catbuffer_rust/receipt_source_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 for receipt source.
25#[derive(Debug, Clone)]
26pub struct ReceiptSourceBuilder {
27 /// Transaction primary source (e.g. index within block).
28 primary_id: u32,
29 /// Transaction secondary source (e.g. index within aggregate).
30 secondary_id: u32,
31}
32
33
34impl ReceiptSourceBuilder {
35 /// Creates an instance of ReceiptSourceBuilder from binary payload.
36 /// payload: Byte payload to use to serialize the object.
37 /// # Returns
38 /// A ReceiptSourceBuilder.
39 pub fn from_binary(_bytes: &[u8]) -> Self {
40 let buf = fixed_bytes::<4>(&_bytes);
41 let primary_id = u32::from_le_bytes(buf); // kind:SIMPLE
42 let _bytes = (&_bytes[4..]).to_vec();
43 let buf = fixed_bytes::<4>(&_bytes);
44 let secondary_id = u32::from_le_bytes(buf); // kind:SIMPLE
45 let _bytes = (&_bytes[4..]).to_vec();
46 ReceiptSourceBuilder { primary_id, secondary_id }
47 }
48
49 /// Gets transaction primary source (e.g. index within block).
50 ///
51 /// # Returns
52 /// A Transaction primary source (e.g. index within block).
53 pub fn get_primary_id(&self) -> u32 {
54 self.primary_id.clone()
55 }
56
57 /// Gets transaction secondary source (e.g. index within aggregate).
58 ///
59 /// # Returns
60 /// A Transaction secondary source (e.g. index within aggregate).
61 pub fn get_secondary_id(&self) -> u32 {
62 self.secondary_id.clone()
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; // primary_id;
72 size += 4; // secondary_id;
73 size
74 }
75
76 /// Serializes self to bytes.
77 ///
78 /// # Returns
79 /// A Serialized bytes.
80 pub fn serializer(&self) -> Vec<u8> {
81 let mut buf: Vec<u8> = vec![];
82 buf.append(&mut self.get_primary_id().to_le_bytes().to_vec()); // kind:SIMPLE
83 buf.append(&mut self.get_secondary_id().to_le_bytes().to_vec()); // kind:SIMPLE
84 buf
85 }
86}
87