1use core::fmt;
2use core::str::Utf8Error;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum Error {
7 Utf8Error(Utf8Error),
9 IndexOutOfBounds { index: usize, len: usize },
11 InvalidRange { start: usize, end: usize },
13 InvalidCharBoundary { index: usize },
15}
16
17impl fmt::Display for Error {
18 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19 match self {
20 Error::Utf8Error(e) => write!(f, "UTF-8 error: {}", e),
21 Error::IndexOutOfBounds { index, len } => {
22 write!(f, "index {} out of bounds (len: {})", index, len)
23 }
24 Error::InvalidRange { start, end } => {
25 write!(f, "range start {} is greater than end {}", start, end)
26 }
27 Error::InvalidCharBoundary { index } => {
28 write!(f, "index {} is not a char boundary", index)
29 }
30 }
31 }
32}
33
34#[cfg(feature = "std")]
35impl std::error::Error for Error {
36 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37 match self {
38 Error::Utf8Error(e) => Some(e),
39 _ => None,
40 }
41 }
42}
43
44impl From<Utf8Error> for Error {
45 fn from(e: Utf8Error) -> Self {
46 Error::Utf8Error(e)
47 }
48}
49
50pub type Result<T> = core::result::Result<T, Error>;