smart_string/pascal_string/
error.rs1use core::fmt;
2use core::str::Utf8Error;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum TryFromStrError {
7 TooLong,
9}
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum InsertError {
14 OutOfBounds { idx: usize, len: usize },
16 NotCharBoundary { idx: usize },
18 TooLong,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum RemoveError {
25 OutOfBounds { idx: usize, len: usize },
27 NotCharBoundary { idx: usize },
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum ReplaceRangeError {
34 OutOfBounds {
36 start: usize,
37 end: usize,
38 len: usize,
39 },
40 NotCharBoundary { idx: usize },
42 TooLong,
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum TryFromBytesError {
49 TooLong,
51 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum SplitOffError {
133 OutOfBounds { at: usize, len: usize },
135 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}