You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
37 lines
1.1 KiB
use std::fmt::{Display, Formatter};
|
|
|
|
/**
|
|
A common type that can be used as a validation error for a field value in a form.
|
|
*/
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub enum FieldError {
|
|
Required,
|
|
ValidIdentifier,
|
|
PositiveNumber,
|
|
WholeNumber,
|
|
Currency,
|
|
SelectOption,
|
|
Custom(&'static str),
|
|
}
|
|
|
|
impl Display for FieldError {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
FieldError::Required => write!(f, "Requires a value"),
|
|
FieldError::ValidIdentifier => write!(f, "Invalid identifier"),
|
|
FieldError::PositiveNumber => write!(f, "Requires a positive number"),
|
|
FieldError::WholeNumber => write!(f, "Requires a whole number"),
|
|
FieldError::Currency => write!(f, "Requires a dollar amount"),
|
|
FieldError::SelectOption => write!(f, "Requires a valid selection"),
|
|
FieldError::Custom(s) => write!(f, "{}", s),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<&'static str> for FieldError {
|
|
fn from(s: &'static str) -> Self {
|
|
FieldError::Custom(s)
|
|
}
|
|
}
|
|
|