Skip to main content

cheetah_string/
error.rs

1use core::fmt;
2use core::str::Utf8Error;
3
4/// Errors that can occur during CheetahString operations
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum Error {
7    /// UTF-8 validation failed
8    Utf8Error(Utf8Error),
9    /// Index out of bounds
10    IndexOutOfBounds { index: usize, len: usize },
11    /// Invalid range where the start is greater than the end
12    InvalidRange { start: usize, end: usize },
13    /// Invalid character boundary
14    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
50/// Result type for CheetahString operations
51pub type Result<T> = core::result::Result<T, Error>;