1use core::fmt;
2
3#[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#[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#[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 pub fn error(&self) -> &Error<Io> {
80 &self.1
81 }
82
83 pub fn into_writer(self) -> W {
86 self.0
87 }
88
89 pub fn into_error(self) -> Error<Io> {
93 self.1
94 }
95
96 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}