Skip to main content

sim_lib_net_core/
error.rs

1//! The NetError type for net-core parsing failures.
2
3/// Errors produced by the net-core parsing primitives.
4///
5/// These describe malformed or oversize wire data only. They carry no I/O or
6/// application context; callers translate them into their own error domain.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub enum NetError {
9    /// The URL did not contain a `scheme://` separator, or was otherwise
10    /// structurally malformed (e.g. missing host).
11    MalformedUrl(String),
12    /// The URL scheme is not one this crate recognizes for default-port
13    /// resolution.
14    UnsupportedScheme(String),
15    /// The URL scheme did not match the scheme a caller required (via
16    /// [`parse_url_for_scheme`](crate::parse_url_for_scheme)).
17    UnexpectedScheme {
18        /// The scheme the caller required.
19        expected: String,
20        /// The scheme actually found in the URL.
21        found: String,
22    },
23    /// The URL port component could not be parsed as a `u16`.
24    InvalidPort(String),
25    /// The HTTP response head (status line + headers) was malformed.
26    InvalidHead(String),
27    /// A chunked-transfer size line was malformed.
28    InvalidChunkSize(String),
29    /// A chunked-transfer body ended before a complete chunk or trailer section.
30    TruncatedChunk,
31    /// A chunked-transfer chunk or trailer section used the wrong delimiter.
32    InvalidChunkDelimiter,
33    /// A length-prefixed or buffered payload exceeded the caller's size limit.
34    OversizeBody(usize),
35}
36
37impl core::fmt::Display for NetError {
38    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
39        match self {
40            Self::MalformedUrl(url) => write!(formatter, "malformed url {url}"),
41            Self::UnsupportedScheme(scheme) => write!(formatter, "unsupported url scheme {scheme}"),
42            Self::UnexpectedScheme { expected, found } => {
43                write!(formatter, "expected url scheme {expected}, found {found}")
44            }
45            Self::InvalidPort(url) => write!(formatter, "invalid port in url {url}"),
46            Self::InvalidHead(detail) => write!(formatter, "invalid http head: {detail}"),
47            Self::InvalidChunkSize(detail) => write!(formatter, "invalid chunk size: {detail}"),
48            Self::TruncatedChunk => write!(formatter, "truncated chunked body"),
49            Self::InvalidChunkDelimiter => write!(formatter, "invalid chunked body delimiter"),
50            Self::OversizeBody(limit) => {
51                write!(formatter, "payload exceeded size limit of {limit} bytes")
52            }
53        }
54    }
55}
56
57impl core::error::Error for NetError {}