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