use std::{
error::Error,
fmt::{self, Display},
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IndexOutOfBoundsError {
use_check_to_instantiate: (),
}
impl IndexOutOfBoundsError {
pub fn check(index: usize, len: usize) -> Result<(), Self> {
if index >= len {
Err(IndexOutOfBoundsError {
use_check_to_instantiate: (),
})
} else {
Ok(())
}
}
}
impl Display for IndexOutOfBoundsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:#?}\n Index out of bounds!")
}
}
impl Error for IndexOutOfBoundsError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_index_out_of_bounds_error() {
assert!(IndexOutOfBoundsError::check(4, 5).is_ok());
assert!(IndexOutOfBoundsError::check(5, 5).is_err());
}
}