mut_str/
errors.rs

1use core::{fmt, str::Utf8Error};
2#[cfg(feature = "std")]
3use std::error::Error;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6/// Length not equal error can occur if replacing a string or a character with one of a different length without padding.
7pub struct LenNotEqual;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10/// Replacement too long error can occur if replacing a string or a character with a longer one.
11pub struct ReplacementTooLong;
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14/// Replace with pad error can occur when replacing a string or a character with a byte pad.
15pub enum ReplaceWithPadError {
16    /// The given pad is not valid UTF-8.
17    InvalidPad(Utf8Error),
18    /// The replacement is longer than the original.
19    ReplacementLen(ReplacementTooLong),
20}
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23/// Replace with pad [`prim@char`] error can occur when replacing a string or a character with a [`prim@char`] pad.
24pub enum ReplaceWithPadCharError {
25    /// The given pad is more than one byte long when UTF-8 encoded.
26    PadCharTooLong,
27    /// The replacement is longer than the original.
28    ReplacementLen(ReplacementTooLong),
29}
30
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32/// Try from [`prim@str`] error can occur when trying to convert a [`prim@str`] to a [`crate::Char`].
33pub enum TryFromStrError {
34    /// The [`prim@str`] contains no characters.
35    Empty,
36    /// The [`prim@str`] contains multiple characters.
37    MultipleChars,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41/// Try from bytes error can occur when trying to convert a byte slice (`[u8]`) to a [`crate::Char`].
42pub enum TryFromBytesError {
43    /// The bytes are not valid UTF-8. See [`Utf8Error`].
44    Utf8(Utf8Error),
45    /// The bytes do not contain exactly one UTF-8 encoded character. See [`TryFromStrError`].
46    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 {}