use std::fmt;
use serde::{Deserialize, Serialize};
use crate::definition::ReasonKind;
use crate::judgment::Unresolved;
use crate::limits::RECORD_V1;
use crate::schema;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RuntimeReasonDoc {
pub schema: String,
pub reason: Unresolved,
}
crate::document::document!(RuntimeReasonDoc, schema::RUNTIME_REASON, RECORD_V1);
pub fn is_runtime_reason(kind: ReasonKind) -> bool {
matches!(
kind,
ReasonKind::BackendUnavailable
| ReasonKind::InvalidBackendOutput
| ReasonKind::BudgetExhausted
| ReasonKind::DeadlineExceeded
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NotARuntimeReason(pub ReasonKind);
impl fmt::Display for NotARuntimeReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} is not a runtime reason", self.0.name())
}
}
impl std::error::Error for NotARuntimeReason {}
impl RuntimeReasonDoc {
pub fn new(reason: Unresolved) -> Result<Self, NotARuntimeReason> {
let doc = Self {
schema: schema::RUNTIME_REASON.into(),
reason,
};
doc.check()?;
Ok(doc)
}
pub fn check(&self) -> Result<(), NotARuntimeReason> {
let k = self.reason.kind();
if is_runtime_reason(k) {
Ok(())
} else {
Err(NotARuntimeReason(k))
}
}
}