use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Error)]
pub enum ValidationError {
#[error("'{value}' is not a valid {type_name}")]
InvalidFormat {
type_name: &'static str,
value: String,
},
#[error("{type_name} must be between {min} and {max}, got {actual}")]
OutOfRange {
type_name: &'static str,
min: String,
max: String,
actual: String,
},
#[error("{type_name} must not be empty")]
Empty { type_name: &'static str },
#[error("{type_name}: {message}")]
Custom {
type_name: &'static str,
message: String,
},
}
impl ValidationError {
pub fn invalid(type_name: &'static str, value: &str) -> Self {
Self::InvalidFormat {
type_name,
value: value.to_owned(),
}
}
pub fn empty(type_name: &'static str) -> Self {
Self::Empty { type_name }
}
}