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 length-prefixed or buffered payload exceeded the caller's size limit.
28    OversizeBody(usize),
29}
30
31impl core::fmt::Display for NetError {
32    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33        match self {
34            Self::MalformedUrl(url) => write!(formatter, "malformed url {url}"),
35            Self::UnsupportedScheme(scheme) => write!(formatter, "unsupported url scheme {scheme}"),
36            Self::UnexpectedScheme { expected, found } => {
37                write!(formatter, "expected url scheme {expected}, found {found}")
38            }
39            Self::InvalidPort(url) => write!(formatter, "invalid port in url {url}"),
40            Self::InvalidHead(detail) => write!(formatter, "invalid http head: {detail}"),
41            Self::OversizeBody(limit) => {
42                write!(formatter, "payload exceeded size limit of {limit} bytes")
43            }
44        }
45    }
46}
47
48impl core::error::Error for NetError {}