buffa/text/error.rs
1//! Parse error type for the textproto format.
2//!
3//! Every parse error carries the 1-based `line:col` position into the input
4//! string, mirroring the protobuf C++ and Go text-format diagnostics.
5
6/// An error encountered while parsing textproto input.
7///
8/// The `line` and `col` fields are 1-based and point at (or immediately after)
9/// the offending byte in the input. `col` counts Unicode scalar values, not
10/// bytes, so multi-byte UTF-8 characters advance the column by one.
11#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
12#[error("text format parse error (line {line}:{col}): {kind}")]
13#[non_exhaustive]
14pub struct ParseError {
15 /// 1-based line number.
16 pub line: u32,
17 /// 1-based column (Unicode scalar count from start of line).
18 pub col: u32,
19 /// What went wrong.
20 pub kind: ParseErrorKind,
21}
22
23impl ParseError {
24 /// Construct a new parse error at the given position.
25 #[inline]
26 pub(crate) fn new(line: u32, col: u32, kind: ParseErrorKind) -> Self {
27 Self { line, col, kind }
28 }
29}
30
31/// The category of textproto parse error.
32///
33/// Variants are all zero-allocation: they hold `&'static str` or small `Copy`
34/// payloads. The full diagnostic (including position) lives in [`ParseError`].
35#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
36#[non_exhaustive]
37pub enum ParseErrorKind {
38 /// Input ended while a value was still being parsed.
39 #[error("unexpected end of input")]
40 UnexpectedEof,
41
42 /// A token appeared where the grammar didn't permit it.
43 ///
44 /// The `expected` string describes what the parser wanted — e.g.
45 /// `"field name"`, `"scalar value"`, `"',' or ']'"`.
46 #[error("unexpected token, expected {expected}")]
47 UnexpectedToken {
48 /// Human-readable description of what was expected.
49 expected: &'static str,
50 },
51
52 /// A number literal couldn't be interpreted as the target numeric type
53 /// (out of range, wrong base, or float where integer expected).
54 #[error("invalid number literal")]
55 InvalidNumber,
56
57 /// A string literal contained a malformed escape sequence.
58 ///
59 /// Holds the reason produced by the unescaper — e.g.
60 /// `"invalid \\x escape"`, `"invalid unicode escape"`.
61 #[error("invalid string: {0}")]
62 InvalidString(&'static str),
63
64 /// A `string` field's unescaped bytes were not valid UTF-8.
65 ///
66 /// `bytes` fields use [`TextDecoder::read_bytes`](super::TextDecoder::read_bytes)
67 /// and do not produce this error.
68 #[error("invalid UTF-8 in string field")]
69 InvalidUtf8,
70
71 /// A field name appeared that the message's `merge_text` does not
72 /// recognise, and the caller did not ask to skip unknowns.
73 #[error("unknown field")]
74 UnknownField,
75
76 /// An enum variant name was not recognised, or a closed enum received a
77 /// numeric value outside its defined set.
78 ///
79 /// Open (proto3) enums accept any in-range integer and never produce this
80 /// error for numeric input. Closed (proto2) enums reject unknown numbers.
81 #[error("unknown enum value")]
82 UnknownEnumValue,
83
84 /// Message nesting exceeded [`RECURSION_LIMIT`](crate::RECURSION_LIMIT).
85 #[error("recursion limit exceeded")]
86 RecursionLimitExceeded,
87
88 /// The parse would materialize more repeated/map elements than the
89 /// configured element-memory budget allows.
90 ///
91 /// The textproto equivalent of
92 /// [`DecodeError::ElementMemoryLimitExceeded`](crate::DecodeError::ElementMemoryLimitExceeded):
93 /// and needed for the same reason: an empty repeated message element
94 /// costs three input bytes (`{},`) and `size_of::<Element>()` in the
95 /// `Vec` it lands in — very nearly the wire format's own ratio, which is
96 /// two bytes for the same footprint — so a payload well inside any
97 /// input-size cap can still expand by two orders of magnitude. Raise it
98 /// with
99 /// [`TextDecoder::with_element_memory_limit`](crate::text::TextDecoder::with_element_memory_limit).
100 #[error("element memory limit exceeded")]
101 ElementMemoryLimitExceeded,
102
103 /// A message was opened with `{` but closed with `>`, or vice versa.
104 ///
105 /// Textproto allows either `{...}` or `<...>` around sub-messages, but
106 /// the pair must match.
107 #[error("mismatched message delimiters")]
108 DelimiterMismatch,
109
110 /// An internal invariant was violated during parsing.
111 ///
112 /// This indicates a bug in buffa, not a problem with the input. Please
113 /// report it if encountered.
114 #[error("internal error (this is a buffa bug): {0}")]
115 Internal(&'static str),
116}