Skip to main content

wyvern_schema/
error.rs

1//! Validation-stage errors for schema checking.
2
3use crate::error_code::ErrorCode;
4use crate::field_name::FieldName;
5
6/// Failure while validating command JSON against the phase surface.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum ValidationError {
9    /// Schema or field-level validation failure.
10    Validation { field: FieldName, message: String },
11    /// Mode/lifecycle state failure (e.g. action outside `--interactive`).
12    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    /// Build a schema validation error for `field`.
30    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    /// Build a state error for `field`.
38    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    /// Stable exit code for this validation failure.
46    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}