-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_header.rs
More file actions
47 lines (39 loc) · 1.47 KB
/
request_header.rs
File metadata and controls
47 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::cell::RefCell;
use binary_codec::{FromBytes, ToBytes};
use serde::{Deserialize, Serialize};
use crate::{core::BucketId, packets::header::type_and_flags::RequestPacketType};
/// Plabble Packet request header
#[derive(FromBytes, ToBytes, Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct PlabbleRequestHeader {
/// Internal packet type field for binary serialization/deserialization
#[serde(skip_serializing, skip_deserializing)]
#[bits = 4]
#[variant_for("packet_type")]
_type: RefCell<u8>,
/// Packet type (derived from `_type`)
#[variant_by = "packet_type"]
#[serde(flatten)]
pub packet_type: RequestPacketType,
/// ID of the bucket, if needed by the type
/// The following types require the bucket ID in the header:
/// Get, Stream, Patch, Put, Delete, Subscribe
#[toggled_by_variant = "packet_type=2|3|5|6|7|8"]
pub id: Option<BucketId>,
}
impl PlabbleRequestHeader {
/// Create new packet header for specific request packet type
pub fn new(packet_type: RequestPacketType, id: Option<BucketId>) -> Self {
Self {
_type: RefCell::new(packet_type.get_discriminator()),
packet_type,
id,
}
}
/// Indicates if SESSION packet
pub fn is_session_packet(&self) -> bool {
matches!(self.packet_type, RequestPacketType::Session { .. })
}
pub fn preprocess(&self) {
self._type.replace(self.packet_type.get_discriminator());
}
}