diff options
| author | theduke | 2017-02-16 19:43:12 +0100 |
|---|---|---|
| committer | theduke | 2017-02-16 19:43:12 +0100 |
| commit | 7227d46a6be8a977fdf5b4171afef29dc0654883 (patch) | |
| tree | 4c96eb4fcef09964802edee0b9ed77b33ba6d483 | |
| parent | f42c43bb856484fe20df79c98fd72b5aa532e42e (diff) | |
| download | validator-7227d46a6be8a977fdf5b4171afef29dc0654883.tar.bz2 | |
Implement Error for Errors.
For Dispaly, it's not the prettiest output imaginable,
since it just contains error codes.
But it's still better than nothing.
| -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>; } |
