catbuffer_rust/
global_key_value_set_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::global_key_value_builder::*;
24
25/// Binary layout for a global restriction key-value set.
26#[derive(Debug, Clone)]
27pub struct GlobalKeyValueSetBuilder {
28    /// Key value array.
29    keys: Vec<GlobalKeyValueBuilder>,
30}
31
32
33impl GlobalKeyValueSetBuilder {
34    /// Creates an instance of GlobalKeyValueSetBuilder from binary payload.
35    /// payload: Byte payload to use to serialize the object.
36    /// # Returns
37    /// A GlobalKeyValueSetBuilder.
38    pub fn from_binary(_bytes: &[u8]) -> Self {
39        let buf = fixed_bytes::<1>(&_bytes);
40        let keyValueCount = u8::from_le_bytes(buf); // kind:SIZE_FIELD
41        let mut _bytes = (&_bytes[1..]).to_vec();
42        let mut keys: Vec<GlobalKeyValueBuilder> = vec![]; // kind:ARRAY
43        let mut _bytes = _bytes.to_vec();
44        for _ in 0..keyValueCount {
45            let item = GlobalKeyValueBuilder::from_binary(&_bytes);
46            keys.push(item.clone());
47            _bytes = (&_bytes[item.get_size()..]).to_vec();
48        }
49        GlobalKeyValueSetBuilder { keys }
50    }
51
52    /// Gets key value array.
53    ///
54    /// # Returns
55    /// A Key value array.
56    pub fn get_keys(&self) -> Vec<GlobalKeyValueBuilder> {
57        self.keys.clone() // ARRAY or FILL_ARRAY
58    }
59
60    /// Gets the size of the type.
61    ///
62    /// Returns:
63    /// A size in bytes.
64    pub fn get_size(&self) -> usize {
65        let mut size = 0;
66        size += 1; // key_value_count;
67        size += self.keys.iter().map(|item| item.get_size()).sum::<usize>(); // array or fill_array;
68        size
69    }
70
71    /// Serializes self to bytes.
72    ///
73    /// # Returns
74    /// A Serialized bytes.
75    pub fn serializer(&self) -> Vec<u8> {
76        let mut buf: Vec<u8> = vec![];
77        buf.append(&mut (self.get_keys().len() as u8).to_le_bytes().to_vec()); // kind:SIZE_FIELD
78        for i in &self.keys {
79            buf.append(&mut i.serializer()); // kind:ARRAY|FILL_ARRAY
80        }
81        buf
82    }
83}
84