more_errors 0.1.0

Provides reusable errors.
Documentation
use std::{
    error::Error,
    fmt::{self, Display},
};

/// Error representing an index out of bounds situation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IndexOutOfBoundsError {
    use_check_to_instantiate: (),
}

impl IndexOutOfBoundsError {
    /// Checks if the given `index` is within bounds of `len`.
    ///
    /// Returns `Ok(())` if `index` is within bounds, otherwise `Err(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() {
        // Valid case
        assert!(IndexOutOfBoundsError::check(4, 5).is_ok());

        // Invalid case
        assert!(IndexOutOfBoundsError::check(5, 5).is_err());
    }
}