use alloc::string::String;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WireError {
Truncated {
at: usize,
needed: usize,
available: usize,
},
ImpossibleLength {
at: usize,
claimed: usize,
available: usize,
},
NotUtf8 {
at: usize,
},
OutOfRange {
at: usize,
expected: String,
},
Discarded {
at: usize,
expected: String,
found: String,
},
WrongFraming {
at: usize,
expected: String,
found: String,
fits: Option<&'static str>,
},
}
impl WireError {
#[must_use]
pub fn refused(at: usize, expected: &str) -> Self {
Self::OutOfRange {
at,
expected: String::from(expected),
}
}
#[must_use]
pub const fn is_discarded(&self) -> bool {
matches!(self, Self::Discarded { .. })
}
}
impl core::fmt::Display for WireError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Truncated {
at,
needed,
available,
} => write!(f, "at {at}: {needed} bytes needed, {available} available"),
Self::ImpossibleLength {
at,
claimed,
available,
} => write!(
f,
"at {at}: a length of {claimed} with {available} bytes left"
),
Self::NotUtf8 { at } => write!(f, "at {at}: the string is not UTF-8"),
Self::Discarded {
at,
expected,
found,
} => write!(
f,
"at {at}: the sample is discarded — this type holds {expected} and the stream \
carried {found}"
),
Self::OutOfRange { at, expected } => write!(f, "at {at}: expected {expected}"),
Self::WrongFraming {
at,
expected,
found,
fits,
} => match fits {
Some(setting) => write!(
f,
"at {at}: this reader is configured for {expected} and the stream has \
{found}, which is what {setting} writes"
),
None => write!(
f,
"at {at}: this reader is configured for {expected} and the stream has \
{found}, which no setting writes"
),
},
}
}
}
impl core::error::Error for WireError {}