barectf_parser/types/
packet.rs

1use crate::{
2    config::ClockType,
3    types::{Event, EventCount, FieldValue, SequenceNumber, StreamId, Timestamp},
4};
5use internment::Intern;
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
10pub struct Packet {
11    pub header: PacketHeader,
12    pub context: PacketContext,
13    pub events: Vec<Event>,
14}
15
16#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
17pub struct PacketHeader {
18    /// Magic number ([`PacketHeader::MAGIC`]) specifies that this is a CTF packet.
19    pub magic_number: Option<u32>,
20    /// Trace UUID, used to ensure the event packet match the metadata used.
21    pub trace_uuid: Option<Uuid>,
22    /// Stream ID, used as reference to stream description in metadata.
23    pub stream_id: StreamId,
24    /// Stream name
25    pub stream_name: Intern<String>,
26    /// Name of this stream's default clock
27    pub clock_name: Option<Intern<String>>,
28    /// This stream's clock type
29    pub clock_type: Option<Intern<ClockType>>,
30}
31
32impl PacketHeader {
33    pub const MAGIC: u32 = 0xC1FC_1FC1;
34}
35
36#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
37pub struct PacketContext {
38    /// Event packet size (in bits, includes padding).
39    pub packet_size_bits: usize,
40    /// Event packet content size (in bits).
41    pub content_size_bits: usize,
42    /// Time-stamp at the beginning of the event packet.
43    pub beginning_timestamp: Option<Timestamp>,
44    /// Time-stamp at the end of the event packet.
45    pub end_timestamp: Option<Timestamp>,
46    /// Snapshot of a per-stream free-running counter,
47    /// counting the number of events discarded that were supposed to be
48    /// written in the stream after the last event in the event packet.
49    pub events_discarded: Option<EventCount>,
50    /// Per-stream event packet sequence count.
51    pub sequence_number: Option<SequenceNumber>,
52    /// Extra, user-defined members to be appended to this data stream type’s packet context structure field type.
53    pub extra_members: Vec<(Intern<String>, FieldValue)>,
54}
55
56impl PacketContext {
57    /// Event packet size (in bytes).
58    pub fn packet_size(&self) -> usize {
59        self.packet_size_bits >> 3
60    }
61
62    /// Event packet content size (in bytes).
63    pub fn content_size(&self) -> usize {
64        self.content_size_bits >> 3
65    }
66}