Add Display impl for OutOfBounds

This commit is contained in:
Robert Habermeier 2016-05-20 15:19:26 -04:00
parent 852155959d
commit cf9b6e9e07
1 changed files with 21 additions and 1 deletions

View File

@ -57,7 +57,6 @@ pub enum UtilError {
BadSize,
}
#[derive(Debug, PartialEq, Eq)]
/// Error indicating an expected value was not found.
pub struct Mismatch<T: fmt::Debug> {
@ -78,6 +77,27 @@ pub struct OutOfBounds<T: fmt::Debug> {
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 {
fn from(err: FromHexError) -> UtilError {
UtilError::FromHex(err)