Skip to main content

weavatrix_lsp_wire/
error.rs

1use std::{error::Error, fmt, io};
2
3/// An invalid resource-limit configuration.
4#[derive(Clone, Debug, Eq, PartialEq)]
5#[non_exhaustive]
6pub enum LimitsError {
7    /// The complete-header limit cannot hold the smallest valid frame header.
8    HeaderBytesTooSmall {
9        /// Minimum supported value.
10        minimum: usize,
11        /// Supplied value.
12        actual: usize,
13    },
14    /// The line limit cannot hold a minimal `Content-Length` line.
15    LineBytesTooSmall {
16        /// Minimum supported value.
17        minimum: usize,
18        /// Supplied value.
19        actual: usize,
20    },
21    /// A limit that must be non-zero was zero.
22    Zero {
23        /// Configuration field name.
24        name: &'static str,
25    },
26}
27
28/// A fatal inbound framing error.
29///
30/// After returning one of these errors, a [`crate::Decoder`] remains poisoned
31/// until [`crate::Decoder::reset`] is called.
32#[derive(Clone, Debug, Eq, PartialEq)]
33#[non_exhaustive]
34pub enum DecodeError {
35    /// The decoder previously failed and must be reset.
36    Poisoned,
37    /// A clean EOF was already marked; reset before starting another stream.
38    Finished,
39    /// The complete header block exceeded its configured bound.
40    HeaderTooLarge {
41        /// Configured maximum header bytes.
42        limit: usize,
43    },
44    /// A header line exceeded its configured bound.
45    LineTooLong {
46        /// Configured maximum line bytes.
47        limit: usize,
48    },
49    /// The header contained too many fields.
50    TooManyHeaderFields {
51        /// Configured maximum field count.
52        limit: usize,
53    },
54    /// A header byte was outside seven-bit ASCII.
55    NonAsciiHeader {
56        /// Rejected byte.
57        byte: u8,
58    },
59    /// An ASCII control byte was invalid in a header.
60    InvalidHeaderByte {
61        /// Rejected control byte.
62        byte: u8,
63    },
64    /// LF appeared without a preceding CR.
65    BareLineFeed,
66    /// CR was not immediately followed by LF.
67    BareCarriageReturn,
68    /// A field lacked a colon or used an invalid field name.
69    MalformedHeaderField,
70    /// The required `Content-Length` field was absent.
71    MissingContentLength,
72    /// More than one `Content-Length` field appeared.
73    DuplicateContentLength,
74    /// `Content-Length` was empty or contained a non-decimal byte.
75    InvalidContentLength,
76    /// `Content-Length` did not fit in `usize`.
77    ContentLengthOverflow,
78    /// The declared body length exceeded the inbound payload bound.
79    BodyTooLarge {
80        /// Declared payload length.
81        length: usize,
82        /// Configured maximum inbound payload length.
83        limit: usize,
84    },
85    /// More than one `Content-Type` field appeared.
86    DuplicateContentType,
87    /// The media type or parameter syntax was unsupported.
88    UnsupportedContentType,
89    /// A declared charset was not UTF-8.
90    UnsupportedCharset,
91    /// Memory for a bounded frame could not be reserved.
92    AllocationFailed {
93        /// Requested bounded capacity.
94        requested: usize,
95    },
96    /// EOF arrived partway through a header block.
97    UnexpectedEofHeaders {
98        /// Header bytes received before EOF.
99        received: usize,
100    },
101    /// EOF arrived before the declared body length.
102    UnexpectedEofBody {
103        /// Declared payload length.
104        expected: usize,
105        /// Payload bytes received before EOF.
106        received: usize,
107    },
108}
109
110/// An outbound payload could not be encoded.
111#[derive(Clone, Debug, Eq, PartialEq)]
112#[non_exhaustive]
113pub enum EncodeError {
114    /// The outbound payload exceeded its configured bound.
115    OutboundTooLarge {
116        /// Supplied payload length.
117        length: usize,
118        /// Configured maximum outbound payload length.
119        limit: usize,
120    },
121    /// The encoded header exceeded its configured byte bound.
122    HeaderTooLarge {
123        /// Encoded header length, including its terminator.
124        length: usize,
125        /// Configured maximum header bytes.
126        limit: usize,
127    },
128    /// The encoded `Content-Length` line exceeded its configured byte bound.
129    LineTooLong {
130        /// Encoded line length, excluding CRLF.
131        length: usize,
132        /// Configured maximum line bytes.
133        limit: usize,
134    },
135    /// The total encoded frame length overflowed `usize`.
136    FrameLengthOverflow,
137    /// Memory for the bounded encoded frame could not be reserved.
138    AllocationFailed {
139        /// Requested bounded frame capacity.
140        requested: usize,
141    },
142}
143
144/// Encoding or writing an outbound frame failed.
145#[derive(Debug)]
146#[non_exhaustive]
147pub enum WriteFrameError {
148    /// The payload violated encoder limits.
149    Encode(EncodeError),
150    /// The writer failed during `write_all` or `flush` and may contain a
151    /// partial frame. The underlying LSP stream must be discarded.
152    Io(io::Error),
153}
154
155impl fmt::Display for LimitsError {
156    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157        write!(f, "invalid wire limits: {self:?}")
158    }
159}
160
161impl fmt::Display for DecodeError {
162    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163        write!(f, "LSP frame decoding failed: {self:?}")
164    }
165}
166
167impl fmt::Display for EncodeError {
168    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169        write!(f, "LSP frame encoding failed: {self:?}")
170    }
171}
172
173impl fmt::Display for WriteFrameError {
174    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175        match self {
176            Self::Encode(error) => error.fmt(f),
177            Self::Io(error) => write!(f, "LSP frame write failed: {error}"),
178        }
179    }
180}
181
182impl Error for LimitsError {}
183impl Error for DecodeError {}
184impl Error for EncodeError {}
185impl Error for WriteFrameError {
186    fn source(&self) -> Option<&(dyn Error + 'static)> {
187        match self {
188            Self::Encode(error) => Some(error),
189            Self::Io(error) => Some(error),
190        }
191    }
192}
193
194impl From<EncodeError> for WriteFrameError {
195    fn from(value: EncodeError) -> Self {
196        Self::Encode(value)
197    }
198}
199
200impl From<io::Error> for WriteFrameError {
201    fn from(value: io::Error) -> Self {
202        Self::Io(value)
203    }
204}