Skip to content

Commit

Permalink
builtin features instead
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Oct 11, 2024
1 parent 20ecbd6 commit 89f6330
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 28 deletions.
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ rust-version = "1.70"

[workspace]
members = [
"jiff-cli",
"jiff-tzdb",
"jiff-tzdb-platform",
"sqlx-jiff",
"examples/*",
"jiff-cli",
"jiff-tzdb",
"jiff-tzdb-platform",
"examples/*",
]

# Features are documented in the "Crate features" section of the crate docs:
Expand Down Expand Up @@ -75,11 +74,17 @@ tzdb-zoneinfo = ["std"]
# (This is the same dependency setup that the `getrandom` crate uses.)
js = ["dep:wasm-bindgen", "dep:js-sys"]

# This enables the integrations with the `sqlx` crate.
sqlx = ["dep:sqlx"]
sqlx-postgres = ["sqlx", "sqlx/postgres"]

[dependencies]
jiff-tzdb = { version = "0.1.1", path = "jiff-tzdb", optional = true }
log = { version = "0.4.21", optional = true }
serde = { version = "1.0.203", optional = true }

sqlx = { version = "0.8.2", optional = true }

# Note that the `cfg` gate for the `tzdb-bundle-platform` must repeat the
# target gate on this dependency. The intent is that `tzdb-bundle-platform`
# is enabled by default, but that the `tzdb-bundle-platform` crate is only
Expand Down
21 changes: 0 additions & 21 deletions sqlx-jiff/Cargo.toml

This file was deleted.

2 changes: 0 additions & 2 deletions sqlx-jiff/src/lib.rs

This file was deleted.

Empty file removed sqlx-jiff/src/postgres.rs
Empty file.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ pub mod tz;
mod util;
mod zoned;

#[cfg(feature = "sqlx")]
mod sqlx_impls;

/// Longer form documentation for Jiff.
pub mod _documentation {
#[doc = include_str!("../COMPARE.md")]
Expand Down
2 changes: 2 additions & 0 deletions src/sqlx_impls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(feature = "sqlx-postgres")]
mod postgres;
69 changes: 69 additions & 0 deletions src/sqlx_impls/postgres.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::{SignedDuration, Timestamp};
use core::str::FromStr;
use sqlx::encode::IsNull;
use sqlx::error::BoxDynError;
use sqlx::postgres::types::Oid;
use sqlx::postgres::{
PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat,
};
use sqlx::{Database, Decode, Encode, Postgres, Type};
use std::format;

impl Type<Postgres> for Timestamp {
fn type_info() -> PgTypeInfo {
// 1184 => PgType::Timestamptz
PgTypeInfo::with_oid(Oid(1184))
}
}

impl PgHasArrayType for Timestamp {
fn array_type_info() -> PgTypeInfo {
// 1185 => PgType::TimestamptzArray
PgTypeInfo::with_oid(Oid(1185))
}
}

impl Encode<'_, Postgres> for Timestamp {
fn encode_by_ref(
&self,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
// TIMESTAMP is encoded as the microseconds since the epoch
let micros =
self.duration_since(postgres_epoch_timestamp()).as_micros();
let micros = i64::try_from(micros).map_err(|_| {
format!("Timestamp {} out of range for Postgres: {micros}", self)
})?;
Encode::<Postgres>::encode(micros, buf)
}

fn size_hint(&self) -> usize {
size_of::<i64>()
}
}

impl<'r> Decode<'r, Postgres> for Timestamp {
fn decode(
value: <Postgres as Database>::ValueRef<'r>,
) -> Result<Self, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Binary => {
// TIMESTAMP is encoded as the microseconds since the epoch
let us = Decode::<Postgres>::decode(value)?;
let ts = postgres_epoch_timestamp()
.checked_add(SignedDuration::from_micros(us))?;
ts
}
PgValueFormat::Text => {
let s = value.as_str()?;
let ts = Timestamp::from_str(s)?;
ts
}
})
}
}

fn postgres_epoch_timestamp() -> Timestamp {
Timestamp::from_str("2000-01-01T00:00:00Z")
.expect("2000-01-01T00:00:00Z is a valid timestamp")
}

0 comments on commit 89f6330

Please sign in to comment.