use std::time::{Duration, SystemTime};
use crate::core::Match;
use super::Mismatch;
#[derive(Debug)]
pub struct ApproxEqTimeMatcher {
expected: SystemTime,
threshold: Duration,
}
impl ApproxEqTimeMatcher {
pub fn new(expected: SystemTime, threshold: Duration) -> Self {
Self {
expected,
threshold,
}
}
}
impl Match<SystemTime> for ApproxEqTimeMatcher {
type Fail = Mismatch<SystemTime, SystemTime>;
fn matches(&mut self, actual: &SystemTime) -> crate::Result<bool> {
match actual.cmp(&self.expected) {
std::cmp::Ordering::Less => {
Ok(self.expected.duration_since(*actual)? <= self.threshold)
}
std::cmp::Ordering::Equal => Ok(true),
std::cmp::Ordering::Greater => {
Ok(actual.duration_since(self.expected)? <= self.threshold)
}
}
}
fn fail(self, actual: SystemTime) -> Self::Fail {
Mismatch {
expected: self.expected,
actual,
}
}
}