From f63ae2ef794a7b075466ecb7fa1d07ee0ca71bb7 Mon Sep 17 00:00:00 2001 From: Jay White Date: Thu, 17 Oct 2024 15:33:35 -0400 Subject: [PATCH] refactor: remove unused `IntermediateDecimalError` variants and merge into `DecimalError` --- .../src/base/math/big_decimal_ext.rs | 9 ++-- .../src/base/math/decimal_error.rs | 45 +++++-------------- crates/proof-of-sql/src/base/math/mod.rs | 2 +- crates/proof-of-sql/src/base/scalar/error.rs | 2 +- crates/proof-of-sql/src/sql/parse/error.rs | 9 ++-- 5 files changed, 22 insertions(+), 45 deletions(-) diff --git a/crates/proof-of-sql/src/base/math/big_decimal_ext.rs b/crates/proof-of-sql/src/base/math/big_decimal_ext.rs index ce2e791fa..5dbdb1fc5 100644 --- a/crates/proof-of-sql/src/base/math/big_decimal_ext.rs +++ b/crates/proof-of-sql/src/base/math/big_decimal_ext.rs @@ -1,5 +1,5 @@ use super::{DecimalError, DecimalResult, Precision}; -use crate::base::scalar::{Scalar, ScalarConversionError}; +use crate::base::scalar::Scalar; use bigdecimal::BigDecimal; use num_bigint::BigInt; @@ -63,7 +63,7 @@ impl BigDecimalExt for BigDecimal { target_precision: Precision, target_scale: i8, ) -> DecimalResult { - try_convert_bigdecimal_into_bigint_with_precision_and_scale( + Ok(try_convert_bigdecimal_into_bigint_with_precision_and_scale( self, target_precision.value().into(), target_scale.into(), @@ -71,10 +71,7 @@ impl BigDecimalExt for BigDecimal { .ok_or(DecimalError::RoundingError { error: self.to_string(), })? - .try_into() - .map_err(|e: ScalarConversionError| DecimalError::InvalidDecimal { - error: e.to_string(), - }) + .try_into()?) } } diff --git a/crates/proof-of-sql/src/base/math/decimal_error.rs b/crates/proof-of-sql/src/base/math/decimal_error.rs index d2ce3c8df..37c66a250 100644 --- a/crates/proof-of-sql/src/base/math/decimal_error.rs +++ b/crates/proof-of-sql/src/base/math/decimal_error.rs @@ -1,41 +1,19 @@ +use super::InvalidPrecisionError; +use crate::base::scalar::ScalarConversionError; use alloc::string::{String, ToString}; use bigdecimal::ParseBigDecimalError; use snafu::Snafu; -use super::InvalidPrecisionError; - -/// Errors related to the processing of decimal values in proof-of-sql -#[derive(Snafu, Debug, PartialEq)] -pub enum IntermediateDecimalError { - /// Represents an error encountered during the parsing of a decimal string. - #[snafu(display("{error}"))] - ParseError { - /// The underlying error - error: ParseBigDecimalError, - }, - /// Error occurs when this decimal cannot fit in a primitive. - #[snafu(display("Value out of range for target type"))] - OutOfRange, - /// Error occurs when this decimal cannot be losslessly cast into a primitive. - #[snafu(display("Fractional part of decimal is non-zero"))] - LossyCast, - /// Cannot cast this decimal to a big integer - #[snafu(display("Conversion to integer failed"))] - ConversionFailure, -} - -impl Eq for IntermediateDecimalError {} - /// Errors related to decimal operations. -#[derive(Snafu, Debug, Eq, PartialEq)] +#[derive(Snafu, Debug, PartialEq)] pub enum DecimalError { - #[snafu(display("Invalid decimal format or value: {error}"))] + #[snafu(transparent)] /// Error when a decimal format or value is incorrect, /// the string isn't even a decimal e.g. "notastring", /// "-21.233.122" etc aka `InvalidDecimal` InvalidDecimal { /// The underlying error - error: String, + source: ScalarConversionError, }, #[snafu(transparent)] @@ -63,15 +41,16 @@ pub enum DecimalError { error: String, }, - /// Errors that may occur when parsing an intermediate decimal - /// into a posql decimal - #[snafu(transparent)] - IntermediateDecimalConversionError { - /// The underlying source error - source: IntermediateDecimalError, + /// Represents an error encountered during the parsing of a decimal string. + #[snafu(display("{error}"))] + ParseError { + /// The underlying error + error: ParseBigDecimalError, }, } +impl Eq for DecimalError {} + /// Result type for decimal operations. pub type DecimalResult = Result; diff --git a/crates/proof-of-sql/src/base/math/mod.rs b/crates/proof-of-sql/src/base/math/mod.rs index e7a905956..38540967a 100644 --- a/crates/proof-of-sql/src/base/math/mod.rs +++ b/crates/proof-of-sql/src/base/math/mod.rs @@ -16,4 +16,4 @@ pub use precision::{InvalidPrecisionError, Precision}; mod precision_test; mod decimal_error; -pub use decimal_error::{DecimalError, DecimalResult, IntermediateDecimalError}; +pub use decimal_error::{DecimalError, DecimalResult}; diff --git a/crates/proof-of-sql/src/base/scalar/error.rs b/crates/proof-of-sql/src/base/scalar/error.rs index c012a1d87..6212d1641 100644 --- a/crates/proof-of-sql/src/base/scalar/error.rs +++ b/crates/proof-of-sql/src/base/scalar/error.rs @@ -1,7 +1,7 @@ use alloc::string::String; use snafu::Snafu; -#[derive(Snafu, Debug)] +#[derive(Snafu, Debug, Eq, PartialEq)] /// These errors occur when a scalar conversion fails. pub enum ScalarConversionError { #[snafu(display("Overflow error: {error}"))] diff --git a/crates/proof-of-sql/src/sql/parse/error.rs b/crates/proof-of-sql/src/sql/parse/error.rs index 516fbf127..a86245ec3 100644 --- a/crates/proof-of-sql/src/sql/parse/error.rs +++ b/crates/proof-of-sql/src/sql/parse/error.rs @@ -1,12 +1,13 @@ use crate::base::{ database::{ColumnOperationError, ColumnType}, - math::{DecimalError, IntermediateDecimalError, InvalidPrecisionError}, + math::{DecimalError, InvalidPrecisionError}, }; use alloc::{ boxed::Box, format, string::{String, ToString}, }; +use bigdecimal::ParseBigDecimalError; use core::result::Result; use proof_of_sql_parser::{posql_time::PoSQLTimestampError, Identifier, ResourceId}; use snafu::Snafu; @@ -159,10 +160,10 @@ impl From for String { } } -impl From for ConversionError { - fn from(err: IntermediateDecimalError) -> ConversionError { +impl From for ConversionError { + fn from(err: ParseBigDecimalError) -> ConversionError { ConversionError::DecimalConversionError { - source: DecimalError::IntermediateDecimalConversionError { source: err }, + source: DecimalError::ParseError { error: err }, } } }