From cf9b6e9e07339f8180d63a869adee7cd0cface51 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 20 May 2016 15:19:26 -0400 Subject: [PATCH] Add Display impl for OutOfBounds --- util/src/error.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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)