arraystring/
error.rs

1//! Contains all of this crate errors
2
3use core::fmt::{self, Debug, Display, Formatter};
4use core::{char::DecodeUtf16Error, hash::Hash, hash::Hasher, str::EncodeUtf16, str::Utf8Error};
5#[cfg(feature = "logs")]
6use log::trace;
7
8/// Every error possible when using [`ArrayString`]
9///
10/// [`ArrayString`]: ../struct.ArrayString.html
11#[derive(Copy, Clone)]
12pub enum Error {
13    /// Conversion between available byte slice and UTF-8 failed (invalid data or invalid utf-8 character index)
14    Utf8,
15    /// Conversion between available `u16` slice and string failed
16    Utf16,
17    /// Out of boundaries access
18    OutOfBounds,
19}
20
21impl PartialEq for Error {
22    #[inline]
23    fn eq(&self, other: &Self) -> bool {
24        use self::Error::*;
25        match (self, other) {
26            (Utf8, Utf8) | (Utf16, Utf16) | (OutOfBounds, OutOfBounds) => true,
27            _ => false,
28        }
29    }
30}
31
32impl Eq for Error {}
33
34impl Hash for Error {
35    #[inline]
36    fn hash<H: Hasher>(&self, hasher: &mut H) {
37        match self {
38            Error::Utf8 => "Utf8".hash(hasher),
39            Error::Utf16 => "Utf16".hash(hasher),
40            Error::OutOfBounds => "OutOfBounds".hash(hasher),
41        }
42    }
43}
44
45impl Display for Error {
46    #[inline]
47    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
48        match self {
49            Error::Utf8 => write!(f, "Utf8"),
50            Error::Utf16 => write!(f, "Utf16"),
51            Error::OutOfBounds => write!(f, "OutOfBounds"),
52        }
53    }
54}
55
56impl Debug for Error {
57    #[inline]
58    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
59        match self {
60            Error::Utf8 => write!(f, "Error::Utf8"),
61            Error::Utf16 => write!(f, "Error::Utf16"),
62            Error::OutOfBounds => write!(f, "Error::OutOfBounds"),
63        }
64    }
65}
66
67#[cfg(feature = "std")]
68impl std::error::Error for Error {}
69
70impl From<Utf8Error> for Error {
71    #[inline]
72    fn from(_: Utf8Error) -> Self {
73        Error::Utf8
74    }
75}
76
77impl From<DecodeUtf16Error> for Error {
78    #[inline]
79    fn from(_: DecodeUtf16Error) -> Self {
80        Error::Utf16
81    }
82}
83
84impl<'a> From<EncodeUtf16<'a>> for Error {
85    #[inline]
86    fn from(_: EncodeUtf16) -> Self {
87        Error::Utf16
88    }
89}
90
91/// Error caused by invalid UTF-8 data
92#[derive(Copy, Clone, Default, PartialEq, Eq)]
93pub struct Utf8;
94
95impl Debug for Utf8 {
96    #[inline]
97    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
98        write!(f, "Utf8")
99    }
100}
101
102impl Display for Utf8 {
103    #[inline]
104    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
105        write!(f, "Utf8")
106    }
107}
108
109#[cfg(feature = "std")]
110impl std::error::Error for Utf8 {}
111
112impl From<Utf8Error> for Utf8 {
113    #[inline]
114    fn from(_: Utf8Error) -> Self {
115        Utf8
116    }
117}
118
119impl From<Utf8> for Error {
120    #[inline]
121    fn from(_: Utf8) -> Self {
122        trace!("From Utf8");
123        Error::Utf8
124    }
125}
126
127/// Error caused by invalid UTF-16 data
128#[derive(Copy, Clone, Default, PartialEq, Eq)]
129pub struct Utf16;
130
131impl Debug for Utf16 {
132    #[inline]
133    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
134        write!(f, "Utf16")
135    }
136}
137
138#[cfg(feature = "std")]
139impl std::error::Error for Utf16 {}
140
141impl Display for Utf16 {
142    #[inline]
143    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
144        write!(f, "Utf16")
145    }
146}
147
148impl From<Utf16> for Error {
149    #[inline]
150    fn from(_: Utf16) -> Self {
151        trace!("From Utf16");
152        Error::Utf16
153    }
154}
155
156impl From<DecodeUtf16Error> for Utf16 {
157    #[inline]
158    fn from(_: DecodeUtf16Error) -> Self {
159        Utf16
160    }
161}
162
163impl<'a> From<EncodeUtf16<'a>> for Utf16 {
164    #[inline]
165    fn from(_: EncodeUtf16) -> Self {
166        Utf16
167    }
168}
169
170/// Error caused by out of bounds access to [`ArrayString`]
171///
172/// [`ArrayString`]: ../struct.ArrayString.html
173#[derive(Copy, Clone, Default, PartialEq, Eq)]
174pub struct OutOfBounds;
175
176impl Debug for OutOfBounds {
177    #[inline]
178    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
179        write!(f, "OutOfBounds")
180    }
181}
182
183impl Display for OutOfBounds {
184    #[inline]
185    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
186        write!(f, "OutOfBounds")
187    }
188}
189
190#[cfg(feature = "std")]
191impl std::error::Error for OutOfBounds {}
192
193impl From<OutOfBounds> for Error {
194    #[inline]
195    fn from(_: OutOfBounds) -> Self {
196        trace!("From OutOfBounds");
197        Error::OutOfBounds
198    }
199}