use std::fmt::{Display, Formatter, Error};
use enums::FormatDesc;
#[derive(Debug)]
pub enum ValidationError
{
CannotParse(Option<FormatDesc>),
DurationOverflow(Option<String>, Option<String>),
DurationUnderrun(Option<String>, Option<String>),
IncorrectType(&'static str),
Long(Option<usize>, Option<usize>),
Overflow(Option<i64>, Option<i64>),
Short(Option<usize>, Option<usize>),
Underrun(Option<i64>, Option<i64>)
}
impl Display for ValidationError
{
fn fmt(
&self,
fmt : &mut Formatter
) -> Result<(), Error>
{
match *self
{
ValidationError::CannotParse(ref desc) => {
if let Some(desc) = desc.as_ref()
{
write!(fmt, "{}", desc)
}
else
{
write!(fmt, "Could not be parsed.")
}
},
ValidationError::DurationOverflow(ref min, ref max) => {
let max = max.as_ref().unwrap();
if let Some(min) = min.as_ref()
{
write!(fmt, "Duration must be between {} and {}, inclusive.", min, max)
}
else
{
write!(fmt, "Duration must be less than or equal to {}.", max)
}
},
ValidationError::DurationUnderrun(ref min, ref max) => {
let min = min.as_ref().unwrap();
if let Some(max) = max.as_ref()
{
write!(fmt, "Duration must be between {} and {}, inclusive.", min, max)
}
else
{
write!(fmt, "Duration must be greater than or equal to {}.", min)
}
},
ValidationError::IncorrectType(data) => {
write!(fmt, "Type must be a {}.", data)
},
ValidationError::Long(ref min, ref max) => {
let max = max.as_ref().unwrap();
if let Some(min) = min.as_ref()
{
write!(fmt, "String length must be between {} and {}, inclusive.", min, max)
}
else
{
write!(fmt, "String length must be less than or equal to {}.", max)
}
},
ValidationError::Overflow(ref min, ref max) => {
let max = max.as_ref().unwrap();
if let Some(min) = min.as_ref()
{
write!(fmt, "Value must be between {} and {}, inclusive.", min, max)
}
else
{
write!(fmt, "Value must be less than or equal to {}.", max)
}
},
ValidationError::Short(ref min, ref max) => {
let min = min.as_ref().unwrap();
if let Some(max) = max.as_ref()
{
write!(fmt, "String length must be between {} and {}, inclusive.", min, max)
}
else
{
write!(fmt, "String length must be greater than or equal to {}.", min)
}
},
ValidationError::Underrun(ref min, ref max) => {
let min = min.as_ref().unwrap();
if let Some(max) = max.as_ref()
{
write!(fmt, "Value must be between {} and {}, inclusive.", min, max)
}
else
{
write!(fmt, "Value must be greater than or equal to {}.", min)
}
}
}
}
}