use crate::{EPSILON, ExpirationDate};
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
impl Hash for ExpirationDate {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Self::Days(d) => {
state.write_u8(0);
d.hash(state);
}
Self::DateTime(dt) => {
state.write_u8(1);
dt.timestamp().hash(state);
dt.timestamp_subsec_nanos().hash(state);
}
}
}
}
impl PartialEq for ExpirationDate {
#[inline]
fn eq(&self, other: &Self) -> bool {
match (self.get_days(), other.get_days()) {
(Ok(s), Ok(o)) => (s.to_dec() - o.to_dec()).abs() < EPSILON,
_ => false,
}
}
}
impl Eq for ExpirationDate {}
impl PartialOrd for ExpirationDate {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ExpirationDate {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
match (self.get_days(), other.get_days()) {
(Ok(self_days), Ok(other_days)) => self_days.cmp(&other_days),
(Err(self_err), Err(other_err)) => self_err.to_string().cmp(&other_err.to_string()),
(Err(_), Ok(_)) => Ordering::Less,
(Ok(_), Err(_)) => Ordering::Greater,
}
}
}