catbuffer_rust/
namespace_registration_transaction_body_builder.rs1use super::block_duration_dto::*;
23use super::generator_utils::*;
24use super::namespace_id_dto::*;
25use super::namespace_registration_type_dto::*;
26
27#[derive(Debug, Clone)]
29pub struct NamespaceRegistrationTransactionBodyBuilder {
30 pub duration: Option<BlockDurationDto>,
32 pub parent_id: Option<NamespaceIdDto>,
34 pub id: NamespaceIdDto,
36 pub registration_type: NamespaceRegistrationTypeDto,
38 pub name: Vec<u8>,
40}
41
42impl NamespaceRegistrationTransactionBodyBuilder {
43 pub fn from_binary(payload: &[u8]) -> Self {
48 let mut _bytes = payload.to_vec();
49 let registration_type_condition = _bytes[0..8].to_vec();
50 _bytes = (&_bytes[8..]).to_vec();
51 let id = NamespaceIdDto::from_binary(&_bytes); _bytes = _bytes[id.get_size()..].to_vec();
53 let registration_type = NamespaceRegistrationTypeDto::from_binary(&_bytes); _bytes = (&_bytes[registration_type.get_size()..]).to_vec();
55 let buf = fixed_bytes::<1>(&_bytes);
56 let name_size = u8::from_le_bytes(buf); _bytes = (&_bytes[1..]).to_vec();
58 let name = (&_bytes[..name_size as usize]).to_vec(); _bytes = (&_bytes[name_size as usize..]).to_vec();
60 let mut duration = None;
61 if registration_type == NamespaceRegistrationTypeDto::ROOT {
62 duration = Some(BlockDurationDto::from_binary(®istration_type_condition)); }
64 let mut parent_id = None;
65 if registration_type == NamespaceRegistrationTypeDto::CHILD {
66 parent_id = Some(NamespaceIdDto::from_binary(®istration_type_condition)); }
68 NamespaceRegistrationTransactionBodyBuilder { duration, parent_id, id, registration_type, name } }
71
72 pub fn get_size(&self) -> usize {
77 let mut size = 0;
78 if self.registration_type == NamespaceRegistrationTypeDto::ROOT {
79 size += self.duration.as_ref().unwrap().get_size() }
81 if self.registration_type == NamespaceRegistrationTypeDto::CHILD {
82 size += self.parent_id.as_ref().unwrap().get_size() }
84 size += self.id.get_size(); size += self.registration_type.get_size(); size += 1; size += self.name.len();
88 size
89 }
90
91 pub fn serializer(&self) -> Vec<u8> {
96 let mut buf: Vec<u8> = vec![];
97 if self.registration_type == NamespaceRegistrationTypeDto::ROOT {
98 buf.append(&mut self.duration.as_ref().unwrap().serializer()); }
100 if self.registration_type == NamespaceRegistrationTypeDto::CHILD {
101 buf.append(&mut self.parent_id.as_ref().unwrap().serializer()); }
103 buf.append(&mut self.id.serializer()); buf.append(&mut self.registration_type.serializer()); let size_value: u8 = self.name.len() as u8;
106 buf.append(&mut size_value.to_le_bytes().to_vec()); buf.append(&mut self.name.clone()); buf
109 }
110}
111