use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
UnexpectedEof { needed: usize, remaining: usize },
TrailingBytes { remaining: usize },
InvalidBool { value: u32 },
InvalidDiscriminant { type_name: &'static str, value: i32 },
InvalidPadding { value: u8 },
LengthLimitExceeded { len: usize, max: usize },
LengthOutOfRange { len: usize },
InvalidUtf8,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedEof { needed, remaining } => {
write!(
f,
"unexpected end of XDR input: need {needed} bytes, have {remaining}"
)
}
Self::TrailingBytes { remaining } => {
write!(f, "XDR input has {remaining} trailing bytes")
}
Self::InvalidBool { value } => {
write!(f, "invalid XDR bool value {value}")
}
Self::InvalidDiscriminant { type_name, value } => {
write!(f, "invalid XDR discriminant {value} for {type_name}")
}
Self::InvalidPadding { value } => {
write!(f, "invalid non-zero XDR padding byte {value}")
}
Self::LengthLimitExceeded { len, max } => {
write!(f, "XDR length {len} exceeds limit {max}")
}
Self::LengthOutOfRange { len } => {
write!(f, "XDR length {len} cannot be represented as u32")
}
Self::InvalidUtf8 => f.write_str("XDR string is not valid UTF-8"),
}
}
}
impl std::error::Error for Error {}