Skip to content

Commit

Permalink
refactor: remove unused IntermediateDecimalError variants and merge…
Browse files Browse the repository at this point in the history
… into `DecimalError`
  • Loading branch information
JayWhite2357 committed Oct 22, 2024
1 parent 8c3986c commit f63ae2e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 45 deletions.
9 changes: 3 additions & 6 deletions crates/proof-of-sql/src/base/math/big_decimal_ext.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -63,18 +63,15 @@ impl BigDecimalExt for BigDecimal {
target_precision: Precision,
target_scale: i8,
) -> DecimalResult<S> {
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(),
)
.ok_or(DecimalError::RoundingError {
error: self.to_string(),
})?
.try_into()
.map_err(|e: ScalarConversionError| DecimalError::InvalidDecimal {
error: e.to_string(),
})
.try_into()?)
}
}

Expand Down
45 changes: 12 additions & 33 deletions crates/proof-of-sql/src/base/math/decimal_error.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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<T> = Result<T, DecimalError>;

Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/base/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/base/scalar/error.rs
Original file line number Diff line number Diff line change
@@ -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}"))]
Expand Down
9 changes: 5 additions & 4 deletions crates/proof-of-sql/src/sql/parse/error.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -159,10 +160,10 @@ impl From<ConversionError> for String {
}
}

impl From<IntermediateDecimalError> for ConversionError {
fn from(err: IntermediateDecimalError) -> ConversionError {
impl From<ParseBigDecimalError> for ConversionError {
fn from(err: ParseBigDecimalError) -> ConversionError {
ConversionError::DecimalConversionError {
source: DecimalError::IntermediateDecimalConversionError { source: err },
source: DecimalError::ParseError { error: err },
}
}
}
Expand Down

0 comments on commit f63ae2e

Please sign in to comment.