use super::BoundaryFace;
use crate::model::ModelError;
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum EngineError {
Model(ModelError),
Soil {
cell: Option<usize>,
reason: String,
retryable: bool,
},
LandSource {
cell: Option<usize>,
reason: String,
},
Boundary {
face: BoundaryFace,
reason: String,
},
Rejected {
operation: &'static str,
reason: String,
},
}
impl EngineError {
#[doc(hidden)]
pub fn rejected(operation: &'static str, reason: impl Into<String>) -> Self {
Self::Rejected {
operation,
reason: reason.into(),
}
}
}
impl From<ModelError> for EngineError {
fn from(value: ModelError) -> Self {
Self::Model(value)
}
}
impl From<numr::error::Error> for EngineError {
fn from(value: numr::error::Error) -> Self {
Self::rejected(
"numr execution",
format!("{value}: inspect tensor inputs and selected runtime support"),
)
}
}
impl fmt::Display for EngineError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Model(error) => error.fmt(f),
Self::Soil { cell, reason, .. } => write!(f, "soil cell={cell:?} rejected: {reason}"),
Self::LandSource { cell, reason } => {
write!(f, "land source cell={cell:?} rejected: {reason}")
}
Self::Boundary { face, reason } => write!(f, "boundary {face:?} rejected: {reason}"),
Self::Rejected { operation, reason } => write!(f, "{operation} rejected: {reason}"),
}
}
}
impl std::error::Error for EngineError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Model(error) => Some(error),
Self::Rejected { .. }
| Self::Boundary { .. }
| Self::Soil { .. }
| Self::LandSource { .. } => None,
}
}
}