Skip to content

Commit

Permalink
wrap civil Date and Time
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Oct 23, 2024
1 parent c3ee65b commit d8472eb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 61 deletions.
Empty file added jiff-sqlx/src/postgres/date.rs
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions jiff-sqlx/src/postgres/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod date;
mod datetime;
mod interval;
mod time;
mod timestamp;
Empty file added jiff-sqlx/src/postgres/time.rs
Empty file.
101 changes: 40 additions & 61 deletions jiff-sqlx/src/wrap_types.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,40 @@
pub trait ToTimestamp {
fn to_sqlx(self) -> Timestamp;
}

#[derive(Debug, Clone, Copy)]
pub struct Timestamp(jiff::Timestamp);

impl Timestamp {
pub fn to_jiff(self) -> jiff::Timestamp {
self.0
}
}

impl ToTimestamp for jiff::Timestamp {
fn to_sqlx(self) -> Timestamp {
Timestamp(self)
}
}

impl From<Timestamp> for jiff::Timestamp {
fn from(ts: Timestamp) -> Self {
ts.0
}
}

impl From<jiff::Timestamp> for Timestamp {
fn from(ts: jiff::Timestamp) -> Self {
Self(ts)
}
}

pub trait ToSignedDuration {
fn to_sqlx(self) -> SignedDuration;
}

#[derive(Debug, Clone, Copy)]
pub struct SignedDuration(jiff::SignedDuration);

impl SignedDuration {
pub fn to_jiff(self) -> jiff::SignedDuration {
self.0
}
}

impl ToSignedDuration for jiff::SignedDuration {
fn to_sqlx(self) -> SignedDuration {
SignedDuration(self)
}
}

impl From<SignedDuration> for jiff::SignedDuration {
fn from(sd: SignedDuration) -> Self {
sd.0
}
}

impl From<jiff::SignedDuration> for SignedDuration {
fn from(sd: jiff::SignedDuration) -> Self {
Self(sd)
}
}
macro_rules! define_wrap_type {
($wrapper:ident, $wrapper_trait:ident, $origin:ty) => {
pub trait $wrapper_trait {
fn to_sqlx(self) -> $wrapper;
}

#[derive(Debug, Clone, Copy)]
pub struct $wrapper($origin);

impl $wrapper {
pub fn to_jiff(self) -> $origin {
self.0
}
}

impl $wrapper_trait for $origin {
fn to_sqlx(self) -> $wrapper {
$wrapper(self)
}
}

impl From<$wrapper> for $origin {
fn from(value: $wrapper) -> Self {
value.0
}
}

impl From<$origin> for $wrapper {
fn from(value: $origin) -> Self {
Self(value)
}
}
};
}

define_wrap_type!(Timestamp, ToTimestamp, jiff::Timestamp);
define_wrap_type!(SignedDuration, ToSignedDuration, jiff::SignedDuration);
define_wrap_type!(Date, ToDate, jiff::civil::Date);
define_wrap_type!(Time, ToTime, jiff::civil::Time);
define_wrap_type!(DateTime, ToDateTime, jiff::civil::DateTime);

0 comments on commit d8472eb

Please sign in to comment.