stefans-utils 0.12.0

A collection of useful Rust utility functions, types, and traits.
Documentation
use core::fmt::{Debug, Display, Formatter, Result};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[cfg_attr(feature = "schemars", derive(::schemars::JsonSchema))]
pub enum Comparison {
    Less,
    LessOrEqual,
    Equal,
    Unequal,
    GreaterOrEqual,
    Greater,
}

impl Display for Comparison {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.write_str(match self {
            Comparison::Less => "less than",
            Comparison::LessOrEqual => "less than or equal to",
            Comparison::Equal => "equal to",
            Comparison::Unequal => "unequal to",
            Comparison::GreaterOrEqual => "greater than or equal to",
            Comparison::Greater => "greater than",
        })
    }
}

impl Comparison {
    pub fn compare<T: PartialOrd + PartialEq>(&self, a: &T, b: &T) -> bool {
        match self {
            Comparison::Less => a < b,
            Comparison::LessOrEqual => a <= b,
            Comparison::Equal => a == b,
            Comparison::Unequal => a != b,
            Comparison::GreaterOrEqual => a >= b,
            Comparison::Greater => a > b,
        }
    }
}