diff --git a/util/src/error.rs b/util/src/error.rs index 409cc0e5d..9e76e885d 100644 --- a/util/src/error.rs +++ b/util/src/error.rs @@ -57,7 +57,6 @@ pub enum UtilError { BadSize, } - #[derive(Debug, PartialEq, Eq)] /// Error indicating an expected value was not found. pub struct Mismatch { @@ -78,6 +77,27 @@ pub struct OutOfBounds { pub found: T, } +impl fmt::Display for OutOfBounds { + 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 for UtilError { fn from(err: FromHexError) -> UtilError { UtilError::FromHex(err)