more_errors 0.1.0

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

/// Error representing an index greater than length situation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IndexGreaterThanLenError {
    use_check_to_instantiate: (),
}

impl IndexGreaterThanLenError {
    /// Checks if the given `index` is greater than `len`.
    ///
    /// Returns `Ok(())` if `index` is within bounds, otherwise `Err(IndexGreaterThanLenError)`.
    pub fn check(index: usize, len: usize) -> Result<(), Self> {
        if index > len {
            Err(IndexGreaterThanLenError {
                use_check_to_instantiate: (),
            })
        } else {
            Ok(())
        }
    }
}

impl Display for IndexGreaterThanLenError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:#?}:/n The index was greater than the len.")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_index_greater_than_len_error() {
        // Valid case
        assert!(IndexGreaterThanLenError::check(5, 5).is_ok());

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