Skip to main content

smart_string/pascal_string/
error.rs

1use core::fmt;
2use core::str::Utf8Error;
3
4/// An error returned when a conversion from a `&str` to a `PascalString` fails.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum TryFromStrError {
7    /// The string is too long to fit into a `PascalString`.
8    TooLong,
9}
10
11/// An error returned by insertion operations.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum InsertError {
14    /// The index is outside the string bounds.
15    OutOfBounds { idx: usize, len: usize },
16    /// The index is not on a UTF-8 character boundary.
17    NotCharBoundary { idx: usize },
18    /// The result would exceed the fixed capacity.
19    TooLong,
20}
21
22/// An error returned by removal operations.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum RemoveError {
25    /// The index is outside the string bounds.
26    OutOfBounds { idx: usize, len: usize },
27    /// The index is not on a UTF-8 character boundary.
28    NotCharBoundary { idx: usize },
29}
30
31/// An error returned by replace-range operations.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum ReplaceRangeError {
34    /// The range bounds are invalid or out of bounds.
35    OutOfBounds {
36        start: usize,
37        end: usize,
38        len: usize,
39    },
40    /// The range start or end is not on a UTF-8 character boundary.
41    NotCharBoundary { idx: usize },
42    /// The result would exceed the fixed capacity.
43    TooLong,
44}
45
46/// An error returned when a conversion from a `&[u8]` to a `PascalString` fails.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum TryFromBytesError {
49    /// The string is too long to fit into a `PascalString`.
50    TooLong,
51    /// The string is not valid UTF-8.
52    Utf8Error(Utf8Error),
53}
54
55impl From<Utf8Error> for TryFromBytesError {
56    fn from(e: Utf8Error) -> Self {
57        TryFromBytesError::Utf8Error(e)
58    }
59}
60
61impl From<TryFromStrError> for TryFromBytesError {
62    fn from(e: TryFromStrError) -> Self {
63        match e {
64            TryFromStrError::TooLong => TryFromBytesError::TooLong,
65        }
66    }
67}
68
69impl fmt::Display for TryFromStrError {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        match self {
72            TryFromStrError::TooLong => f.write_str("string too long"),
73        }
74    }
75}
76
77impl fmt::Display for InsertError {
78    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79        match self {
80            InsertError::OutOfBounds { idx, len } => {
81                write!(f, "index out of bounds: idx={idx}, len={len}")
82            }
83            InsertError::NotCharBoundary { idx } => {
84                write!(f, "index is not a char boundary: idx={idx}")
85            }
86            InsertError::TooLong => f.write_str("string too long"),
87        }
88    }
89}
90
91impl fmt::Display for RemoveError {
92    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93        match self {
94            RemoveError::OutOfBounds { idx, len } => {
95                write!(f, "index out of bounds: idx={idx}, len={len}")
96            }
97            RemoveError::NotCharBoundary { idx } => {
98                write!(f, "index is not a char boundary: idx={idx}")
99            }
100        }
101    }
102}
103
104impl fmt::Display for ReplaceRangeError {
105    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106        match self {
107            ReplaceRangeError::OutOfBounds { start, end, len } => {
108                write!(
109                    f,
110                    "range out of bounds: start={start}, end={end}, len={len}"
111                )
112            }
113            ReplaceRangeError::NotCharBoundary { idx } => {
114                write!(f, "index is not a char boundary: idx={idx}")
115            }
116            ReplaceRangeError::TooLong => f.write_str("string too long"),
117        }
118    }
119}
120
121impl fmt::Display for TryFromBytesError {
122    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123        match self {
124            TryFromBytesError::TooLong => f.write_str("string too long"),
125            TryFromBytesError::Utf8Error(e) => e.fmt(f),
126        }
127    }
128}
129
130/// An error returned by `try_split_off`.
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum SplitOffError {
133    /// The index is outside the string bounds.
134    OutOfBounds { at: usize, len: usize },
135    /// The index is not on a UTF-8 character boundary.
136    NotCharBoundary { at: usize },
137}
138
139impl fmt::Display for SplitOffError {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        match self {
142            SplitOffError::OutOfBounds { at, len } => {
143                write!(f, "index out of bounds: at={at}, len={len}")
144            }
145            SplitOffError::NotCharBoundary { at } => {
146                write!(f, "index is not a char boundary: at={at}")
147            }
148        }
149    }
150}