use crate::error_code::ErrorCode;
use crate::field_name::FieldName;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationError {
Validation { field: FieldName, message: String },
State { field: FieldName, message: String },
}
impl std::fmt::Display for ValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Validation { field, message } => {
write!(f, "validation error ({field}): {message}")
}
Self::State { field, message } => write!(f, "state error ({field}): {message}"),
}
}
}
impl std::error::Error for ValidationError {}
impl ValidationError {
pub(crate) fn validation(field: impl Into<FieldName>, message: impl Into<String>) -> Self {
Self::Validation {
field: field.into(),
message: message.into(),
}
}
pub(crate) fn state(field: impl Into<FieldName>, message: impl Into<String>) -> Self {
Self::State {
field: field.into(),
message: message.into(),
}
}
pub fn exit_code(&self) -> i32 {
match self {
Self::Validation { .. } => ErrorCode::ValidationError.exit_code(),
Self::State { .. } => ErrorCode::StateError.exit_code(),
}
}
}