Add Display impl for OutOfBounds

This commit is contained in:
Robert Habermeier 2016-05-20 15:19:26 -04:00
parent 852155959d
commit cf9b6e9e07

View File

@ -57,7 +57,6 @@ pub enum UtilError {
BadSize, BadSize,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
/// Error indicating an expected value was not found. /// Error indicating an expected value was not found.
pub struct Mismatch<T: fmt::Debug> { pub struct Mismatch<T: fmt::Debug> {
@ -78,6 +77,27 @@ pub struct OutOfBounds<T: fmt::Debug> {
pub found: T, pub found: T,
} }
impl<T: fmt::Debug + fmt::Display> fmt::Display for OutOfBounds<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match (self.min.as_ref(), self.max.as_ref()) {
(Some(min), Some(max)) => {
format!("Min={}, Max={}", min, max)
}
(Some(min), _) => {
format!("Min={}", min)
}
(_, Some(max)) => {
format!("Max={}", max)
}
(None, None) => {
"".into()
}
};
f.write_fmt(format_args!("Value {} out of bounds. {}", self.found, msg))
}
}
impl From<FromHexError> for UtilError { impl From<FromHexError> for UtilError {
fn from(err: FromHexError) -> UtilError { fn from(err: FromHexError) -> UtilError {
UtilError::FromHex(err) UtilError::FromHex(err)