1use core::{fmt, str::Utf8Error};
2#[cfg(feature = "std")]
3use std::error::Error;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub struct LenNotEqual;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub struct ReplacementTooLong;
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum ReplaceWithPadError {
16 InvalidPad(Utf8Error),
18 ReplacementLen(ReplacementTooLong),
20}
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub enum ReplaceWithPadCharError {
25 PadCharTooLong,
27 ReplacementLen(ReplacementTooLong),
29}
30
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum TryFromStrError {
34 Empty,
36 MultipleChars,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum TryFromBytesError {
43 Utf8(Utf8Error),
45 Length(TryFromStrError),
47}
48
49impl ReplaceWithPadError {
50 pub(crate) const CHAR_LEN: Self = Self::ReplacementLen(ReplacementTooLong);
51}
52
53impl From<Utf8Error> for ReplaceWithPadError {
54 #[inline]
55 fn from(value: Utf8Error) -> Self {
56 Self::InvalidPad(value)
57 }
58}
59
60impl ReplaceWithPadCharError {
61 pub(crate) const CHAR_LEN: Self = Self::ReplacementLen(ReplacementTooLong);
62}
63
64impl From<Utf8Error> for TryFromBytesError {
65 #[inline]
66 fn from(value: Utf8Error) -> Self {
67 Self::Utf8(value)
68 }
69}
70
71impl From<TryFromStrError> for TryFromBytesError {
72 #[inline]
73 fn from(value: TryFromStrError) -> Self {
74 Self::Length(value)
75 }
76}
77
78impl fmt::Display for LenNotEqual {
79 #[inline]
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 "Replacement character is not the same length as the character!".fmt(f)
82 }
83}
84
85impl fmt::Display for ReplacementTooLong {
86 #[inline]
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 "Replacement character longer than the character!".fmt(f)
89 }
90}
91
92impl fmt::Display for ReplaceWithPadError {
93 #[inline]
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 match self {
96 Self::InvalidPad(_) => "Pad character is not valid UTF-8!".fmt(f),
97 Self::ReplacementLen(error) => error.fmt(f),
98 }
99 }
100}
101
102impl fmt::Display for ReplaceWithPadCharError {
103 #[inline]
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 match self {
106 Self::PadCharTooLong => "Pad character must be one byte long!".fmt(f),
107 Self::ReplacementLen(error) => error.fmt(f),
108 }
109 }
110}
111
112impl fmt::Display for TryFromStrError {
113 #[inline]
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 match self {
116 Self::Empty => "String slice is empty!",
117 Self::MultipleChars => "String slice contains multiple characters!",
118 }
119 .fmt(f)
120 }
121}
122
123impl fmt::Display for TryFromBytesError {
124 #[inline]
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126 match self {
127 Self::Utf8(error) => error.fmt(f),
128 Self::Length(error) => error.fmt(f),
129 }
130 }
131}
132
133#[cfg(feature = "std")]
134impl Error for LenNotEqual {}
135
136#[cfg(feature = "std")]
137impl Error for ReplacementTooLong {}
138
139#[cfg(feature = "std")]
140impl Error for ReplaceWithPadError {
141 #[inline]
142 fn source(&self) -> Option<&(dyn Error + 'static)> {
143 Some(match self {
144 Self::InvalidPad(error) => error,
145 Self::ReplacementLen(error) => error,
146 })
147 }
148}
149
150#[cfg(feature = "std")]
151impl Error for ReplaceWithPadCharError {
152 #[inline]
153 fn source(&self) -> Option<&(dyn Error + 'static)> {
154 match self {
155 Self::PadCharTooLong => None,
156 Self::ReplacementLen(error) => Some(error),
157 }
158 }
159}
160
161#[cfg(feature = "std")]
162impl std::error::Error for TryFromBytesError {
163 #[inline]
164 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
165 Some(match self {
166 Self::Utf8(error) => error,
167 Self::Length(error) => error,
168 })
169 }
170}
171
172#[cfg(feature = "std")]
173impl std::error::Error for TryFromStrError {}