1use std::fmt;
20
21#[derive(Debug, PartialEq, Eq, Clone, Copy)]
22pub struct Mismatch<T> {
24 pub expected: T,
26 pub found: T,
28}
29
30impl<T: fmt::Display> fmt::Display for Mismatch<T> {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 f.write_fmt(format_args!("Expected {}, found {}", self.expected, self.found))
33 }
34}
35
36#[derive(Debug, PartialEq, Eq, Clone, Copy)]
37pub struct OutOfBounds<T> {
39 pub min: Option<T>,
41 pub max: Option<T>,
43 pub found: T,
45}
46
47impl<T> OutOfBounds<T> {
48 pub fn map<F, U>(self, map: F) -> OutOfBounds<U>
49 where F: Fn(T) -> U
50 {
51 OutOfBounds {
52 min: self.min.map(&map),
53 max: self.max.map(&map),
54 found: map(self.found),
55 }
56 }
57}
58
59impl<T: fmt::Display> fmt::Display for OutOfBounds<T> {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 let msg = match (self.min.as_ref(), self.max.as_ref()) {
62 (Some(min), Some(max)) => format!("Min={}, Max={}", min, max),
63 (Some(min), _) => format!("Min={}", min),
64 (_, Some(max)) => format!("Max={}", max),
65 (None, None) => "".into(),
66 };
67
68 f.write_fmt(format_args!("Value {} out of bounds. {}", self.found, msg))
69 }
70}