Skip to main content

entrenar/monitor/inference/serialization/
format.rs

1//! Trace format types and constants.
2
3use serde::{Deserialize, Serialize};
4
5/// Trace format for serialization
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7pub enum TraceFormat {
8    /// APRT binary format (compact, fast)
9    ///
10    /// Header: 0x41505254 ("APRT"), version byte, path type
11    Binary,
12
13    /// Pretty-printed JSON
14    Json,
15
16    /// JSON Lines (one JSON per line)
17    JsonLines,
18}
19
20/// Magic bytes for APRT binary format
21pub const APRT_MAGIC: [u8; 4] = [0x41, 0x50, 0x52, 0x54]; // "APRT"
22
23/// Current format version
24pub const APRT_VERSION: u8 = 1;
25
26/// Path type identifiers
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28#[repr(u8)]
29pub enum PathType {
30    Linear = 0,
31    Tree = 1,
32    Forest = 2,
33    KNN = 3,
34    Neural = 4,
35    Custom = 255,
36}
37
38impl From<u8> for PathType {
39    fn from(value: u8) -> Self {
40        match value {
41            0 => PathType::Linear,
42            1 => PathType::Tree,
43            2 => PathType::Forest,
44            3 => PathType::KNN,
45            4 => PathType::Neural,
46            5..=255 => PathType::Custom,
47        }
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_path_type_from_u8_all_variants() {
57        let cases: &[(u8, PathType)] = &[
58            (0, PathType::Linear),
59            (1, PathType::Tree),
60            (2, PathType::Forest),
61            (3, PathType::KNN),
62            (4, PathType::Neural),
63            (5, PathType::Custom),
64            (128, PathType::Custom),
65            (255, PathType::Custom),
66        ];
67
68        for &(input, expected) in cases {
69            let result = PathType::from(input);
70            // Syntactic match covering all arms from From<u8>
71            let label = match input {
72                0 => PathType::Linear,
73                1 => PathType::Tree,
74                2 => PathType::Forest,
75                3 => PathType::KNN,
76                4 => PathType::Neural,
77                5..=255 => PathType::Custom,
78            };
79            assert_eq!(result, expected);
80            assert_eq!(result, label);
81        }
82    }
83
84    #[test]
85    fn test_trace_format_variants() {
86        let _binary = TraceFormat::Binary;
87        let _json = TraceFormat::Json;
88        let _jsonl = TraceFormat::JsonLines;
89    }
90
91    #[test]
92    fn test_aprt_constants() {
93        assert_eq!(APRT_MAGIC, [0x41, 0x50, 0x52, 0x54]);
94        assert_eq!(APRT_VERSION, 1);
95    }
96}