use core::fmt;
#[derive(Debug, Clone, Copy)]
pub struct InvalidCapacity;
impl fmt::Display for InvalidCapacity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Invalid buffer capacity: capacity must be greater than 0")
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidCapacity {}
#[cfg(feature = "std")]
impl From<InvalidCapacity> for std::io::Error {
fn from(err: InvalidCapacity) -> Self {
std::io::Error::new(std::io::ErrorKind::OutOfMemory, err)
}
}
#[derive(Debug, Clone)]
pub enum Error<Io> {
Aead,
Io(Io),
}
impl<Io> From<Io> for Error<Io> {
fn from(err: Io) -> Self {
Self::Io(err)
}
}
impl<Io> fmt::Display for Error<Io>
where
Io: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Aead => f.write_str("AEAD error occured"),
Self::Io(io) => io.fmt(f),
}
}
}
#[cfg(feature = "std")]
impl<Io> std::error::Error for Error<Io> where Io: fmt::Display + fmt::Debug {}
#[cfg(feature = "std")]
impl<Io> From<Error<Io>> for std::io::Error
where
Io: Into<std::io::Error>,
{
fn from(err: Error<Io>) -> Self {
match err {
Error::Aead => std::io::Error::new(std::io::ErrorKind::Other, "an AEAD error occured"),
Error::Io(err) => err.into(),
}
}
}
#[derive(Debug, Clone)]
pub struct IntoInnerError<W, Io>(W, Error<Io>);
impl<W, Io> IntoInnerError<W, Io> {
pub(crate) fn new(writer: W, error: Error<Io>) -> Self {
Self(writer, error)
}
pub fn error(&self) -> &Error<Io> {
&self.1
}
pub fn into_writer(self) -> W {
self.0
}
pub fn into_error(self) -> Error<Io> {
self.1
}
pub fn into_parts(self) -> (W, Error<Io>) {
(self.0, self.1)
}
}
impl<W, Io> fmt::Display for IntoInnerError<W, Io>
where
Io: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error().fmt(f)
}
}
#[cfg(feature = "std")]
impl<W, Io> std::error::Error for IntoInnerError<W, Io>
where
Io: fmt::Display + fmt::Debug,
W: fmt::Debug,
{
}
#[cfg(feature = "std")]
impl<W, Io> From<IntoInnerError<W, Io>> for std::io::Error
where
Io: Into<std::io::Error>,
{
fn from(err: IntoInnerError<W, Io>) -> Self {
err.into_error().into()
}
}