hydr_structures/structures/
error.rs1use std::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum StructureInputErrorKind {
8 OperatingPoint,
10 Definition,
12 Identity,
14 Datum,
16 HeadConvention,
18 UnknownSupport,
20 UnsupportedRegime,
22 NonfiniteInput,
24}
25
26#[derive(Clone, Debug, PartialEq)]
28pub enum StructureError {
29 InvalidInput {
31 element: Option<usize>,
33 kind: StructureInputErrorKind,
35 context: String,
37 },
38 Numerical {
40 element: Option<usize>,
42 context: String,
44 },
45 Runtime {
47 element: Option<usize>,
49 context: String,
51 },
52}
53impl StructureError {
54 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 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 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}