Skip to main content

hydr_structures/structures/
error.rs

1//! Typed structure-law support and numerical rejection.
2
3use std::fmt;
4
5/// Explicit input-admission category, independent from numeric representation errors.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum StructureInputErrorKind {
8    /// Incomplete or law-incompatible operation or decision-sample input.
9    OperatingPoint,
10    /// Invalid physical definition or explicit interval declaration.
11    Definition,
12    /// Unknown, duplicate, or reordered structure, endpoint, controller, or signal identity.
13    Identity,
14    /// Incompatible vertical reference.
15    Datum,
16    /// Unsupported energy-head convention.
17    HeadConvention,
18    /// Influential endpoint support is unknown.
19    UnknownSupport,
20    /// Known input lies outside declared physical or temporal support.
21    UnsupportedRegime,
22    /// Original heads, queries, or control samples contain nonfinite values.
23    NonfiniteInput,
24}
25
26/// Rejection preserves accepted state, definitions, and earlier proposals.
27#[derive(Clone, Debug, PartialEq)]
28pub enum StructureError {
29    /// Physical metadata, observations, or temporal support is unsupported.
30    InvalidInput {
31        /// Ordered element index when known.
32        element: Option<usize>,
33        /// Structured admission category.
34        kind: StructureInputErrorKind,
35        /// Offending input and corrective action.
36        context: String,
37    },
38    /// Numerical evaluation or timing cannot meet its declared contract.
39    Numerical {
40        /// Ordered element index when known.
41        element: Option<usize>,
42        /// Offending arithmetic and corrective action.
43        context: String,
44    },
45    /// A configured numerical operation cannot execute.
46    Runtime {
47        /// Ordered element index when known.
48        element: Option<usize>,
49        /// Original numerical operation diagnostic.
50        context: String,
51    },
52}
53impl StructureError {
54    /// Ordered element index without diagnostic-string parsing.
55    pub fn element(&self) -> Option<usize> {
56        match self {
57            Self::InvalidInput { element, .. }
58            | Self::Numerical { element, .. }
59            | Self::Runtime { element, .. } => *element,
60        }
61    }
62    /// Physical input category. Numerical/runtime errors retain their distinct variants.
63    pub fn input_kind(&self) -> Option<StructureInputErrorKind> {
64        match self {
65            Self::InvalidInput { kind, .. } => Some(*kind),
66            _ => None,
67        }
68    }
69    pub(crate) fn input(kind: StructureInputErrorKind, context: impl Into<String>) -> Self {
70        Self::InvalidInput {
71            element: None,
72            kind,
73            context: context.into(),
74        }
75    }
76    /// Builds a definition-kind input error.
77    pub(crate) fn definition(message: impl Into<String>) -> Self {
78        Self::input(StructureInputErrorKind::Definition, message)
79    }
80    pub(crate) fn numerical(context: impl Into<String>) -> Self {
81        Self::Numerical {
82            element: None,
83            context: context.into(),
84        }
85    }
86    pub(crate) fn at(mut self, index: usize) -> Self {
87        match &mut self {
88            Self::InvalidInput { element, .. }
89            | Self::Numerical { element, .. }
90            | Self::Runtime { element, .. } => *element = Some(index),
91        }
92        self
93    }
94}
95impl fmt::Display for StructureError {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        match self {
98            Self::InvalidInput {
99                element,
100                kind,
101                context,
102            } => write!(f, "structure input {kind:?},element={element:?}: {context}"),
103            Self::Numerical { element, context } => {
104                write!(f, "structure numerical,element={element:?}: {context}")
105            }
106            Self::Runtime { element, context } => {
107                write!(f, "structure runtime,element={element:?}: {context}")
108            }
109        }
110    }
111}
112impl std::error::Error for StructureError {}
113impl From<numr::error::Error> for StructureError {
114    fn from(error: numr::error::Error) -> Self {
115        Self::Runtime {
116            element: None,
117            context: error.to_string(),
118        }
119    }
120}
121
122impl From<solvr::common::arithmetic::ArithmeticError> for StructureError {
123    fn from(error: solvr::common::arithmetic::ArithmeticError) -> Self {
124        use solvr::common::arithmetic::ArithmeticError;
125        match error {
126            ArithmeticError::Numr(error) => error.into(),
127            other => Self::Numerical {
128                element: None,
129                context: other.to_string(),
130            },
131        }
132    }
133}
134
135impl From<solvr::optimize::OptimizeError> for StructureError {
136    fn from(error: solvr::optimize::OptimizeError) -> Self {
137        use solvr::optimize::OptimizeError;
138        match error {
139            OptimizeError::NumrError(context) => Self::Runtime {
140                element: None,
141                context,
142            },
143            other => Self::Numerical {
144                element: None,
145                context: other.to_string(),
146            },
147        }
148    }
149}
150
151impl From<solvr::integrate::IntegrateError> for StructureError {
152    fn from(error: solvr::integrate::IntegrateError) -> Self {
153        use solvr::integrate::IntegrateError;
154        match error {
155            IntegrateError::NumrError(context) => Self::Runtime {
156                element: None,
157                context,
158            },
159            other => Self::Numerical {
160                element: None,
161                context: other.to_string(),
162            },
163        }
164    }
165}
166
167impl From<solvr::interpolate::InterpolateError> for StructureError {
168    fn from(error: solvr::interpolate::InterpolateError) -> Self {
169        Self::Runtime {
170            element: None,
171            context: error.to_string(),
172        }
173    }
174}