trace_recorder_parser/snapshot/
markers.rs

1use crate::snapshot::Error;
2use derive_more::Display;
3use std::io::{Read, Seek};
4
5#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
6pub enum MarkerBytes {
7    #[display(fmt = "StartMarker ({:X?})", "MarkerBytes::Start.as_bytes()")]
8    Start,
9    #[display(fmt = "EndMarker ({:X?})", "MarkerBytes::End.as_bytes()")]
10    End,
11}
12
13impl MarkerBytes {
14    pub(crate) const SIZE: usize = 12;
15
16    pub(crate) const fn as_bytes(self) -> &'static [u8] {
17        use MarkerBytes::*;
18        match self {
19            Start => &[
20                0x01, 0x02, 0x03, 0x04, 0x71, 0x72, 0x73, 0x74, 0xF1, 0xF2, 0xF3, 0xF4,
21            ],
22            End => &[
23                0x0A, 0x0B, 0x0C, 0x0D, 0x71, 0x72, 0x73, 0x74, 0xF1, 0xF2, 0xF3, 0xF4,
24            ],
25        }
26    }
27
28    pub(crate) fn read<R: Read + Seek>(self, r: &mut R) -> Result<(), Error> {
29        let pos = r.stream_position()?;
30        let mut bytes: [u8; MarkerBytes::SIZE] = [0; 12];
31        r.read_exact(&mut bytes)?;
32        if bytes.as_slice() != self.as_bytes() {
33            Err(Error::MarkerBytes(pos, bytes, self))
34        } else {
35            Ok(())
36        }
37    }
38}
39
40#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
41pub enum DebugMarker {
42    #[display(fmt = "DebugMarker0 (0x{:X})", "DebugMarker::Marker0.into_u32()")]
43    Marker0,
44    #[display(fmt = "DebugMarker1 (0x{:X})", "DebugMarker::Marker1.into_u32()")]
45    Marker1,
46    #[display(fmt = "DebugMarker2 (0x{:X})", "DebugMarker::Marker2.into_u32()")]
47    Marker2,
48    #[display(fmt = "DebugMarker3 (0x{:X})", "DebugMarker::Marker3.into_u32()")]
49    Marker3,
50}
51
52impl DebugMarker {
53    const fn into_u32(self) -> u32 {
54        use DebugMarker::*;
55        match self {
56            Marker0 => 0xF0F0F0F0,
57            Marker1 => 0xF1F1F1F1,
58            Marker2 => 0xF2F2F2F2,
59            Marker3 => 0xF3F3F3F3,
60        }
61    }
62
63    pub(crate) fn read<R: Read + Seek>(self, r: &mut R) -> Result<(), Error> {
64        let pos = r.stream_position()?;
65        let mut bytes: [u8; 4] = [0; 4];
66        r.read_exact(&mut bytes)?;
67        let marker = u32::from_le_bytes(bytes);
68        if marker != self.into_u32() {
69            Err(Error::DebugMarker(pos, marker, self))
70        } else {
71            Ok(())
72        }
73    }
74}