facet_format_msgpack/
parser.rs

1//! MsgPack parser implementing FormatParser and FormatJitParser.
2//!
3//! This is a Tier-2 only parser. The FormatParser methods return errors
4//! because only JIT deserialization is supported.
5
6use crate::error::{MsgPackError, codes};
7use facet_format::{FieldEvidence, FormatParser, ParseEvent, ProbeStream};
8
9/// MsgPack parser for Tier-2 JIT deserialization.
10///
11/// This parser only supports JIT mode. Calling non-JIT methods will return errors.
12pub struct MsgPackParser<'de> {
13    #[cfg_attr(not(feature = "jit"), allow(dead_code))]
14    input: &'de [u8],
15    pos: usize,
16}
17
18impl<'de> MsgPackParser<'de> {
19    /// Create a new MsgPack parser from input bytes.
20    pub fn new(input: &'de [u8]) -> Self {
21        Self { input, pos: 0 }
22    }
23
24    /// Create an "unsupported" error for non-JIT methods.
25    fn unsupported_error(&self) -> MsgPackError {
26        MsgPackError {
27            code: codes::UNSUPPORTED,
28            pos: self.pos,
29            message: "MsgPackParser is Tier-2 JIT only - FormatParser methods are not supported"
30                .to_string(),
31        }
32    }
33}
34
35/// Stub probe stream for MsgPackParser.
36///
37/// This is never actually used since we don't support non-JIT parsing.
38pub struct MsgPackProbe;
39
40impl<'de> ProbeStream<'de> for MsgPackProbe {
41    type Error = MsgPackError;
42
43    fn next(&mut self) -> Result<Option<FieldEvidence<'de>>, Self::Error> {
44        Err(MsgPackError {
45            code: codes::UNSUPPORTED,
46            pos: 0,
47            message: "MsgPackParser is Tier-2 JIT only - ProbeStream methods are not supported"
48                .to_string(),
49        })
50    }
51}
52
53impl<'de> FormatParser<'de> for MsgPackParser<'de> {
54    type Error = MsgPackError;
55    type Probe<'a>
56        = MsgPackProbe
57    where
58        Self: 'a;
59
60    fn next_event(&mut self) -> Result<Option<ParseEvent<'de>>, Self::Error> {
61        Err(self.unsupported_error())
62    }
63
64    fn peek_event(&mut self) -> Result<Option<ParseEvent<'de>>, Self::Error> {
65        Err(self.unsupported_error())
66    }
67
68    fn skip_value(&mut self) -> Result<(), Self::Error> {
69        Err(self.unsupported_error())
70    }
71
72    fn begin_probe(&mut self) -> Result<Self::Probe<'_>, Self::Error> {
73        Err(self.unsupported_error())
74    }
75}
76
77#[cfg(feature = "jit")]
78impl<'de> facet_format::FormatJitParser<'de> for MsgPackParser<'de> {
79    type FormatJit = crate::jit::MsgPackJitFormat;
80
81    fn jit_input(&self) -> &'de [u8] {
82        self.input
83    }
84
85    fn jit_pos(&self) -> Option<usize> {
86        // MsgPack parser is always in a clean state for JIT
87        // (no peeked events, no stack, etc.)
88        Some(self.pos)
89    }
90
91    fn jit_set_pos(&mut self, pos: usize) {
92        self.pos = pos;
93    }
94
95    fn jit_format(&self) -> Self::FormatJit {
96        crate::jit::MsgPackJitFormat
97    }
98
99    fn jit_error(&self, _input: &'de [u8], error_pos: usize, error_code: i32) -> Self::Error {
100        MsgPackError::from_code(error_code, error_pos)
101    }
102}