1use core::fmt;
2
3use neco_algnum::AlgnumError;
4use neco_bigint::BigintError;
5use neco_formsum::FormSumErrorKind;
6use neco_monomial::MonomialErrorKind;
7
8use crate::{AtomId, ConsumerId, ExprId, StorageResource};
9
10#[derive(Debug, Eq, PartialEq)]
11pub enum StorageError {
12 CapacityOverflow {
13 resource: StorageResource,
14 },
15 AllocationFailure {
16 resource: StorageResource,
17 requested_elements: usize,
18 },
19}
20
21#[derive(Debug, Eq, PartialEq)]
22pub enum GraphError {
23 IdExhausted,
24 InvalidChildId { child: ExprId, next: ExprId },
25 Node(EvalError),
26 Storage(StorageError),
27}
28
29#[derive(Debug, Eq, PartialEq)]
30pub enum InsertError {
31 DuplicateId { resource: StorageResource, id: u32 },
32 Value(EvalError),
33 Storage(StorageError),
34}
35
36#[derive(Debug, Eq, PartialEq)]
37pub enum EvalError {
38 DivisionByZero,
39 UndefinedZeroPower,
40 ZeroToNegativePower,
41 EvenRootOfNegative,
42 Bigint(BigintError),
43 Monomial(MonomialErrorKind),
44 FormSum(FormSumErrorKind),
45 Algnum(AlgnumError),
46}
47
48#[derive(Debug, Eq, PartialEq)]
49pub enum ResolveError {
50 MissingAssignment { consumer: ConsumerId },
51 UnknownExprId { consumer: ConsumerId, expr: ExprId },
52 UnknownAtomId { expr: ExprId, atom: AtomId },
53 FloatOutOfRange { consumer: ConsumerId, expr: ExprId },
54 Evaluation(EvalError),
55 Bigint(BigintError),
56 Algnum(AlgnumError),
57 Storage(StorageError),
58}
59
60impl fmt::Display for StorageError {
61 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
62 match self {
63 Self::CapacityOverflow { resource } => {
64 write!(formatter, "capacity overflow for {resource:?}")
65 }
66 Self::AllocationFailure {
67 resource,
68 requested_elements,
69 } => write!(
70 formatter,
71 "allocation failed for {resource:?}: requested {requested_elements} elements"
72 ),
73 }
74 }
75}
76
77impl fmt::Display for GraphError {
78 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
79 match self {
80 Self::IdExhausted => formatter.write_str("expression graph ID space is exhausted"),
81 Self::InvalidChildId { child, next } => write!(
82 formatter,
83 "expression child {} is not earlier than next expression {}",
84 child.get(),
85 next.get()
86 ),
87 Self::Node(error) => write!(formatter, "expression node clone failed: {error}"),
88 Self::Storage(error) => error.fmt(formatter),
89 }
90 }
91}
92
93impl fmt::Display for InsertError {
94 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
95 match self {
96 Self::DuplicateId { resource, id } => {
97 write!(formatter, "duplicate ID {id} for {resource:?}")
98 }
99 Self::Value(error) => write!(formatter, "stored value clone failed: {error}"),
100 Self::Storage(error) => error.fmt(formatter),
101 }
102 }
103}
104
105impl fmt::Display for EvalError {
106 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
107 match self {
108 Self::DivisionByZero => formatter.write_str("division by zero"),
109 Self::UndefinedZeroPower => formatter.write_str("zero raised to the zero power"),
110 Self::ZeroToNegativePower => formatter.write_str("zero raised to a negative power"),
111 Self::EvenRootOfNegative => formatter.write_str("even root of a negative value"),
112 Self::Bigint(error) => error.fmt(formatter),
113 Self::Monomial(error) => error.fmt(formatter),
114 Self::FormSum(error) => error.fmt(formatter),
115 Self::Algnum(error) => error.fmt(formatter),
116 }
117 }
118}
119
120impl fmt::Display for ResolveError {
121 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
122 match self {
123 Self::MissingAssignment { consumer } => {
124 write!(formatter, "consumer {} has no assignment", consumer.get())
125 }
126 Self::UnknownExprId { consumer, expr } => write!(
127 formatter,
128 "consumer {} refers to unknown expression {}",
129 consumer.get(),
130 expr.get()
131 ),
132 Self::UnknownAtomId { expr, atom } => write!(
133 formatter,
134 "expression {} refers to unknown atom {}",
135 expr.get(),
136 atom.get()
137 ),
138 Self::FloatOutOfRange { consumer, expr } => write!(
139 formatter,
140 "expression {} for consumer {} is outside finite f64 range",
141 expr.get(),
142 consumer.get()
143 ),
144 Self::Evaluation(error) => error.fmt(formatter),
145 Self::Bigint(error) => error.fmt(formatter),
146 Self::Algnum(error) => error.fmt(formatter),
147 Self::Storage(error) => error.fmt(formatter),
148 }
149 }
150}
151
152#[cfg(feature = "std")]
153impl std::error::Error for StorageError {}
154
155#[cfg(feature = "std")]
156impl std::error::Error for GraphError {
157 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
158 match self {
159 Self::Node(error) => Some(error),
160 Self::Storage(error) => Some(error),
161 _ => None,
162 }
163 }
164}
165
166#[cfg(feature = "std")]
167impl std::error::Error for InsertError {
168 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
169 match self {
170 Self::Value(error) => Some(error),
171 Self::Storage(error) => Some(error),
172 Self::DuplicateId { .. } => None,
173 }
174 }
175}
176
177#[cfg(feature = "std")]
178impl std::error::Error for EvalError {
179 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
180 match self {
181 Self::Bigint(error) => Some(error),
182 Self::Monomial(error) => Some(error),
183 Self::FormSum(error) => Some(error),
184 Self::Algnum(error) => Some(error),
185 _ => None,
186 }
187 }
188}
189
190#[cfg(feature = "std")]
191impl std::error::Error for ResolveError {
192 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
193 match self {
194 Self::Evaluation(error) => Some(error),
195 Self::Bigint(error) => Some(error),
196 Self::Algnum(error) => Some(error),
197 Self::Storage(error) => Some(error),
198 _ => None,
199 }
200 }
201}
202
203impl From<BigintError> for EvalError {
204 fn from(error: BigintError) -> Self {
205 Self::Bigint(error)
206 }
207}
208
209impl From<MonomialErrorKind> for EvalError {
210 fn from(error: MonomialErrorKind) -> Self {
211 Self::Monomial(error)
212 }
213}
214
215impl From<FormSumErrorKind> for EvalError {
216 fn from(error: FormSumErrorKind) -> Self {
217 Self::FormSum(error)
218 }
219}
220
221impl From<AlgnumError> for EvalError {
222 fn from(error: AlgnumError) -> Self {
223 Self::Algnum(error)
224 }
225}
226
227impl From<StorageError> for InsertError {
228 fn from(error: StorageError) -> Self {
229 Self::Storage(error)
230 }
231}
232
233pub(crate) fn try_clone_eval_error(error: &EvalError) -> Result<EvalError, EvalError> {
234 match error {
235 EvalError::DivisionByZero => Ok(EvalError::DivisionByZero),
236 EvalError::UndefinedZeroPower => Ok(EvalError::UndefinedZeroPower),
237 EvalError::ZeroToNegativePower => Ok(EvalError::ZeroToNegativePower),
238 EvalError::EvenRootOfNegative => Ok(EvalError::EvenRootOfNegative),
239 EvalError::Bigint(error) => Ok(EvalError::Bigint(try_clone_bigint_error(error)?)),
240 EvalError::Monomial(error) => error
241 .try_clone()
242 .map(EvalError::Monomial)
243 .map_err(EvalError::Monomial),
244 EvalError::FormSum(error) => Ok(EvalError::FormSum(try_clone_formsum_error(error)?)),
245 EvalError::Algnum(error) => Ok(EvalError::Algnum(try_clone_algnum_error(error)?)),
246 }
247}
248
249fn try_clone_bigint_error(error: &BigintError) -> Result<BigintError, EvalError> {
250 Ok(match error {
251 BigintError::CapacityOverflow => BigintError::CapacityOverflow,
252 BigintError::AllocationFailure { requested_limbs } => BigintError::AllocationFailure {
253 requested_limbs: *requested_limbs,
254 },
255 BigintError::UnsignedUnderflow => BigintError::UnsignedUnderflow,
256 BigintError::DivisionByZero => BigintError::DivisionByZero,
257 BigintError::NonExactDivision => BigintError::NonExactDivision,
258 BigintError::ZeroDenominator => BigintError::ZeroDenominator,
259 BigintError::NonFiniteFloat => BigintError::NonFiniteFloat,
260 BigintError::FloatOutOfRange => BigintError::FloatOutOfRange,
261 BigintError::InvalidInterval => BigintError::InvalidInterval,
262 BigintError::ExponentOverflow { required, maximum } => BigintError::ExponentOverflow {
263 required: required.try_clone().map_err(EvalError::Bigint)?,
264 maximum: *maximum,
265 },
266 })
267}
268
269fn try_clone_formsum_error(error: &FormSumErrorKind) -> Result<FormSumErrorKind, EvalError> {
270 Ok(match error {
271 FormSumErrorKind::DivisionByZero => FormSumErrorKind::DivisionByZero,
272 FormSumErrorKind::DimensionOverflow {
273 resource,
274 required,
275 maximum,
276 } => FormSumErrorKind::DimensionOverflow {
277 resource: *resource,
278 required: required.try_clone().map_err(EvalError::Bigint)?,
279 maximum: maximum.try_clone().map_err(EvalError::Bigint)?,
280 },
281 FormSumErrorKind::AllocationFailure {
282 resource,
283 requested,
284 } => FormSumErrorKind::AllocationFailure {
285 resource: *resource,
286 requested: *requested,
287 },
288 FormSumErrorKind::Bigint(error) => FormSumErrorKind::Bigint(try_clone_bigint_error(error)?),
289 FormSumErrorKind::Monomial(error) => {
290 FormSumErrorKind::Monomial(error.try_clone().map_err(EvalError::Monomial)?)
291 }
292 })
293}
294
295fn try_clone_algnum_error(error: &AlgnumError) -> Result<AlgnumError, EvalError> {
296 Ok(match error {
297 AlgnumError::ZeroPolynomial => AlgnumError::ZeroPolynomial,
298 AlgnumError::InvalidIsolation => AlgnumError::InvalidIsolation,
299 AlgnumError::NoTargetRoot => AlgnumError::NoTargetRoot,
300 AlgnumError::MultipleTargetRoots => AlgnumError::MultipleTargetRoots,
301 AlgnumError::DivisionByZero => AlgnumError::DivisionByZero,
302 AlgnumError::UndefinedZeroPower => AlgnumError::UndefinedZeroPower,
303 AlgnumError::ZeroToNegativePower => AlgnumError::ZeroToNegativePower,
304 AlgnumError::ZeroRootDegree => AlgnumError::ZeroRootDegree,
305 AlgnumError::EvenRootOfNegative => AlgnumError::EvenRootOfNegative,
306 AlgnumError::RepresentationLimit {
307 resource,
308 required,
309 maximum,
310 } => AlgnumError::RepresentationLimit {
311 resource: *resource,
312 required: required.try_clone().map_err(EvalError::Bigint)?,
313 maximum: maximum.try_clone().map_err(EvalError::Bigint)?,
314 },
315 AlgnumError::AllocationLimit {
316 resource,
317 required,
318 maximum,
319 } => AlgnumError::AllocationLimit {
320 resource: *resource,
321 required: required.try_clone().map_err(EvalError::Bigint)?,
322 maximum: maximum.try_clone().map_err(EvalError::Bigint)?,
323 },
324 AlgnumError::AllocationFailure {
325 resource,
326 requested,
327 } => AlgnumError::AllocationFailure {
328 resource: *resource,
329 requested: *requested,
330 },
331 AlgnumError::Bigint(error) => AlgnumError::Bigint(try_clone_bigint_error(error)?),
332 AlgnumError::FormSum(error) => AlgnumError::FormSum(try_clone_formsum_error(error)?),
333 })
334}
335
336#[cfg(all(test, feature = "std"))]
337mod tests {
338 use std::error::Error;
339
340 use neco_bigint::BigintError;
341 use neco_formsum::FormSumErrorKind;
342 use neco_monomial::MonomialErrorKind;
343
344 use crate::{EvalError, GraphError, InsertError, ResolveError, StorageError, StorageResource};
345
346 #[test]
347 fn error_sources_are_exactly_the_owned_lower_failures() {
348 let bigint = EvalError::Bigint(BigintError::DivisionByZero);
349 assert!(core::ptr::eq(
350 bigint
351 .source()
352 .unwrap()
353 .downcast_ref::<BigintError>()
354 .unwrap(),
355 match &bigint {
356 EvalError::Bigint(error) => error,
357 _ => unreachable!(),
358 }
359 ));
360 let monomial = EvalError::Monomial(MonomialErrorKind::DivisionByZero);
361 assert!(monomial.source().unwrap().is::<MonomialErrorKind>());
362 let form_sum = EvalError::FormSum(FormSumErrorKind::DivisionByZero);
363 assert!(form_sum.source().unwrap().is::<FormSumErrorKind>());
364
365 let graph = GraphError::Node(EvalError::DivisionByZero);
366 assert!(graph.source().unwrap().is::<EvalError>());
367 let insert = InsertError::Value(EvalError::DivisionByZero);
368 assert!(insert.source().unwrap().is::<EvalError>());
369 let resolve = ResolveError::Evaluation(EvalError::DivisionByZero);
370 assert!(resolve.source().unwrap().is::<EvalError>());
371 let storage = StorageError::CapacityOverflow {
372 resource: StorageResource::GraphNodes,
373 };
374 let graph_storage = GraphError::Storage(storage);
375 assert!(graph_storage.source().unwrap().is::<StorageError>());
376
377 assert!(EvalError::DivisionByZero.source().is_none());
378 assert!(GraphError::IdExhausted.source().is_none());
379 assert!(InsertError::DuplicateId {
380 resource: StorageResource::AtomEntries,
381 id: 1,
382 }
383 .source()
384 .is_none());
385 }
386
387 #[test]
388 fn lower_error_conversions_preserve_variants_and_payloads() {
389 assert_eq!(
390 EvalError::from(MonomialErrorKind::AllocationFailure {
391 requested_elements: 23,
392 }),
393 EvalError::Monomial(MonomialErrorKind::AllocationFailure {
394 requested_elements: 23,
395 })
396 );
397 assert_eq!(
398 EvalError::from(FormSumErrorKind::DivisionByZero),
399 EvalError::FormSum(FormSumErrorKind::DivisionByZero)
400 );
401 assert_eq!(
402 EvalError::from(BigintError::AllocationFailure { requested_limbs: 5 }),
403 EvalError::Bigint(BigintError::AllocationFailure { requested_limbs: 5 })
404 );
405 }
406}
407
408#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
409pub enum FailureOperation {
410 Read,
411 Allocate,
412 Normalize,
413 Decide,
414 Resolve,
415 Assemble,
416}
417
418#[derive(Clone, Copy, Debug, Eq, PartialEq)]
419pub struct FailureLocation {
420 operation: FailureOperation,
421 consumer: Option<ConsumerId>,
422 expr: Option<ExprId>,
423 atom: Option<AtomId>,
424 decision: Option<u32>,
425}
426
427impl FailureLocation {
428 pub const fn operation(operation: FailureOperation) -> Self {
429 Self {
430 operation,
431 consumer: None,
432 expr: None,
433 atom: None,
434 decision: None,
435 }
436 }
437
438 pub const fn with_consumer(mut self, consumer: ConsumerId) -> Self {
439 self.consumer = Some(consumer);
440 self
441 }
442
443 pub const fn with_expr(mut self, expr: ExprId) -> Self {
444 self.expr = Some(expr);
445 self
446 }
447
448 pub const fn with_atom(mut self, atom: AtomId) -> Self {
449 self.atom = Some(atom);
450 self
451 }
452
453 pub const fn with_decision(mut self, decision: u32) -> Self {
454 self.decision = Some(decision);
455 self
456 }
457
458 pub const fn operation_kind(&self) -> FailureOperation {
459 self.operation
460 }
461
462 pub const fn consumer(&self) -> Option<ConsumerId> {
463 self.consumer
464 }
465
466 pub const fn expr(&self) -> Option<ExprId> {
467 self.expr
468 }
469
470 pub const fn atom(&self) -> Option<AtomId> {
471 self.atom
472 }
473
474 pub const fn decision(&self) -> Option<u32> {
475 self.decision
476 }
477}
478
479#[derive(Debug, Eq, PartialEq)]
480pub enum StorageFailurePayload {
481 Storage(StorageError),
482 GraphIdExhausted,
483 DuplicateId { resource: StorageResource, id: u32 },
484 Bigint(BigintError),
485 Monomial(MonomialErrorKind),
486 FormSum(FormSumErrorKind),
487 Algnum(AlgnumError),
488}
489
490#[derive(Debug, Eq, PartialEq)]
491pub enum UnsupportedFailurePayload {
492 RequiredOperation(&'static str),
493 SourceIdentity,
494 Graph(GraphError),
495 Insert(InsertError),
496 Evaluation(EvalError),
497 Resolution(ResolveError),
498}
499
500#[derive(Debug, Eq, PartialEq)]
501pub enum NecoFailure {
502 MissingAssignment {
503 location: FailureLocation,
504 },
505 UnknownExpression {
506 location: FailureLocation,
507 },
508 UnknownAtom {
509 location: FailureLocation,
510 },
511 DivisionByZero {
512 location: FailureLocation,
513 },
514 UndefinedPower {
515 location: FailureLocation,
516 },
517 EvenRootOfNegative {
518 location: FailureLocation,
519 },
520 InvalidIsolation {
521 location: FailureLocation,
522 },
523 MultipleTargetRoots {
524 location: FailureLocation,
525 },
526 FloatOutOfRange {
527 location: FailureLocation,
528 },
529 StorageFailure {
530 location: FailureLocation,
531 payload: StorageFailurePayload,
532 },
533 UnsupportedRequiredOperation {
534 location: FailureLocation,
535 payload: UnsupportedFailurePayload,
536 },
537}
538
539impl NecoFailure {
540 pub fn location(&self) -> &FailureLocation {
541 match self {
542 Self::MissingAssignment { location }
543 | Self::UnknownExpression { location }
544 | Self::UnknownAtom { location }
545 | Self::DivisionByZero { location }
546 | Self::UndefinedPower { location }
547 | Self::EvenRootOfNegative { location }
548 | Self::InvalidIsolation { location }
549 | Self::MultipleTargetRoots { location }
550 | Self::FloatOutOfRange { location }
551 | Self::StorageFailure { location, .. }
552 | Self::UnsupportedRequiredOperation { location, .. } => location,
553 }
554 }
555
556 pub fn from_graph(location: FailureLocation, error: GraphError) -> Self {
557 map_graph_failure(location, error)
558 }
559
560 pub fn from_insert(location: FailureLocation, error: InsertError) -> Self {
561 map_insert_failure(location, error)
562 }
563
564 pub fn from_evaluation(location: FailureLocation, error: EvalError) -> Self {
565 map_eval_failure(location, error)
566 }
567
568 pub fn from_resolution(location: FailureLocation, error: ResolveError) -> Self {
569 map_resolve_failure(location, error)
570 }
571}
572
573pub(crate) fn map_storage_failure(location: FailureLocation, error: StorageError) -> NecoFailure {
574 NecoFailure::StorageFailure {
575 location,
576 payload: StorageFailurePayload::Storage(error),
577 }
578}
579
580pub(crate) fn map_graph_failure(location: FailureLocation, error: GraphError) -> NecoFailure {
581 match error {
582 GraphError::IdExhausted => NecoFailure::StorageFailure {
583 location,
584 payload: StorageFailurePayload::GraphIdExhausted,
585 },
586 GraphError::InvalidChildId { child, .. } => NecoFailure::UnknownExpression {
587 location: location.with_expr(child),
588 },
589 GraphError::Node(error) => map_eval_failure(location, error),
590 GraphError::Storage(error) => map_storage_failure(location, error),
591 }
592}
593
594pub(crate) fn map_insert_failure(location: FailureLocation, error: InsertError) -> NecoFailure {
595 match error {
596 InsertError::DuplicateId { resource, id } => NecoFailure::StorageFailure {
597 location,
598 payload: StorageFailurePayload::DuplicateId { resource, id },
599 },
600 InsertError::Value(error) => map_eval_failure(location, error),
601 InsertError::Storage(error) => map_storage_failure(location, error),
602 }
603}
604
605pub(crate) fn map_eval_failure(location: FailureLocation, error: EvalError) -> NecoFailure {
606 match error {
607 EvalError::DivisionByZero => NecoFailure::DivisionByZero { location },
608 EvalError::UndefinedZeroPower | EvalError::ZeroToNegativePower => {
609 NecoFailure::UndefinedPower { location }
610 }
611 EvalError::EvenRootOfNegative => NecoFailure::EvenRootOfNegative { location },
612 EvalError::Bigint(error) => map_bigint_failure(location, error),
613 EvalError::Monomial(error) => map_monomial_failure(location, error),
614 EvalError::FormSum(error) => map_formsum_failure(location, error),
615 EvalError::Algnum(error) => map_algnum_failure(location, error),
616 }
617}
618
619pub(crate) fn map_resolve_failure(location: FailureLocation, error: ResolveError) -> NecoFailure {
620 match error {
621 ResolveError::MissingAssignment { consumer } => NecoFailure::MissingAssignment {
622 location: location.with_consumer(consumer),
623 },
624 ResolveError::UnknownExprId { consumer, expr } => NecoFailure::UnknownExpression {
625 location: location.with_consumer(consumer).with_expr(expr),
626 },
627 ResolveError::UnknownAtomId { expr, atom } => NecoFailure::UnknownAtom {
628 location: location.with_expr(expr).with_atom(atom),
629 },
630 ResolveError::FloatOutOfRange { consumer, expr } => NecoFailure::FloatOutOfRange {
631 location: location.with_consumer(consumer).with_expr(expr),
632 },
633 ResolveError::Evaluation(error) => map_eval_failure(location, error),
634 ResolveError::Bigint(error) => map_bigint_failure(location, error),
635 ResolveError::Algnum(error) => map_algnum_failure(location, error),
636 ResolveError::Storage(error) => map_storage_failure(location, error),
637 }
638}
639
640fn map_bigint_failure(location: FailureLocation, error: BigintError) -> NecoFailure {
641 match error {
642 BigintError::DivisionByZero | BigintError::ZeroDenominator => {
643 NecoFailure::DivisionByZero { location }
644 }
645 BigintError::FloatOutOfRange | BigintError::NonFiniteFloat => {
646 NecoFailure::FloatOutOfRange { location }
647 }
648 BigintError::CapacityOverflow
649 | BigintError::AllocationFailure { .. }
650 | BigintError::ExponentOverflow { .. } => NecoFailure::StorageFailure {
651 location,
652 payload: StorageFailurePayload::Bigint(error),
653 },
654 BigintError::UnsignedUnderflow
655 | BigintError::NonExactDivision
656 | BigintError::InvalidInterval => NecoFailure::UnsupportedRequiredOperation {
657 location,
658 payload: UnsupportedFailurePayload::Evaluation(EvalError::Bigint(error)),
659 },
660 }
661}
662
663fn map_monomial_failure(location: FailureLocation, error: MonomialErrorKind) -> NecoFailure {
664 match error {
665 MonomialErrorKind::DivisionByZero => NecoFailure::DivisionByZero { location },
666 MonomialErrorKind::UndefinedZeroPower | MonomialErrorKind::ZeroToNegativePower => {
667 NecoFailure::UndefinedPower { location }
668 }
669 MonomialErrorKind::EvenRootOfNegative => NecoFailure::EvenRootOfNegative { location },
670 MonomialErrorKind::CapacityOverflow | MonomialErrorKind::AllocationFailure { .. } => {
671 NecoFailure::StorageFailure {
672 location,
673 payload: StorageFailurePayload::Monomial(error),
674 }
675 }
676 MonomialErrorKind::Bigint(error) => map_bigint_failure(location, error),
677 MonomialErrorKind::InvalidRadicalBasis => NecoFailure::UnsupportedRequiredOperation {
678 location,
679 payload: UnsupportedFailurePayload::Evaluation(EvalError::Monomial(error)),
680 },
681 }
682}
683
684fn map_formsum_failure(location: FailureLocation, error: FormSumErrorKind) -> NecoFailure {
685 match error {
686 FormSumErrorKind::DivisionByZero => NecoFailure::DivisionByZero { location },
687 FormSumErrorKind::DimensionOverflow { .. } | FormSumErrorKind::AllocationFailure { .. } => {
688 NecoFailure::StorageFailure {
689 location,
690 payload: StorageFailurePayload::FormSum(error),
691 }
692 }
693 FormSumErrorKind::Bigint(error) => map_bigint_failure(location, error),
694 FormSumErrorKind::Monomial(error) => map_monomial_failure(location, error),
695 }
696}
697
698fn map_algnum_failure(location: FailureLocation, error: AlgnumError) -> NecoFailure {
699 match error {
700 AlgnumError::ZeroPolynomial | AlgnumError::InvalidIsolation | AlgnumError::NoTargetRoot => {
701 NecoFailure::InvalidIsolation { location }
702 }
703 AlgnumError::MultipleTargetRoots => NecoFailure::MultipleTargetRoots { location },
704 AlgnumError::DivisionByZero => NecoFailure::DivisionByZero { location },
705 AlgnumError::UndefinedZeroPower | AlgnumError::ZeroToNegativePower => {
706 NecoFailure::UndefinedPower { location }
707 }
708 AlgnumError::EvenRootOfNegative => NecoFailure::EvenRootOfNegative { location },
709 AlgnumError::RepresentationLimit { .. }
710 | AlgnumError::AllocationLimit { .. }
711 | AlgnumError::AllocationFailure { .. } => NecoFailure::StorageFailure {
712 location,
713 payload: StorageFailurePayload::Algnum(error),
714 },
715 AlgnumError::Bigint(error) => map_bigint_failure(location, error),
716 AlgnumError::FormSum(error) => map_formsum_failure(location, error),
717 AlgnumError::ZeroRootDegree => NecoFailure::UnsupportedRequiredOperation {
718 location,
719 payload: UnsupportedFailurePayload::Evaluation(EvalError::Algnum(error)),
720 },
721 }
722}