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 port component could not be parsed as a `u16`.
16    InvalidPort(String),
17    /// The HTTP response head (status line + headers) was malformed.
18    InvalidHead(String),
19    /// A length-prefixed or buffered payload exceeded the caller's size limit.
20    OversizeBody(usize),
21}
22
23impl core::fmt::Display for NetError {
24    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25        match self {
26            Self::MalformedUrl(url) => write!(formatter, "malformed url {url}"),
27            Self::UnsupportedScheme(scheme) => write!(formatter, "unsupported url scheme {scheme}"),
28            Self::InvalidPort(url) => write!(formatter, "invalid port in url {url}"),
29            Self::InvalidHead(detail) => write!(formatter, "invalid http head: {detail}"),
30            Self::OversizeBody(limit) => {
31                write!(formatter, "payload exceeded size limit of {limit} bytes")
32            }
33        }
34    }
35}
36
37impl core::error::Error for NetError {}