pub use std::string::{FromUtf16Error, FromUtf8Error};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SliceError {
StartOutOfBounds,
EndOutOfBounds,
EndBeforeStart,
StartNotAligned,
EndNotAligned,
}
impl std::fmt::Display for SliceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::StartOutOfBounds => write!(f, "start offset out of bounds"),
Self::StartNotAligned => write!(f, "start offset in multibyte UTF-8 sequence"),
Self::EndOutOfBounds => write!(f, "end offset out of bounds"),
Self::EndNotAligned => write!(f, "end offset in multibyte UTF-8 sequence"),
Self::EndBeforeStart => write!(f, "end offset before start offset"),
}
}
}
#[test]
fn slice_error_traits() {
use SliceError::*;
let errors = [
StartOutOfBounds,
EndOutOfBounds,
EndBeforeStart,
StartNotAligned,
EndNotAligned,
];
for error in errors.into_iter() {
let new = error.clone();
assert_eq!(error, new);
format!("{error:?}");
format!("{new}");
}
}