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 InvalidCharBoundary { index: usize },
13}
14
15impl fmt::Display for Error {
16 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17 match self {
18 Error::Utf8Error(e) => write!(f, "UTF-8 error: {}", e),
19 Error::IndexOutOfBounds { index, len } => {
20 write!(f, "index {} out of bounds (len: {})", index, len)
21 }
22 Error::InvalidCharBoundary { index } => {
23 write!(f, "index {} is not a char boundary", index)
24 }
25 }
26 }
27}
28
29#[cfg(feature = "std")]
30impl std::error::Error for Error {
31 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
32 match self {
33 Error::Utf8Error(e) => Some(e),
34 _ => None,
35 }
36 }
37}
38
39impl From<Utf8Error> for Error {
40 fn from(e: Utf8Error) -> Self {
41 Error::Utf8Error(e)
42 }
43}
44
45pub type Result<T> = core::result::Result<T, Error>;