use core::fmt;
#[cfg(feature = "std")]
use std::io;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
#[cfg(feature = "std")]
Io(io::Error),
OutputBufferTooSmall(usize),
IntegerOverflow,
}
impl fmt::Display for Error {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
#[cfg(feature = "std")]
Self::Io(error) => write!(fmt, "I/O error: {error}"),
Self::OutputBufferTooSmall(len) => write!(
fmt,
"output buffer error: too small (requires at least {len:#x} byte(s)"
),
Self::IntegerOverflow => write!(fmt, "integer overflow"),
}
}
}
#[cfg(feature = "std")]
impl From<io::Error> for Error {
#[inline]
fn from(error: io::Error) -> Self {
Self::Io(error)
}
}
impl Error {
pub(super) const fn buffer_too_small(n: usize) -> Self {
Self::OutputBufferTooSmall(n)
}
#[cfg(feature = "std")]
#[must_use]
#[inline]
pub const fn io(&self) -> Option<&io::Error> {
if let Self::Io(err) = self {
Some(err)
} else {
None
}
}
}