Skip to content

Commit c63d33b

Browse files
authored
refactor: simplify proof-of-sql-parser (#59)
1 parent ac8aeef commit c63d33b

26 files changed

+120
-101
lines changed

crates/proof-of-sql-parser/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ lalrpop-util = { workspace = true, features = ["lexer", "unicode"] }
2323
serde = { workspace = true, features = ["serde_derive"] }
2424
thiserror = { workspace = true }
2525

26+
[features]
27+
parser-test-utility = []
28+
2629
[build-dependencies]
2730
lalrpop = { version = "0.20.0" }
2831

-44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use serde::{Deserialize, Serialize};
21
use thiserror::Error;
32

43
/// Errors encountered during the parsing process
@@ -18,46 +17,3 @@ pub enum ParseError {
1817
/// General parsing error that may occur, for example if the provided schema/object_name strings
1918
/// aren't valid postgres-style identifiers (excluding dollar signs).
2019
pub type ParseResult<T> = std::result::Result<T, ParseError>;
21-
22-
/// Errors related to time operations, including timezone and timestamp conversions.s
23-
#[derive(Error, Debug, Eq, PartialEq, Serialize, Deserialize)]
24-
pub enum PoSQLTimestampError {
25-
/// Error when the timezone string provided cannot be parsed into a valid timezone.
26-
#[error("invalid timezone string: {0}")]
27-
InvalidTimezone(String),
28-
29-
/// Error indicating an invalid timezone offset was provided.
30-
#[error("invalid timezone offset")]
31-
InvalidTimezoneOffset,
32-
33-
/// Indicates a failure to convert between different representations of time units.
34-
#[error("Invalid time unit")]
35-
InvalidTimeUnit(String),
36-
37-
/// The local time does not exist because there is a gap in the local time.
38-
/// This variant may also be returned if there was an error while resolving the local time,
39-
/// caused by for example missing time zone data files, an error in an OS API, or overflow.
40-
#[error("Local time does not exist because there is a gap in the local time")]
41-
LocalTimeDoesNotExist,
42-
43-
/// The local time is ambiguous because there is a fold in the local time.
44-
/// This variant contains the two possible results, in the order (earliest, latest).
45-
#[error("Unix timestamp is ambiguous because there is a fold in the local time.")]
46-
Ambiguous(String),
47-
48-
/// Represents a catch-all for parsing errors not specifically covered by other variants.
49-
#[error("Timestamp parsing error: {0}")]
50-
ParsingError(String),
51-
52-
/// Represents a failure to parse a provided time unit precision value, PoSQL supports
53-
/// Seconds, Milliseconds, Microseconds, and Nanoseconds
54-
#[error("Timestamp parsing error: {0}")]
55-
UnsupportedPrecision(String),
56-
}
57-
58-
// This exists because TryFrom<DataType> for ColumnType error is String
59-
impl From<PoSQLTimestampError> for String {
60-
fn from(error: PoSQLTimestampError) -> Self {
61-
error.to_string()
62-
}
63-
}

crates/proof-of-sql-parser/src/intermediate_ast.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
* https://docs.rs/vervolg/latest/vervolg/ast/enum.Statement.html
55
***/
66

7-
use crate::{
8-
intermediate_decimal::IntermediateDecimal, posql_time::timestamp::PoSQLTimestamp, Identifier,
9-
};
7+
use crate::{intermediate_decimal::IntermediateDecimal, posql_time::PoSQLTimestamp, Identifier};
108
use serde::{Deserialize, Serialize};
119

1210
/// Representation of a SetExpression, a collection of rows, each having one or more columns.

crates/proof-of-sql-parser/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ extern crate lalrpop_util;
99

1010
pub mod intermediate_ast;
1111

12-
#[cfg(test)]
12+
#[cfg(all(test, feature = "parser-test-utility"))]
1313
mod intermediate_ast_tests;
1414

15-
#[cfg(test)]
16-
pub(crate) mod test_utility;
15+
#[cfg(feature = "parser-test-utility")]
16+
/// Shortcuts to construct intermediate AST nodes.
17+
pub mod test_utility;
1718

1819
pub(crate) mod select_statement;
1920
pub use select_statement::SelectStatement;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use serde::{Deserialize, Serialize};
2+
use thiserror::Error;
3+
4+
/// Errors related to time operations, including timezone and timestamp conversions.s
5+
#[derive(Error, Debug, Eq, PartialEq, Serialize, Deserialize)]
6+
pub enum PoSQLTimestampError {
7+
/// Error when the timezone string provided cannot be parsed into a valid timezone.
8+
#[error("invalid timezone string: {0}")]
9+
InvalidTimezone(String),
10+
11+
/// Error indicating an invalid timezone offset was provided.
12+
#[error("invalid timezone offset")]
13+
InvalidTimezoneOffset,
14+
15+
/// Indicates a failure to convert between different representations of time units.
16+
#[error("Invalid time unit")]
17+
InvalidTimeUnit(String),
18+
19+
/// The local time does not exist because there is a gap in the local time.
20+
/// This variant may also be returned if there was an error while resolving the local time,
21+
/// caused by for example missing time zone data files, an error in an OS API, or overflow.
22+
#[error("Local time does not exist because there is a gap in the local time")]
23+
LocalTimeDoesNotExist,
24+
25+
/// The local time is ambiguous because there is a fold in the local time.
26+
/// This variant contains the two possible results, in the order (earliest, latest).
27+
#[error("Unix timestamp is ambiguous because there is a fold in the local time.")]
28+
Ambiguous(String),
29+
30+
/// Represents a catch-all for parsing errors not specifically covered by other variants.
31+
#[error("Timestamp parsing error: {0}")]
32+
ParsingError(String),
33+
34+
/// Represents a failure to parse a provided time unit precision value, PoSQL supports
35+
/// Seconds, Milliseconds, Microseconds, and Nanoseconds
36+
#[error("Timestamp parsing error: {0}")]
37+
UnsupportedPrecision(String),
38+
}
39+
40+
// This exists because TryFrom<DataType> for ColumnType error is String
41+
impl From<PoSQLTimestampError> for String {
42+
fn from(error: PoSQLTimestampError) -> Self {
43+
error.to_string()
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
mod error;
2+
/// Errors related to time operations, including timezone and timestamp conversions.
3+
pub use error::PoSQLTimestampError;
4+
mod timestamp;
15
/// Defines an RFC3339-formatted timestamp
2-
pub mod timestamp;
6+
pub use timestamp::PoSQLTimestamp;
7+
mod timezone;
38
/// Defines a timezone as count of seconds offset from UTC
4-
pub mod timezone;
9+
pub use timezone::PoSQLTimeZone;
10+
mod unit;
511
/// Defines the precision of the timestamp
6-
pub mod unit;
12+
pub use unit::PoSQLTimeUnit;

crates/proof-of-sql-parser/src/posql_time/timestamp.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use super::{timezone, unit::PoSQLTimeUnit};
2-
use crate::error::PoSQLTimestampError;
1+
use super::{PoSQLTimeUnit, PoSQLTimeZone, PoSQLTimestampError};
32
use chrono::{offset::LocalResult, DateTime, TimeZone, Utc};
43
use serde::{Deserialize, Serialize};
54

@@ -13,7 +12,7 @@ pub struct PoSQLTimestamp {
1312
pub timeunit: PoSQLTimeUnit,
1413

1514
/// The timezone of the datetime, either UTC or a fixed offset from UTC.
16-
pub timezone: timezone::PoSQLTimeZone,
15+
pub timezone: PoSQLTimeZone,
1716
}
1817

1918
impl PoSQLTimestamp {
@@ -32,7 +31,7 @@ impl PoSQLTimestamp {
3231
/// # Examples
3332
/// ```
3433
/// use chrono::{DateTime, Utc};
35-
/// use proof_of_sql_parser::posql_time::{timestamp::PoSQLTimestamp, timezone::PoSQLTimeZone};
34+
/// use proof_of_sql_parser::posql_time::{PoSQLTimestamp, PoSQLTimeZone};
3635
///
3736
/// // Parsing an RFC 3339 timestamp without a timezone:
3837
/// let timestamp_str = "2009-01-03T18:15:05Z";
@@ -49,7 +48,7 @@ impl PoSQLTimestamp {
4948
.map_err(|e| PoSQLTimestampError::ParsingError(e.to_string()))?;
5049

5150
let offset_seconds = dt.offset().local_minus_utc();
52-
let timezone = timezone::PoSQLTimeZone::from_offset(offset_seconds);
51+
let timezone = PoSQLTimeZone::from_offset(offset_seconds);
5352
let nanoseconds = dt.timestamp_subsec_nanos();
5453
let timeunit = if nanoseconds % 1_000 != 0 {
5554
PoSQLTimeUnit::Nanosecond
@@ -78,7 +77,7 @@ impl PoSQLTimestamp {
7877
/// # Examples
7978
/// ```
8079
/// use chrono::{DateTime, Utc};
81-
/// use proof_of_sql_parser::posql_time::{timestamp::PoSQLTimestamp, timezone::PoSQLTimeZone};
80+
/// use proof_of_sql_parser::posql_time::{PoSQLTimestamp, PoSQLTimeZone};
8281
///
8382
/// // Parsing a Unix epoch timestamp (assumed to be seconds and UTC):
8483
/// let unix_time = 1231006505;
@@ -90,7 +89,7 @@ impl PoSQLTimestamp {
9089
LocalResult::Single(timestamp) => Ok(PoSQLTimestamp {
9190
timestamp,
9291
timeunit: PoSQLTimeUnit::Second,
93-
timezone: timezone::PoSQLTimeZone::Utc,
92+
timezone: PoSQLTimeZone::Utc,
9493
}),
9594
LocalResult::Ambiguous(earliest, latest) => Err(PoSQLTimestampError::Ambiguous(
9695
format!("The local time is ambiguous because there is a fold in the local time: earliest: {} latest: {} ", earliest, latest),
@@ -107,7 +106,7 @@ mod tests {
107106
#[test]
108107
fn test_unix_epoch_time_timezone() {
109108
let unix_time = 1231006505; // Unix time as string
110-
let expected_timezone = timezone::PoSQLTimeZone::Utc; // Unix time should always be UTC
109+
let expected_timezone = PoSQLTimeZone::Utc; // Unix time should always be UTC
111110
let result = PoSQLTimestamp::to_timestamp(unix_time).unwrap();
112111
assert_eq!(result.timezone, expected_timezone);
113112
}

crates/proof-of-sql-parser/src/posql_time/timezone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::error::PoSQLTimestampError;
1+
use super::PoSQLTimestampError;
22
use core::fmt;
33
use serde::{Deserialize, Serialize};
44
use std::sync::Arc;

crates/proof-of-sql-parser/src/posql_time/unit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::error::PoSQLTimestampError;
1+
use super::PoSQLTimestampError;
22
use arrow::datatypes::TimeUnit as ArrowTimeUnit;
33
use core::fmt;
44
use serde::{Deserialize, Serialize};
@@ -68,7 +68,7 @@ impl fmt::Display for PoSQLTimeUnit {
6868
#[allow(deprecated)]
6969
mod time_unit_tests {
7070
use super::*;
71-
use crate::{error::PoSQLTimestampError, posql_time::timestamp::PoSQLTimestamp};
71+
use crate::posql_time::{PoSQLTimestamp, PoSQLTimestampError};
7272
use chrono::{TimeZone, Utc};
7373

7474
#[test]

crates/proof-of-sql-parser/src/sql.lalrpop

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::intermediate_ast;
22
use crate::select_statement;
33
use crate::identifier;
44
use lalrpop_util::ParseError::User;
5-
use crate::{intermediate_decimal::IntermediateDecimal, posql_time::timestamp::PoSQLTimestamp};
5+
use crate::{intermediate_decimal::IntermediateDecimal, posql_time::PoSQLTimestamp};
66

77
grammar;
88

0 commit comments

Comments
 (0)