mathhook_core/core/polynomial/
error.rs1use crate::core::{Expression, Symbol};
6use std::fmt;
7
8#[derive(Debug, Clone, PartialEq)]
10pub enum PolynomialError {
11 DivisionByZero,
13
14 DivisionNotExact {
16 dividend: Expression,
17 divisor: Expression,
18 },
19
20 NotPolynomial {
22 expression: Expression,
23 reason: String,
24 },
25
26 WrongVariableCount {
28 expected: usize,
29 got: usize,
30 operation: &'static str,
31 },
32
33 VariableNotFound { var: Symbol },
35
36 DegreeComputationFailed { expression: Expression },
38
39 GcdComputationFailed { reason: String },
41
42 FactorizationFailed { reason: String },
44
45 NumericOverflow { operation: &'static str },
47
48 MaxIterationsExceeded {
50 operation: &'static str,
51 limit: usize,
52 },
53}
54
55impl fmt::Display for PolynomialError {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 match self {
58 PolynomialError::DivisionByZero => {
59 write!(f, "division by zero polynomial")
60 }
61 PolynomialError::DivisionNotExact { dividend, divisor } => {
62 write!(
63 f,
64 "division of {} by {} does not yield exact result",
65 dividend, divisor
66 )
67 }
68 PolynomialError::NotPolynomial { expression, reason } => {
69 write!(f, "not a polynomial: {} ({})", expression, reason)
70 }
71 PolynomialError::WrongVariableCount {
72 expected,
73 got,
74 operation,
75 } => {
76 write!(
77 f,
78 "{} requires {} variable(s), got {}",
79 operation, expected, got
80 )
81 }
82 PolynomialError::VariableNotFound { var } => {
83 write!(f, "variable {:?} not found in polynomial", var)
84 }
85 PolynomialError::DegreeComputationFailed { expression } => {
86 write!(f, "failed to compute degree of {}", expression)
87 }
88 PolynomialError::GcdComputationFailed { reason } => {
89 write!(f, "GCD computation failed: {}", reason)
90 }
91 PolynomialError::FactorizationFailed { reason } => {
92 write!(f, "factorization failed: {}", reason)
93 }
94 PolynomialError::NumericOverflow { operation } => {
95 write!(f, "numeric overflow during {}", operation)
96 }
97 PolynomialError::MaxIterationsExceeded { operation, limit } => {
98 write!(f, "{} exceeded maximum iterations ({})", operation, limit)
99 }
100 }
101 }
102}
103
104impl std::error::Error for PolynomialError {}
105
106#[allow(dead_code)]
108pub type PolynomialResult<T> = Result<T, PolynomialError>;
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn test_error_display() {
116 let err = PolynomialError::DivisionByZero;
117 assert_eq!(format!("{}", err), "division by zero polynomial");
118 }
119
120 #[test]
121 fn test_wrong_variable_count() {
122 let err = PolynomialError::WrongVariableCount {
123 expected: 1,
124 got: 3,
125 operation: "univariate division",
126 };
127 assert!(format!("{}", err).contains("1 variable(s)"));
128 assert!(format!("{}", err).contains("got 3"));
129 }
130}