imstr/
error.rs

1//! Error types
2pub use std::string::{FromUtf16Error, FromUtf8Error};
3
4/// A possible error when slicing a [`ImString`](crate::ImString).
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum SliceError {
7    /// Start offset out of bounds.
8    StartOutOfBounds,
9    /// End offset out of bounds.
10    EndOutOfBounds,
11    /// End index smaller than start index.
12    EndBeforeStart,
13    /// Start index not on [`char`] boundary.
14    StartNotAligned,
15    /// End index not on [`char`] boundary.
16    EndNotAligned,
17}
18
19impl std::fmt::Display for SliceError {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            Self::StartOutOfBounds => write!(f, "start offset out of bounds"),
23            Self::StartNotAligned => write!(f, "start offset in multibyte UTF-8 sequence"),
24            Self::EndOutOfBounds => write!(f, "end offset out of bounds"),
25            Self::EndNotAligned => write!(f, "end offset in multibyte UTF-8 sequence"),
26            Self::EndBeforeStart => write!(f, "end offset before start offset"),
27        }
28    }
29}
30
31#[test]
32fn slice_error_traits() {
33    use SliceError::*;
34    let errors = [
35        StartOutOfBounds,
36        EndOutOfBounds,
37        EndBeforeStart,
38        StartNotAligned,
39        EndNotAligned,
40    ];
41
42    for error in errors.into_iter() {
43        // implements clone
44        let new = error.clone();
45        // implements partial eq
46        assert_eq!(error, new);
47        // implements debug
48        format!("{error:?}");
49        // implements display
50        format!("{new}");
51    }
52}