1use crate::error_code::ErrorCode;
4use crate::field_name::FieldName;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum ValidationError {
9 Validation { field: FieldName, message: String },
11 State { field: FieldName, message: String },
13}
14
15impl std::fmt::Display for ValidationError {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 match self {
18 Self::Validation { field, message } => {
19 write!(f, "validation error ({field}): {message}")
20 }
21 Self::State { field, message } => write!(f, "state error ({field}): {message}"),
22 }
23 }
24}
25
26impl std::error::Error for ValidationError {}
27
28impl ValidationError {
29 pub(crate) fn validation(field: impl Into<FieldName>, message: impl Into<String>) -> Self {
31 Self::Validation {
32 field: field.into(),
33 message: message.into(),
34 }
35 }
36
37 pub(crate) fn state(field: impl Into<FieldName>, message: impl Into<String>) -> Self {
39 Self::State {
40 field: field.into(),
41 message: message.into(),
42 }
43 }
44
45 pub fn exit_code(&self) -> i32 {
47 match self {
48 Self::Validation { .. } => ErrorCode::ValidationError.exit_code(),
49 Self::State { .. } => ErrorCode::StateError.exit_code(),
50 }
51 }
52}