#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResolutionReason {
None,
StatusCodeOutOfRange,
LatencyExceeded,
SchemaValidationFailed,
RequiredFieldsMissing,
BodyTooShort,
HashMismatch,
EvidenceUnavailable,
GeneralRejection,
Custom(u16),
}
pub const CUSTOM_REASON_BASE: u16 = 256;
impl From<ResolutionReason> for u16 {
fn from(reason: ResolutionReason) -> u16 {
match reason {
ResolutionReason::None => 0,
ResolutionReason::StatusCodeOutOfRange => 1,
ResolutionReason::LatencyExceeded => 2,
ResolutionReason::SchemaValidationFailed => 3,
ResolutionReason::RequiredFieldsMissing => 4,
ResolutionReason::BodyTooShort => 5,
ResolutionReason::HashMismatch => 6,
ResolutionReason::EvidenceUnavailable => 7,
ResolutionReason::GeneralRejection => 255,
ResolutionReason::Custom(code) => code,
}
}
}
impl From<u16> for ResolutionReason {
fn from(code: u16) -> Self {
match code {
0 => Self::None,
1 => Self::StatusCodeOutOfRange,
2 => Self::LatencyExceeded,
3 => Self::SchemaValidationFailed,
4 => Self::RequiredFieldsMissing,
5 => Self::BodyTooShort,
6 => Self::HashMismatch,
7 => Self::EvidenceUnavailable,
255 => Self::GeneralRejection,
code => Self::Custom(code),
}
}
}
impl std::fmt::Display for ResolutionReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::None => write!(f, "none"),
Self::StatusCodeOutOfRange => write!(f, "status_code_out_of_range"),
Self::LatencyExceeded => write!(f, "latency_exceeded"),
Self::SchemaValidationFailed => write!(f, "schema_validation_failed"),
Self::RequiredFieldsMissing => write!(f, "required_fields_missing"),
Self::BodyTooShort => write!(f, "body_too_short"),
Self::HashMismatch => write!(f, "hash_mismatch"),
Self::EvidenceUnavailable => write!(f, "evidence_unavailable"),
Self::GeneralRejection => write!(f, "general_rejection"),
Self::Custom(code) => write!(f, "custom({})", code),
}
}
}
impl ResolutionReason {
pub fn is_standard(&self) -> bool {
!matches!(self, Self::Custom(_))
}
pub fn is_custom(&self) -> bool {
matches!(self, Self::Custom(_))
}
pub fn code(&self) -> u16 {
u16::from(*self)
}
}