1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright © 2023 Niklas Siemer
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
//! Contains our central error enum for easy error propagation.
//!
//! This module contains this crate's error enum. This enum can hold all sorts
//! of errors occurring in this crate s.t. error propagation is simple for
//! developers of this crate and all sorts of thrown errors and error types can
//! be easily found and accessed by developers using this crate. Furthermore,
//! the actual errors are wrapped s.t. all information about the error can be
//! unwrapped again.
// **For developers:**
// - How to add an error to an `enum`? First of all, find a name
// that is not too specific for your current case s.t. it could be used in other
// contexts afterwards as well. Then, find the spot according to your chosen error
// name in a alphanumerically sorted way in the list of supported errors in the doc
// comment and inside the `enum` itself.
// Afterwards, add the error to the list of implemented error
// types in the doc comment of the `enum` with a short description when it is thrown.
// Probably use this description for the doc comment above the implementation of
// error in the `enum`. Then, add `#[error(<error msg>)]` to define the error message
// output once your error is thrown. Below, write down `<error name>(<input>),` to
// define the error with its name and possibly some inputs. The input can be of the
// form [`String`], but also another error, whose conversion must be declared via
// `#[from] OtherError`. It is best to use the existing structure as a guide. For any
// further information, check out the here used [`thiserror`]-crate.
use NulError;
use Error;
/// [`MathError`] defines this crate's error enum, which can hold all sorts of
/// errors occurring in this crate.
///
/// Implemented error types:
/// - [`ConversionError`](MathError::ConversionError) is thrown if a conversion
/// between types is not possible.
/// - [`DivisionByZeroError`](MathError::DivisionByZeroError) is thrown if it is
/// tried to perform a division by `0`.
/// - [`InvalidExponent`](MathError::InvalidExponent) is thrown if an invalid
/// exponent is used for a `pow` function.
/// - [`InvalidIntegerInput`](MathError::InvalidIntegerInput) is thrown if an
/// integer input is provided as parameter that does not meet the conditions
/// of that function.
/// - [`InvalidInterval`](MathError::InvalidInterval) is thrown if an invalid
/// interval, e.g. of negative size, is provided.
/// - [`InvalidModulus`](MathError::InvalidModulus) is thrown if an integer is
/// provided, which is smaller than `2`.
/// - [`NulError`](MathError::NulError) is thrown if a [`NulError`] is thrown,
/// which currently only happens if an invalid string is given to construct
/// a [`CString`](std::ffi::CString).
/// - [`MismatchingMatrixDimension`](MathError::MismatchingMatrixDimension) is
/// thrown if arithmetic is done with matrices of mismatching dimensions.
/// - [`MismatchingModulus`](MathError::MismatchingModulus) is thrown if any
/// function is called on two objects with different modulus where equal
/// modulus is required.
/// - [`NonPositive`](MathError::NonPositive) is thrown if the function expects
/// a positive number, but a number smaller than `1` is provided.
/// - [`NoSquareMatrix`](MathError::NoSquareMatrix) is thrown if a matrix is
/// not square.
/// - [`OutOfBounds`](MathError::OutOfBounds) is thrown if a provided index
/// is not in a desired range.
/// - [`VectorFunctionCalledOnNonVector`](MathError::VectorFunctionCalledOnNonVector)
/// is thrown if a function defined on vectors was called on a matrix instance
/// that is not a vector.
///
/// # Examples
/// ```
/// use qfall_math::{error::MathError, rational::Q};
/// use std::str::FromStr;
///
/// fn parse_string_to_q() -> Result<(), MathError> {
/// let text = "2/0";
/// let q = Q::from_str(text)?;
/// return Ok(());
/// }
/// ```
/// [`StringConversionError`] defines an error enum,
/// which holds all [`String`] to data-type conversion errors.
///
/// Implemented error types:
/// - [`InvalidMatrix`](StringConversionError::InvalidMatrix) is thrown if an
/// invalid string input of a matrix is given.
/// - [`InvalidStringToPolyInput`](StringConversionError::InvalidStringToPolyInput)
/// is thrown if an invalid string is given to construct a polynomial.
/// - [`InvalidStringToPolyMissingWhitespace`](StringConversionError::InvalidStringToPolyMissingWhitespace)
/// is thrown if an invalid string is given to construct a polynomial which
/// did not contain two whitespaces.
/// - [`InvalidStringToPolyModulusInput`](StringConversionError::InvalidStringToPolyModulusInput)
/// is thrown if an invalid string is given
/// to construct a [`PolyOverZq`](crate::integer_mod_q::PolyOverZq), i.e. it is
/// not formatted correctly.
/// - [`InvalidStringToPolyRingZqInput`](StringConversionError::InvalidStringToPolyRingZqInput)
/// is thrown if an invalid string is given
/// to construct a [`PolynomialRingZq`](crate::integer_mod_q::PolynomialRingZq), i.e. it is
/// not formatted correctly.
/// - [`InvalidStringToQInput`](StringConversionError::InvalidStringToQInput)
/// is thrown if an invalid string is given to construct a [`Q`](crate::rational::Q).
/// - [`InvalidStringToZInput`](StringConversionError::InvalidStringToZInput)
/// is thrown if an invalid string is given to construct a [`Z`](crate::integer::Z).
/// - [`InvalidStringToZqInput`](StringConversionError::InvalidStringToZqInput)
/// is thrown if an invalid string is given to construct a [`Zq`](crate::integer_mod_q::Zq).
///
/// # Examples
/// ```
/// use qfall_math::error::StringConversionError;
///
/// fn throws_error() -> Result<(), StringConversionError> {
/// return Err(
/// StringConversionError::InvalidMatrix(String::from(
/// "Some silly mistake was made",
/// )),
/// );
///
/// Ok(())
/// }
/// ```