datex_core/global/protocol_structures/
block_header.rs1use super::serializable::Serializable;
2use crate::values::core_values::endpoint::Endpoint;
3use binrw::{BinRead, BinWrite};
4
5use modular_bitfield::{Specifier, bitfield, prelude::B43};
6use strum_macros::Display;
7
8#[derive(
10 Debug,
11 Display,
12 PartialEq,
13 Clone,
14 Copy,
15 Default,
16 Specifier,
17 serde::Serialize,
18 serde::Deserialize,
19)]
20#[bits = 4]
21pub enum BlockType {
22 #[default]
23 Request = 0,
24 Response = 1,
25 Hello = 2,
26 Trace = 3,
27 TraceBack = 4,
28}
29
30impl BlockType {
31 pub fn is_response(&self) -> bool {
32 core::matches!(self, BlockType::Response | BlockType::TraceBack)
33 }
34}
35
36#[bitfield]
40#[derive(BinWrite, BinRead, Clone, Copy, Debug, PartialEq)]
41#[bw(map = |&x| Self::into_bytes(x))]
42#[br(map = Self::from_bytes)]
43#[brw(little)]
44pub struct FlagsAndTimestamp {
45 pub block_type: BlockType,
47 pub has_side_effects: bool,
49 pub has_only_data: bool,
51 pub is_end_of_section: bool,
53 pub is_end_of_context: bool,
55 pub has_lifetime: bool,
57 pub has_represented_by: bool,
59 pub has_iv: bool,
61 pub is_compressed: bool,
63 pub is_signature_in_last_subblock: bool,
65 pub has_skip_after_block: bool,
67 pub is_debug_block: bool,
69 #[allow(unused)]
70 unused_2: bool,
71 #[allow(unused)]
72 unused_3: bool,
73 #[allow(unused)]
74 unused_4: bool,
75 #[allow(unused)]
76 unused_5: bool,
77 #[allow(unused)]
78 unused_6: bool,
79 #[allow(unused)]
80 unused_7: bool,
81
82 pub creation_timestamp: B43,
83}
84
85mod flags_and_timestamp_serde {
86 use super::*;
87 use serde::{Deserialize, Deserializer, Serialize, Serializer};
88
89 #[derive(Serialize, Deserialize)]
90 struct FlagsHelper {
91 block_type: BlockType,
92 has_side_effects: bool,
93 has_only_data: bool,
94 is_end_of_section: bool,
95 is_end_of_context: bool,
96 has_lifetime: bool,
97 has_represented_by: bool,
98 has_iv: bool,
99 is_compressed: bool,
100 is_signature_in_last_subblock: bool,
101 creation_timestamp: u64,
102 }
103
104 impl Serialize for FlagsAndTimestamp {
105 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
106 where
107 S: Serializer,
108 {
109 let helper = FlagsHelper {
110 block_type: self.block_type(),
111 has_side_effects: self.has_side_effects(),
112 has_only_data: self.has_only_data(),
113 is_end_of_section: self.is_end_of_section(),
114 is_end_of_context: self.is_end_of_context(),
115 has_lifetime: self.has_lifetime(),
116 has_represented_by: self.has_represented_by(),
117 has_iv: self.has_iv(),
118 is_compressed: self.is_compressed(),
119 is_signature_in_last_subblock: self
120 .is_signature_in_last_subblock(),
121 creation_timestamp: self.creation_timestamp(),
122 };
123 helper.serialize(serializer)
124 }
125 }
126
127 impl<'de> Deserialize<'de> for FlagsAndTimestamp {
128 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
129 where
130 D: Deserializer<'de>,
131 {
132 let helper = FlagsHelper::deserialize(deserializer)?;
133 Ok(FlagsAndTimestamp::new()
134 .with_block_type(helper.block_type)
135 .with_has_side_effects(helper.has_side_effects)
136 .with_has_only_data(helper.has_only_data)
137 .with_is_end_of_section(helper.is_end_of_section)
138 .with_is_end_of_context(helper.is_end_of_context)
139 .with_has_lifetime(helper.has_lifetime)
140 .with_has_represented_by(helper.has_represented_by)
141 .with_has_iv(helper.has_iv)
142 .with_is_compressed(helper.is_compressed)
143 .with_is_signature_in_last_subblock(
144 helper.is_signature_in_last_subblock,
145 )
146 .with_creation_timestamp(helper.creation_timestamp))
147 }
148 }
149}
150
151impl Default for FlagsAndTimestamp {
152 fn default() -> Self {
153 FlagsAndTimestamp::new()
154 .with_block_type(BlockType::Request)
155 .with_has_side_effects(true)
156 .with_has_only_data(false)
157 .with_is_end_of_section(true)
158 .with_is_end_of_context(true)
159 .with_has_lifetime(false)
160 .with_has_represented_by(false)
161 .with_has_iv(false)
162 .with_is_compressed(false)
163 .with_is_signature_in_last_subblock(false)
164 }
165}
166
167#[derive(
170 serde::Serialize,
171 serde::Deserialize,
172 Debug,
173 Clone,
174 Default,
175 BinWrite,
176 BinRead,
177 PartialEq,
178)]
179#[brw(little)]
180pub struct BlockHeader {
181 pub context_id: u32,
184 pub section_index: u16,
188 pub block_number: u16,
194
195 pub flags_and_timestamp: FlagsAndTimestamp,
196
197 #[brw(if(flags_and_timestamp.has_skip_after_block()))]
201 pub skip_after_block: Option<u16>,
202
203 #[brw(if(flags_and_timestamp.has_lifetime()))]
204 pub lifetime: Option<u32>,
205
206 #[brw(if(flags_and_timestamp.has_represented_by()))]
207 pub represented_by: Option<Endpoint>,
208
209 #[brw(if(flags_and_timestamp.has_iv()))]
210 pub iv: Option<[u8; 16]>,
211}
212
213impl Serializable for BlockHeader {}