1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Contains all of this crate errors

use core::fmt::{self, Debug, Display, Formatter};
use core::{char::DecodeUtf16Error, hash::Hash, hash::Hasher, str::EncodeUtf16, str::Utf8Error};
#[cfg(feature = "logs")]
use log::trace;

/// Every error possible when using [`ArrayString`]
///
/// [`ArrayString`]: ../struct.ArrayString.html
#[derive(Copy, Clone)]
pub enum Error {
    /// Conversion between available byte slice and UTF-8 failed (invalid data or invalid utf-8 character index)
    Utf8,
    /// Conversion between available `u16` slice and string failed
    Utf16,
    /// Out of boundaries access
    OutOfBounds,
}

impl PartialEq for Error {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        use self::Error::*;
        match (self, other) {
            (Utf8, Utf8) | (Utf16, Utf16) | (OutOfBounds, OutOfBounds) => true,
            _ => false,
        }
    }
}

impl Eq for Error {}

impl Hash for Error {
    #[inline]
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        match self {
            Error::Utf8 => "Utf8".hash(hasher),
            Error::Utf16 => "Utf16".hash(hasher),
            Error::OutOfBounds => "OutOfBounds".hash(hasher),
        }
    }
}

impl Display for Error {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Error::Utf8 => write!(f, "Utf8"),
            Error::Utf16 => write!(f, "Utf16"),
            Error::OutOfBounds => write!(f, "OutOfBounds"),
        }
    }
}

impl Debug for Error {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Error::Utf8 => write!(f, "Error::Utf8"),
            Error::Utf16 => write!(f, "Error::Utf16"),
            Error::OutOfBounds => write!(f, "Error::OutOfBounds"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl From<Utf8Error> for Error {
    #[inline]
    fn from(_: Utf8Error) -> Self {
        Error::Utf8
    }
}

impl From<DecodeUtf16Error> for Error {
    #[inline]
    fn from(_: DecodeUtf16Error) -> Self {
        Error::Utf16
    }
}

impl<'a> From<EncodeUtf16<'a>> for Error {
    #[inline]
    fn from(_: EncodeUtf16) -> Self {
        Error::Utf16
    }
}

/// Error caused by invalid UTF-8 data
#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub struct Utf8;

impl Debug for Utf8 {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Utf8")
    }
}

impl Display for Utf8 {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Utf8")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Utf8 {}

impl From<Utf8Error> for Utf8 {
    #[inline]
    fn from(_: Utf8Error) -> Self {
        Utf8
    }
}

impl From<Utf8> for Error {
    #[inline]
    fn from(_: Utf8) -> Self {
        trace!("From Utf8");
        Error::Utf8
    }
}

/// Error caused by invalid UTF-16 data
#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub struct Utf16;

impl Debug for Utf16 {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Utf16")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Utf16 {}

impl Display for Utf16 {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Utf16")
    }
}

impl From<Utf16> for Error {
    #[inline]
    fn from(_: Utf16) -> Self {
        trace!("From Utf16");
        Error::Utf16
    }
}

impl From<DecodeUtf16Error> for Utf16 {
    #[inline]
    fn from(_: DecodeUtf16Error) -> Self {
        Utf16
    }
}

impl<'a> From<EncodeUtf16<'a>> for Utf16 {
    #[inline]
    fn from(_: EncodeUtf16) -> Self {
        Utf16
    }
}

/// Error caused by out of bounds access to [`ArrayString`]
///
/// [`ArrayString`]: ../struct.ArrayString.html
#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub struct OutOfBounds;

impl Debug for OutOfBounds {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "OutOfBounds")
    }
}

impl Display for OutOfBounds {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "OutOfBounds")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for OutOfBounds {}

impl From<OutOfBounds> for Error {
    #[inline]
    fn from(_: OutOfBounds) -> Self {
        trace!("From OutOfBounds");
        Error::OutOfBounds
    }
}