use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StructureInputErrorKind {
OperatingPoint,
Definition,
Identity,
Datum,
HeadConvention,
UnknownSupport,
UnsupportedRegime,
NonfiniteInput,
}
#[derive(Clone, Debug, PartialEq)]
pub enum StructureError {
InvalidInput {
element: Option<usize>,
kind: StructureInputErrorKind,
context: String,
},
Numerical {
element: Option<usize>,
context: String,
},
Runtime {
element: Option<usize>,
context: String,
},
}
impl StructureError {
pub fn element(&self) -> Option<usize> {
match self {
Self::InvalidInput { element, .. }
| Self::Numerical { element, .. }
| Self::Runtime { element, .. } => *element,
}
}
pub fn input_kind(&self) -> Option<StructureInputErrorKind> {
match self {
Self::InvalidInput { kind, .. } => Some(*kind),
_ => None,
}
}
pub(crate) fn input(kind: StructureInputErrorKind, context: impl Into<String>) -> Self {
Self::InvalidInput {
element: None,
kind,
context: context.into(),
}
}
pub(crate) fn definition(message: impl Into<String>) -> Self {
Self::input(StructureInputErrorKind::Definition, message)
}
pub(crate) fn numerical(context: impl Into<String>) -> Self {
Self::Numerical {
element: None,
context: context.into(),
}
}
pub(crate) fn at(mut self, index: usize) -> Self {
match &mut self {
Self::InvalidInput { element, .. }
| Self::Numerical { element, .. }
| Self::Runtime { element, .. } => *element = Some(index),
}
self
}
}
impl fmt::Display for StructureError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidInput {
element,
kind,
context,
} => write!(f, "structure input {kind:?},element={element:?}: {context}"),
Self::Numerical { element, context } => {
write!(f, "structure numerical,element={element:?}: {context}")
}
Self::Runtime { element, context } => {
write!(f, "structure runtime,element={element:?}: {context}")
}
}
}
}
impl std::error::Error for StructureError {}
impl From<numr::error::Error> for StructureError {
fn from(error: numr::error::Error) -> Self {
Self::Runtime {
element: None,
context: error.to_string(),
}
}
}
impl From<solvr::common::arithmetic::ArithmeticError> for StructureError {
fn from(error: solvr::common::arithmetic::ArithmeticError) -> Self {
use solvr::common::arithmetic::ArithmeticError;
match error {
ArithmeticError::Numr(error) => error.into(),
other => Self::Numerical {
element: None,
context: other.to_string(),
},
}
}
}
impl From<solvr::optimize::OptimizeError> for StructureError {
fn from(error: solvr::optimize::OptimizeError) -> Self {
use solvr::optimize::OptimizeError;
match error {
OptimizeError::NumrError(context) => Self::Runtime {
element: None,
context,
},
other => Self::Numerical {
element: None,
context: other.to_string(),
},
}
}
}
impl From<solvr::integrate::IntegrateError> for StructureError {
fn from(error: solvr::integrate::IntegrateError) -> Self {
use solvr::integrate::IntegrateError;
match error {
IntegrateError::NumrError(context) => Self::Runtime {
element: None,
context,
},
other => Self::Numerical {
element: None,
context: other.to_string(),
},
}
}
}
impl From<solvr::interpolate::InterpolateError> for StructureError {
fn from(error: solvr::interpolate::InterpolateError) -> Self {
Self::Runtime {
element: None,
context: error.to_string(),
}
}
}