dsf_core/base/
header.rs

1//! Header is a high level representation of the protocol header used in all DSF objects
2
3use crate::types::{Flags, Kind};
4
5/// Header object length
6pub const HEADER_LEN: usize = 16;
7
8/// Offsets for fixed fields in the protocol header
9pub mod offsets {
10    pub const PROTO_VERSION: usize = 0;
11    pub const APPLICATION_ID: usize = 2;
12    pub const OBJECT_KIND: usize = 4;
13    pub const FLAGS: usize = 6;
14    pub const INDEX: usize = 8;
15    pub const DATA_LEN: usize = 10;
16    pub const PRIVATE_OPTIONS_LEN: usize = 12;
17    pub const PUBLIC_OPTIONS_LEN: usize = 14;
18    pub const ID: usize = 16;
19    pub const BODY: usize = 48;
20}
21
22/// Header encodes information for a given page in the database.
23/// Wire encoding and decoding exists in `wire::header`
24#[derive(Clone, PartialEq, Debug)]
25#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
26pub struct Header {
27    /// Protocol version
28    pub protocol_version: u16,
29
30    // Application ID
31    pub application_id: u16,
32
33    /// Object kind
34    pub kind: Kind,
35
36    /// Object flags
37    pub flags: Flags,
38
39    /// Index is the Page Version for Pages, or the Request ID for messages
40    pub index: u16,
41}
42
43impl Default for Header {
44    fn default() -> Self {
45        Self {
46            protocol_version: 0,
47            application_id: 0,
48            kind: Kind(0),
49            flags: Flags::default(),
50            index: 0,
51        }
52    }
53}
54
55impl Header {
56    pub fn new(application_id: u16, kind: Kind, index: u16, flags: Flags) -> Header {
57        Header {
58            protocol_version: 0,
59            application_id,
60            kind,
61            flags,
62            index,
63        }
64    }
65
66    pub fn protocol_version(&self) -> u16 {
67        self.protocol_version
68    }
69
70    pub fn application_id(&self) -> u16 {
71        self.application_id
72    }
73
74    pub fn kind(&self) -> Kind {
75        self.kind
76    }
77
78    pub fn flags(&self) -> Flags {
79        self.flags
80    }
81
82    pub fn index(&self) -> u16 {
83        self.index
84    }
85}