diff options
| author | Vincent Prouillet | 2017-03-25 11:54:19 +0900 |
|---|---|---|
| committer | GitHub | 2017-03-25 11:54:19 +0900 |
| commit | 9b76d48ed3f5d1ed6b376c8c1022bebe2095e96a (patch) | |
| tree | 4c96eb4fcef09964802edee0b9ed77b33ba6d483 | |
| parent | f42c43bb856484fe20df79c98fd72b5aa532e42e (diff) | |
| parent | 7227d46a6be8a977fdf5b4171afef29dc0654883 (diff) | |
| download | validator-9b76d48ed3f5d1ed6b376c8c1022bebe2095e96a.tar.bz2 | |
Merge pull request #18 from theduke/master
Implement Error for Errors.
| -rw-r--r-- | validator/src/types.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/validator/src/types.rs b/validator/src/types.rs index 5b96cfe..0a7c2fa 100644 --- a/validator/src/types.rs +++ b/validator/src/types.rs @@ -1,6 +1,6 @@ +use std::{self, fmt}; use std::collections::HashMap; - #[derive(Debug)] pub struct Errors(HashMap<String, Vec<String>>); @@ -22,6 +22,33 @@ impl Errors { } } +impl fmt::Display for Errors { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "Validation failed:\n")?; + for (field, errs) in &self.0 { + write!(fmt, " {}: [", field)?; + + let last = errs.len() - 1; + for (index, err) in errs.iter().enumerate() { + write!(fmt, "{}", err)?; + if index < last { write!(fmt, ", ")? } + } + write!(fmt, "]\n")?; + } + Ok(()) + } +} + +impl std::error::Error for Errors { + fn description(&self) -> &str { + "validation failed" + } + + fn cause(&self) -> Option<&std::error::Error> { + None + } +} + pub trait Validate { fn validate(&self) -> Result<(), Errors>; } |
