Use thiserror for error handling with less boilerplate

This commit is contained in:
Steve Cook
2023-02-20 09:41:28 -05:00
parent 01e4f4a126
commit f8b29f37db
135 changed files with 868 additions and 905 deletions

View File

@@ -134,7 +134,7 @@ impl GenericGF {
*/
pub fn log(&self, a: i32) -> Result<i32> {
if a == 0 {
return Err(Exceptions::illegalArgument);
return Err(Exceptions::ILLEGAL_ARGUMENT);
}
// let pos: usize = a.try_into().unwrap();
Ok(self.logTable[a as usize])
@@ -145,7 +145,7 @@ impl GenericGF {
*/
pub fn inverse(&self, a: i32) -> Result<i32> {
if a == 0 {
return Err(Exceptions::arithmetic);
return Err(Exceptions::ARITHMETIC);
}
let log_t_loc: usize = a as usize;
let loc: usize = ((self.size as i32) - self.logTable[log_t_loc] - 1) as usize;

View File

@@ -50,7 +50,7 @@ impl GenericGFPoly {
*/
pub fn new(field: GenericGFRef, coefficients: &[i32]) -> Result<Self> {
if coefficients.is_empty() {
return Err(Exceptions::illegalArgumentWith(
return Err(Exceptions::illegal_argument_with(
"coefficients cannot be empty",
));
}
@@ -141,7 +141,7 @@ impl GenericGFPoly {
pub fn addOrSubtract(&self, other: &GenericGFPoly) -> Result<GenericGFPoly> {
if self.field != other.field {
return Err(Exceptions::illegalArgumentWith(
return Err(Exceptions::illegal_argument_with(
"GenericGFPolys do not have same GenericGF field",
));
}
@@ -178,7 +178,7 @@ impl GenericGFPoly {
pub fn multiply(&self, other: &GenericGFPoly) -> Result<GenericGFPoly> {
if self.field != other.field {
//if (!field.equals(other.field)) {
return Err(Exceptions::illegalArgumentWith(
return Err(Exceptions::illegal_argument_with(
"GenericGFPolys do not have same GenericGF field",
));
}
@@ -246,12 +246,12 @@ impl GenericGFPoly {
pub fn divide(&self, other: &GenericGFPoly) -> Result<(GenericGFPoly, GenericGFPoly)> {
if self.field != other.field {
return Err(Exceptions::illegalArgumentWith(
return Err(Exceptions::illegal_argument_with(
"GenericGFPolys do not have same GenericGF field",
));
}
if other.isZero() {
return Err(Exceptions::illegalArgumentWith("Divide by 0"));
return Err(Exceptions::illegal_argument_with("Divide by 0"));
}
let mut quotient = self.getZero();
@@ -260,7 +260,7 @@ impl GenericGFPoly {
let denominator_leading_term = other.getCoefficient(other.getDegree());
let inverse_denominator_leading_term = match self.field.inverse(denominator_leading_term) {
Ok(val) => val,
Err(_issue) => return Err(Exceptions::illegalArgumentWith("arithmetic issue")),
Err(_issue) => return Err(Exceptions::illegal_argument_with("arithmetic issue")),
};
while remainder.getDegree() >= other.getDegree() && !remainder.isZero() {

View File

@@ -78,7 +78,7 @@ impl ReedSolomonDecoder {
return Ok(0);
}
let Ok(syndrome) = GenericGFPoly::new(self.field, &syndromeCoefficients) else {
return Err(Exceptions::reedSolomon);
return Err(Exceptions::REED_SOLOMON);
};
let sigmaOmega = self.runEuclideanAlgorithm(
&GenericGF::buildMonomial(self.field, twoS as usize, 1),
@@ -93,11 +93,11 @@ impl ReedSolomonDecoder {
//for (int i = 0; i < errorLocations.length; i++) {
let log_value = self.field.log(errorLocations[i] as i32)?;
if log_value > received.len() as i32 - 1 {
return Err(Exceptions::reedSolomonWith("Bad error location"));
return Err(Exceptions::reed_solomon_with("Bad error location"));
}
let position: isize = received.len() as isize - 1 - log_value as isize;
if position < 0 {
return Err(Exceptions::reedSolomonWith("Bad error location"));
return Err(Exceptions::reed_solomon_with("Bad error location"));
}
received[position as usize] =
GenericGF::addOrSubtract(received[position as usize], errorMagnitudes[i]);
@@ -135,7 +135,7 @@ impl ReedSolomonDecoder {
// Divide rLastLast by rLast, with quotient in q and remainder in r
if rLast.isZero() {
// Oops, Euclidean algorithm already terminated?
return Err(Exceptions::reedSolomonWith("r_{i-1} was zero"));
return Err(Exceptions::reed_solomon_with("r_{i-1} was zero"));
}
r = rLastLast;
let mut q = r.getZero();
@@ -153,7 +153,7 @@ impl ReedSolomonDecoder {
t = (q.multiply(&tLast)?).addOrSubtract(&tLastLast)?;
if r.getDegree() >= rLast.getDegree() {
return Err(Exceptions::reedSolomonWith(format!(
return Err(Exceptions::reed_solomon_with(format!(
"Division algorithm failed to reduce polynomial? r: {r}, rLast: {rLast}"
)));
}
@@ -161,12 +161,12 @@ impl ReedSolomonDecoder {
let sigmaTildeAtZero = t.getCoefficient(0);
if sigmaTildeAtZero == 0 {
return Err(Exceptions::reedSolomonWith("sigmaTilde(0) was zero"));
return Err(Exceptions::reed_solomon_with("sigmaTilde(0) was zero"));
}
let inverse = match self.field.inverse(sigmaTildeAtZero) {
Ok(res) => res,
Err(_err) => return Err(Exceptions::reedSolomonWith("ArithmetricException")),
Err(_err) => return Err(Exceptions::reed_solomon_with("ArithmetricException")),
};
let sigma = t.multiply_with_scalar(inverse);
let omega = r.multiply_with_scalar(inverse);
@@ -194,7 +194,7 @@ impl ReedSolomonDecoder {
}
}
if e != numErrors {
return Err(Exceptions::reedSolomonWith(
return Err(Exceptions::reed_solomon_with(
"Error locator degree does not match number of roots",
));
}

View File

@@ -74,11 +74,11 @@ impl ReedSolomonEncoder {
pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<()> {
if ec_bytes == 0 {
return Err(Exceptions::illegalArgumentWith("No error correction bytes"));
return Err(Exceptions::illegal_argument_with("No error correction bytes"));
}
let data_bytes = to_encode.len() - ec_bytes;
if data_bytes == 0 {
return Err(Exceptions::illegalArgumentWith("No data bytes provided"));
return Err(Exceptions::illegal_argument_with("No data bytes provided"));
}
let fld = self.field;
let generator = self.buildGenerator(ec_bytes);
@@ -87,7 +87,7 @@ impl ReedSolomonEncoder {
//System.arraycopy(toEncode, 0, infoCoefficients, 0, dataBytes);
let mut info = GenericGFPoly::new(fld, &info_coefficients)?;
info = info.multiply_by_monomial(ec_bytes, 1)?;
let remainder = &info.divide(generator.ok_or(Exceptions::reedSolomon)?)?.1;
let remainder = &info.divide(generator.ok_or(Exceptions::REED_SOLOMON)?)?.1;
let coefficients = remainder.getCoefficients();
let num_zero_coefficients = ec_bytes - coefficients.len();
for i in 0..num_zero_coefficients {