Skip to content

Commit

Permalink
refactor!: migrate from thiserror to snafu
Browse files Browse the repository at this point in the history
  • Loading branch information
lgiussan committed Sep 25, 2024
1 parent ac65d71 commit edf29fe
Show file tree
Hide file tree
Showing 28 changed files with 190 additions and 217 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ rand_core = { version = "0.6", default-features = false }
rayon = { version = "1.5" }
serde = { version = "1", default-features = false }
serde_json = { version = "1" }
thiserror = { version = "1", default-features = false }
snafu = { version = "0.8.4", default-features = false }
tiny-keccak = { version = "2.0.2", features = [ "keccak" ] }
# tokio = { version = "1.39.3" }
tracing = { version = "0.1.36" }
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bigdecimal = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
lalrpop-util = { workspace = true, features = ["lexer", "unicode"] }
serde = { workspace = true, features = ["serde_derive", "alloc"] }
thiserror = { workspace = true }
snafu = { workspace = true }

[build-dependencies]
lalrpop = { workspace = true }
Expand Down
10 changes: 5 additions & 5 deletions crates/proof-of-sql-parser/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use alloc::string::String;
use thiserror::Error;
use snafu::Snafu;

/// Errors encountered during the parsing process
#[derive(Debug, Error, Eq, PartialEq)]
#[derive(Debug, Snafu, Eq, PartialEq)]
pub enum ParseError {
#[error("Unable to parse query")]
#[snafu(display("Unable to parse query"))]
/// Cannot parse the query
QueryParseError {
#[allow(missing_docs)]
error: String,
},
#[error("Unable to parse identifier")]
#[snafu(display("Unable to parse identifier"))]
/// Cannot parse the identifier
IdentifierParseError {
#[allow(missing_docs)]
error: String,
},
#[error("Unable to parse resource_id")]
#[snafu(display("Unable to parse resource_id"))]
/// Can not parse the resource_id
ResourceIdParseError {
#[allow(missing_docs)]
Expand Down
12 changes: 6 additions & 6 deletions crates/proof-of-sql-parser/src/intermediate_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ use alloc::string::String;
use bigdecimal::{num_bigint::BigInt, BigDecimal, ParseBigDecimalError, ToPrimitive};
use core::{fmt, hash::Hash, str::FromStr};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use snafu::Snafu;

/// Errors related to the processing of decimal values in proof-of-sql
#[derive(Error, Debug, PartialEq)]
#[derive(Snafu, Debug, PartialEq)]
pub enum IntermediateDecimalError {
/// Represents an error encountered during the parsing of a decimal string.
#[error("{error}")]
#[snafu(display("{error}"))]
ParseError {
#[allow(missing_docs)]
error: ParseBigDecimalError,
},
/// Error occurs when this decimal cannot fit in a primitive.
#[error("Value out of range for target type")]
#[snafu(display("Value out of range for target type"))]
OutOfRange,
/// Error occurs when this decimal cannot be losslessly cast into a primitive.
#[error("Fractional part of decimal is non-zero")]
#[snafu(display("Fractional part of decimal is non-zero"))]
LossyCast,
/// Cannot cast this decimal to a big integer
#[error("Conversion to integer failed")]
#[snafu(display("Conversion to integer failed"))]
ConversionFailure,
}
impl From<ParseBigDecimalError> for IntermediateDecimalError {
Expand Down
18 changes: 9 additions & 9 deletions crates/proof-of-sql-parser/src/posql_time/error.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use alloc::string::{String, ToString};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use snafu::Snafu;

/// Errors related to time operations, including timezone and timestamp conversions.s
#[derive(Error, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Snafu, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum PoSQLTimestampError {
/// Error when the timezone string provided cannot be parsed into a valid timezone.
#[error("invalid timezone string: {timezone}")]
#[snafu(display("invalid timezone string: {timezone}"))]
InvalidTimezone {
#[allow(missing_docs)]
timezone: String,
},

/// Error indicating an invalid timezone offset was provided.
#[error("invalid timezone offset")]
#[snafu(display("invalid timezone offset"))]
InvalidTimezoneOffset,

/// Indicates a failure to convert between different representations of time units.
#[error("Invalid time unit")]
#[snafu(display("Invalid time unit"))]
InvalidTimeUnit {
#[allow(missing_docs)]
time_unit: String,
Expand All @@ -26,27 +26,27 @@ pub enum PoSQLTimestampError {
/// The local time does not exist because there is a gap in the local time.
/// This variant may also be returned if there was an error while resolving the local time,
/// caused by for example missing time zone data files, an error in an OS API, or overflow.
#[error("Local time does not exist because there is a gap in the local time")]
#[snafu(display("Local time does not exist because there is a gap in the local time"))]
LocalTimeDoesNotExist,

/// The local time is ambiguous because there is a fold in the local time.
/// This variant contains the two possible results, in the order (earliest, latest).
#[error("Unix timestamp is ambiguous because there is a fold in the local time.")]
#[snafu(display("Unix timestamp is ambiguous because there is a fold in the local time."))]
Ambiguous {
#[allow(missing_docs)]
error: String,
},

/// Represents a catch-all for parsing errors not specifically covered by other variants.
#[error("Timestamp parsing error: {error}")]
#[snafu(display("Timestamp parsing error: {error}"))]
ParsingError {
#[allow(missing_docs)]
error: String,
},

/// Represents a failure to parse a provided time unit precision value, PoSQL supports
/// Seconds, Milliseconds, Microseconds, and Nanoseconds
#[error("Timestamp parsing error: {error}")]
#[snafu(display("Timestamp parsing error: {error}"))]
UnsupportedPrecision {
#[allow(missing_docs)]
error: String,
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ rand = { workspace = true, default-features = false, optional = true }
rayon = { workspace = true }
serde = { workspace = true, features = ["serde_derive"] }
serde_json = { workspace = true }
thiserror = { workspace = true }
snafu = { workspace = true }
tiny-keccak = { workspace = true }
tracing = { workspace = true, features = ["attributes"] }
zerocopy = { workspace = true }
Expand Down
12 changes: 7 additions & 5 deletions crates/proof-of-sql/src/base/commitment/column_bounds.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::committable_column::CommittableColumn;
use alloc::boxed::Box;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use snafu::Snafu;

/// Cannot construct bounds where min is greater than max.
#[derive(Error, Debug)]
#[error("cannot construct bounds where min is greater than max")]
#[derive(Snafu, Debug)]
#[snafu(display("cannot construct bounds where min is greater than max"))]
pub struct NegativeBounds;

/// Inner value for [`Bounds::Sharp`] and [`Bounds::Bounded`].
Expand Down Expand Up @@ -187,8 +187,10 @@ where
}

/// Columns with different [`ColumnBounds`] variants cannot operate with each other.
#[derive(Debug, Error)]
#[error("column with bounds {bounds_a:?} cannot operate with column with bounds {bounds_b:?}")]
#[derive(Debug, Snafu)]
#[snafu(display(
"column with bounds {bounds_a:?} cannot operate with column with bounds {bounds_b:?}"
))]
pub struct ColumnBoundsMismatch {
bounds_a: Box<ColumnBounds>,
bounds_b: Box<ColumnBounds>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ use super::{column_bounds::BoundsInner, committable_column::CommittableColumn, C
use crate::base::database::ColumnType;
use core::fmt::Debug;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use snafu::Snafu;

/// Errors that can occur when constructing invalid [`ColumnCommitmentMetadata`].
#[derive(Debug, Error)]
#[derive(Debug, Snafu)]
pub enum InvalidColumnCommitmentMetadata {
/// Column of this type cannot have these bounds.
#[error("column of type {column_type} cannot have bounds like {column_bounds:?}")]
#[snafu(display("column of type {column_type} cannot have bounds like {column_bounds:?}"))]
TypeBoundsMismatch {
column_type: ColumnType,
column_bounds: ColumnBounds,
},
}

/// During column operation, metadata indicates that the operand columns cannot be the same.
#[derive(Debug, Error)]
#[error("column with type {datatype_a} cannot operate with column with type {datatype_b}")]
#[derive(Debug, Snafu)]
#[snafu(display(
"column with type {datatype_a} cannot operate with column with type {datatype_b}"
))]
pub struct ColumnCommitmentMetadataMismatch {
datatype_a: ColumnType,
datatype_b: ColumnType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@ use crate::base::database::ColumnField;
use alloc::string::{String, ToString};
use indexmap::IndexMap;
use proof_of_sql_parser::Identifier;
use thiserror::Error;
use snafu::Snafu;

/// Mapping of column identifiers to column metadata used to associate metadata with commitments.
pub type ColumnCommitmentMetadataMap = IndexMap<Identifier, ColumnCommitmentMetadata>;

/// During commitment operation, metadata indicates that operand tables cannot be the same.
#[derive(Debug, Error)]
#[derive(Debug, Snafu)]
pub enum ColumnCommitmentsMismatch {
/// Anonymous metadata indicates a column mismatch.
#[error(transparent)]
#[snafu(transparent)]
ColumnCommitmentMetadata {
#[allow(missing_docs)]
#[from]
source: ColumnCommitmentMetadataMismatch,
},
/// Commitments with different column counts cannot operate with each other.
#[error("commitments with different column counts cannot operate with each other")]
#[snafu(display("commitments with different column counts cannot operate with each other"))]
NumColumns,
/// Columns with mismatched identifiers cannot operate with each other.
///
/// Strings are used here instead of Identifiers to decrease the size of this variant
#[error("column with identifier {id_a} cannot operate with column with identifier {id_b}")]
#[snafu(display(
"column with identifier {id_a} cannot operate with column with identifier {id_b}"
))]
Identifier {
#[allow(missing_docs)]
id_a: String,
Expand Down
14 changes: 6 additions & 8 deletions crates/proof-of-sql/src/base/commitment/column_commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,28 @@ use core::{iter, slice};
use indexmap::IndexSet;
use proof_of_sql_parser::Identifier;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use snafu::Snafu;

/// Cannot create commitments with duplicate identifier.
#[derive(Debug, Error)]
#[error("cannot create commitments with duplicate identifier: {id}")]
#[derive(Debug, Snafu)]
#[snafu(display("cannot create commitments with duplicate identifier: {id}"))]
pub struct DuplicateIdentifiers {
id: String,
}

/// Errors that can occur when attempting to append rows to ColumnCommitments.
#[derive(Debug, Error)]
#[derive(Debug, Snafu)]
pub enum AppendColumnCommitmentsError {
/// Metadata between new and old columns are mismatched.
#[error(transparent)]
#[snafu(transparent)]
Mismatch {
#[allow(missing_docs)]
#[from]
source: ColumnCommitmentsMismatch,
},
/// New columns have duplicate identifiers.
#[error(transparent)]
#[snafu(transparent)]
DuplicateIdentifiers {
#[allow(missing_docs)]
#[from]
source: DuplicateIdentifiers,
},
}
Expand Down
Loading

0 comments on commit edf29fe

Please sign in to comment.