aead_io/
error.rs

1use core::fmt;
2
3/// An error which occurs when providing an invalid buffer to a
4/// [`BufReader`](crate::DecryptBufReader) or [`BufWriter`](crate::EncryptBufWriter)
5#[derive(Debug, Clone, Copy)]
6pub struct InvalidCapacity;
7
8impl fmt::Display for InvalidCapacity {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        f.write_str("Invalid buffer capacity: capacity must be greater than 0")
11    }
12}
13
14#[cfg(feature = "std")]
15impl std::error::Error for InvalidCapacity {}
16
17#[cfg(feature = "std")]
18impl From<InvalidCapacity> for std::io::Error {
19    fn from(err: InvalidCapacity) -> Self {
20        std::io::Error::new(std::io::ErrorKind::OutOfMemory, err)
21    }
22}
23
24/// An error for read/write operations with custom Error types. Mainly useful for `no_std`
25/// environments
26#[derive(Debug, Clone)]
27pub enum Error<Io> {
28    Aead,
29    Io(Io),
30}
31
32impl<Io> From<Io> for Error<Io> {
33    fn from(err: Io) -> Self {
34        Self::Io(err)
35    }
36}
37
38impl<Io> fmt::Display for Error<Io>
39where
40    Io: fmt::Display,
41{
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Self::Aead => f.write_str("AEAD error occured"),
45            Self::Io(io) => io.fmt(f),
46        }
47    }
48}
49
50#[cfg(feature = "std")]
51impl<Io> std::error::Error for Error<Io> where Io: fmt::Display + fmt::Debug {}
52
53#[cfg(feature = "std")]
54impl<Io> From<Error<Io>> for std::io::Error
55where
56    Io: Into<std::io::Error>,
57{
58    fn from(err: Error<Io>) -> Self {
59        match err {
60            Error::Aead => std::io::Error::new(std::io::ErrorKind::Other, "an AEAD error occured"),
61            Error::Io(err) => err.into(),
62        }
63    }
64}
65
66/// An error returned by `EncryptBufWriter::into_inner` which combines an error that happened
67/// while writing out the buffer, and the buffered writer object which may be used to recover
68/// from the condition.
69#[derive(Debug, Clone)]
70pub struct IntoInnerError<W, Io>(W, Error<Io>);
71
72impl<W, Io> IntoInnerError<W, Io> {
73    pub(crate) fn new(writer: W, error: Error<Io>) -> Self {
74        Self(writer, error)
75    }
76
77    /// Returns the error which caused the call to `EncryptBufWriter::into_inner()` to fail.
78    /// This error was returned when attempting to write the internal buffer.
79    pub fn error(&self) -> &Error<Io> {
80        &self.1
81    }
82
83    /// Returns the buffered writer instance which generated the error.
84    /// The returned object can be used for error recovery, such as re-inspecting the buffer.
85    pub fn into_writer(self) -> W {
86        self.0
87    }
88
89    /// Consumes the IntoInnerError and returns the error which caused the call to
90    /// `EncryptBufWriter::into_inner()` to fail. Unlike error, this can be used to obtain
91    /// ownership of the underlying error.
92    pub fn into_error(self) -> Error<Io> {
93        self.1
94    }
95
96    /// Consumes the IntoInnerError and returns the error which caused the call to
97    /// `EncryptBufWriter::into_inner()` to fail, and the underlying writer. This can be used
98    /// to simply obtain ownership of the underlying error; it can also be used for advanced
99    /// error recovery.
100    pub fn into_parts(self) -> (W, Error<Io>) {
101        (self.0, self.1)
102    }
103}
104
105impl<W, Io> fmt::Display for IntoInnerError<W, Io>
106where
107    Io: fmt::Display,
108{
109    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110        self.error().fmt(f)
111    }
112}
113
114#[cfg(feature = "std")]
115impl<W, Io> std::error::Error for IntoInnerError<W, Io>
116where
117    Io: fmt::Display + fmt::Debug,
118    W: fmt::Debug,
119{
120}
121
122#[cfg(feature = "std")]
123impl<W, Io> From<IntoInnerError<W, Io>> for std::io::Error
124where
125    Io: Into<std::io::Error>,
126{
127    fn from(err: IntoInnerError<W, Io>) -> Self {
128        err.into_error().into()
129    }
130}